Exemple #1
0
        /*
         *
         * This class will make the Calls to the Server.
         * Only one instance of this Class will be Created.
         * The Waiter will call this class for Hitting the API.
         *
         */
        private async Task Async(Waiter waiter)
        {
            try
            {
                // Lets create the Caller, and Call the API with the params.
                // Lets Get the Ready to call the API.
                HttpClient client = new HttpClient();

                // Add the Base Address and the End Point
                client.BaseAddress = new Uri((callType.Equals(CallType.HTTPS) ? "https://" : "http://")
                                             + waiter.url + "/" + waiter.endpoint);

                // Lets Add the Params.
                StringBuilder paramBuild = new StringBuilder();
                foreach (ParamData paramData in paramDatas)
                {
                    if (paramBuild.Length == 0)
                    {
                        paramBuild.Append("?");
                    }
                    paramBuild.Append(paramData.GetParam())
                    .Append("=")
                    .Append(paramData.GetData())
                    .Append('&');
                }

                HttpResponseMessage response = null;
                // Set the Method of the Request.
                switch (waiter.callMethod)
                {
                case CallMethod.GET:
                    response = client.GetAsync(paramBuild.ToString()).Result;
                    break;

                case CallMethod.POST:
                    response = client.PostAsync(paramBuild.ToString(), null).Result;
                    break;

                case CallMethod.PUT:
                    response = client.PutAsync(paramBuild.ToString(), null).Result;
                    break;

                case CallMethod.DELETE:
                    response = client.DeleteAsync(paramBuild.ToString()).Result;
                    break;
                }

                // Lets Execute the Request.
                if (response.IsSuccessStatusCode)
                {
                    // Get the response
                    string responseJsonString = await response.Content.ReadAsStringAsync();

                    // Sent the data to the user.
                    if (waiter.onResponse != null)
                    {
                        waiter.SendToUser(true, responseJsonString);
                    }
                    waiter.AddStatus("Executed Successfully");
                }
                else
                {
                    // The Data was not sent.
                    if (waiter.onResponse != null)
                    {
                        waiter.SendToUser(false, "Err : Not Writend Null");
                    }
                    waiter.AddStatus("Executed With an Error");
                }
                // Set to the Waiter Manager, that the tast has been ended.
                WaiterManager.End(waiter);
            }
            catch (Exception e)
            {
                // There was an Error.
                if (waiter.onResponse != null)
                {
                    waiter.SendToUser(false, "Err : Async : " + e.Message);
                }
                waiter.AddStatus("Executed With an Error : " + e.Message);
                // Set to the Waiter Manager, that the tast has been ended.
                WaiterManager.End(waiter);
            }
            // Set to the Waiter Manager, that the tast has been ended.
            WaiterManager.End(waiter);
        }