예제 #1
0
파일: Client.cs 프로젝트: nmirceac/apiTools
        public static ApiClientTools.Response doPostRequest(string endpoint, Dictionary <string, string> endpointParams = null, ExpandoObject data = null)
        {
            ApiClientTools.Request request = new ApiClientTools.Request();
            request.endpointUrl    = endpoint;
            request.endpointParams = endpointParams;
            request.payloadData    = data;
            request.method         = System.Net.Http.HttpMethod.Post;

            HttpClient client   = new HttpClient(getHttpHandler());
            var        response = client.SendAsync(request.getHttpRequest()).Result;

            ApiClientTools.Response apiClientToolsResponse = ApiClientTools.Response.processResponse(request, response);
            return(apiClientToolsResponse);
        }
예제 #2
0
        public static async Task <ApiClientTools.Response> doGetRequest(string endpoint, Dictionary <string, string> endpointParams = null, Dictionary <string, string> endpointData = null)
        {
            ApiClientTools.Request request = new ApiClientTools.Request();
            request.endpointUrl     = endpoint;
            request.endpointParams  = endpointParams;
            request.endpointUrlData = endpointData;
            request.method          = System.Net.Http.HttpMethod.Get;

            HttpClient client   = new HttpClient();
            var        response = await client.SendAsync(request.getHttpRequest());

            ApiClientTools.Response apiClientToolsResponse = ApiClientTools.Response.processResponse(request, response);
            return(apiClientToolsResponse);
        }
예제 #3
0
        public static Response processResponse(ApiClientTools.Request request, System.Net.Http.HttpResponseMessage httpResponse)
        {
            Response response = new Response();

            var stringResponse = httpResponse.Content.ReadAsStringAsync().Result;

            response.success = httpResponse.IsSuccessStatusCode;

            ExpandoObject responseType = new ExpandoObject();

            if (response.success)
            {
                try {
                    response.jsonResponse = JsonConvert.DeserializeAnonymousType(stringResponse, responseType);
                } catch (JsonReaderException ex) {
                    if (ApiClientTools.Config.getDebug())
                    {
                        throw new ArgumentException("There was a problem parsing the response JSON to the request to " + request.requestUrl + "\n\n", stringResponse, ex);
                    }
                    else
                    {
                        throw new ArgumentException("There was a problem parsing the JSON\n\n", stringResponse.Substring(0, 128), ex);
                    }
                }

                if (request.method == System.Net.Http.HttpMethod.Post)
                {
                    if (!((System.Collections.Generic.IDictionary <String, object>)response.jsonResponse).ContainsKey("ack"))
                    {
                        if (ApiClientTools.Config.getDebug())
                        {
                            throw new ArgumentException("The POST response to the request to " + request.requestUrl + " doesn't include an ACK:\n\n", stringResponse);
                        }
                        else
                        {
                            throw new ArgumentException("The POST response doesn't include an ACK");
                        }
                    }
                }
            }
            else
            {
                try {
                    response.jsonResponse = JsonConvert.DeserializeAnonymousType(stringResponse, responseType);
                } catch (JsonReaderException ex) {
                    if (ApiClientTools.Config.getDebug())
                    {
                        throw new ArgumentException("There was a problem parsing the JSON of a unsuccessful response\n\n", stringResponse, ex);
                    }
                    else
                    {
                        throw new ArgumentException("There was a problem parsing the JSON or an unsuccessful response\n\n", stringResponse.Substring(0, 128), ex);
                    }
                }

                response.jsonResponse = JsonConvert.DeserializeAnonymousType(stringResponse, responseType);
                response.errorMessage = response.jsonResponse.message;
                throw new ArgumentException(response.jsonResponse.message, stringResponse);
            }

            return(response);
        }