Page 1 of 1

DebtorNotesGETManyRequest API Key Bug

PostPosted: Sun Oct 14, 2018 11:19 am
by SBarnes
Hi Guys

I think I have found a bug with DebtorNotesGETManyRequest in the Web API, basically I have a situation where I use the same JSonServiceClient to make a number of calls to the API before logging off, using a user name and password the code works perfectly but as soon as you change it to use an API key the call to DebtorNotesGETManyRequest produces a null reference exception on the manager, interestingly subsequent calls using the same client work so long as the exception has been caught and ignored.

A call to the same route with a brand new JSonServiceClient also fails.

Re: DebtorNotesGETManyRequest API Key Bug

PostPosted: Wed Nov 21, 2018 8:45 am
by Scott.Pearce
Working OK for me using Postman to hit https://api.jiwa.com.au/Debtors/0000000 ... 001V/Notes with an API Key. Are you able to provide further information, perhaps a test project that demonstrates the issue?

Re: DebtorNotesGETManyRequest API Key Bug

PostPosted: Wed Nov 21, 2018 5:15 pm
by SBarnes
Hi Scott,

It happens using JsonServiceClient, I am adding extra headers but I am not clearing them so that can't be the problem and I have a general routine to login as shown below, you can see where I tried the API key and then commented it out:

Code: Select all
public static JsonServiceClient GetAPIClient(string URL = "", bool LoggedIn = true)
        {
            string methodName = MethodBase.GetCurrentMethod().Name;
            string className = "ERP";
            ERP.LogString("User: " + ERP.GetUser().UserLogString(), "Class : " + className + " Method: " + methodName);

            try
            {
               // JsConfig.IncludeNullValues = true;
               // ServiceStack.ClientConfig.SkipEmptyArrays
                if (URL == "")
                {
                    URL = ConfigManager().GetString("JiwaAPIURL");
                }
               
                JsonServiceClient client = new JsonServiceClient(URL);
                client.Headers.Add("jiwa-stateful", "false");
                client.Timeout = new TimeSpan(0, ERP.ConfigManager().GetInt("APITimeOut"), 0);
                client.ReadWriteTimeout = new TimeSpan(0, ERP.ConfigManager().GetInt("APITimeOut"), 0);
                //var cookie1 = new Cookie("USERDETAILS", GetUser().UserLogString());
                client.SetCookie("USERDETAILS", GetUser().UserSSString());
               // client.CookieContainer.Add(cookie1);
               

                if (bool.Parse(ConfigManager().GetString("LogRequests")))
                {
                    client.RequestFilter = request =>
                    {
                        //request.GetRequestStream
                        //string dstring = request.Dump();
                        //string json = request.ToSafeJson();
                        //string strreq = request.SerializeToString();
                        string strMethod = request.Method;
                        string straddress = request.Address.ToString();
                        ERP.LogString("Request ", "Method : " + strMethod + " Address: " + straddress);

                       

                    };
                }

                //{
                //    //string strresp = response.SerializeToString();
                //    //using (var stream = request.GetRequestStream())
                //    //using (var sr = new StreamReader(stream))
                //    //{
                //    //    var text = sr.ReadToEnd();

                //    //}
                //};
                if (bool.Parse(ConfigManager().GetString("LogResponses")))
                {
                    client.ResponseFilter = response =>
                    {

                        string strStatusCode = response.StatusDescription;
                        //string strresp = response.SerializeToString();
                        string straddress = response.ResponseUri.ToString();
                        ERP.LogString("Response ", "Status Code : " + strStatusCode + " Address: " + straddress);
                        //using (var stream = response.GetResponseStream())
                        //using (var sr = new StreamReader(stream))
                        //{
                        //    var text = sr.ReadToEnd();

                        //}
                    };
                }



                //client.Headers.Add("Authorization", "BearerAPI KEy goes here");
                //return client;
                if (LoggedIn)
                {
                    var authResponse = client.Send<ServiceStack.AuthenticateResponse>(new ServiceStack.Authenticate()
                    {
                        provider = "credentials",

                        UserName = ConfigManager().GetString("JiwaAPIUser"),
                        Password = ConfigManager().GetString("JiwaAPIPassword"),
                        RememberMe = true
                    });
                }
                return client;

            }
            catch (Exception ex)
            {
                string error = ex.Message;
                return null;
            }
        }



and the routine that calls the request is

Code: Select all
public static  List<Note> GetJiwaAPIDebtorNotes(JsonServiceClient inClient = null, string DebtorID = "")
        {
            string methodName = MethodBase.GetCurrentMethod().Name;
            string className = "ERP";
            ERP.LogString("User: " + ERP.GetUser().UserLogString(), "Class : " + className + " Method: " + methodName);

            List<Note> result = new List<Note>();
            DebtorNotesGETManyRequest req = new DebtorNotesGETManyRequest();
            try
            {
                User user = GetUser();
                if (DebtorID == "")
                {
                    DebtorID = user.DebtorID;
                }
                req.DebtorID = DebtorID;

                JsonServiceClient client;
                bool nullclient = inClient == null;
                if (nullclient)
                {
                    client = GetAPIClient();
                }
                else
                {
                    client = inClient;
                }
                result = client.Get<List<Note>>(req);
                if (nullclient)
                {
                    Logout(client);
                }

            }
            catch (Exception ex)
            {
                result = new List<Note>();
            }
            return result;

        }



I can't actually send you the project as its fairly massive and is an upgrade of a current production system that was written about fifteen years ago. By the way GetAPIClient works with the API Key for other request just not the notes get many.