Esempio n. 1
0
        private void ApplyAuth(HttpClient client)
        {
            var byteArray = Encoding.ASCII.GetBytes($"{this.Username}:{this.Apikey}");

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
            DebugWriteline?.Invoke(client.DefaultRequestHeaders.ToString());
        }
Esempio n. 2
0
        private void DebugResponse(HttpResponseMessage response, string responseContent)
        {
            DebugWriteline?.Invoke("---Response---");
            DebugWriteline?.Invoke(response.ToString());
            DebugWriteline?.Invoke("Request Headers");


            DebugWriteline?.Invoke(responseContent);
        }
Esempio n. 3
0
        private HttpContent constructJsonContent(IDictionary <string, dynamic> dictionary)
        {
            var         json    = JsonSerializer.Serialize(dictionary);
            HttpContent content = new StringContent(json);

            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            DebugWriteline?.Invoke("JSON: " + json);

            return(content);
        }
Esempio n. 4
0
 private void DebugRequest(HttpClient client, HttpRequestMessage request)
 {
     DebugWriteline?.Invoke("---Request---");
     DebugWriteline?.Invoke(request.ToString());
     DebugWriteline?.Invoke("Request Headers");
     foreach (var item in request.Headers)
     {
         DebugWriteline?.Invoke(item.Key + " -> " + String.Join("", item.Value));
     }
     DebugWriteline?.Invoke("Client Default Headers");
     foreach (var item in client.DefaultRequestHeaders)
     {
         DebugWriteline?.Invoke(item.Key + " -> " + String.Join("", item.Value));
     }
     DebugWriteline?.Invoke("Request Properties");
     foreach (var item in request.Properties)
     {
         DebugWriteline?.Invoke(item.Key + " -> " + item.Value);
     }
 }
Esempio n. 5
0
        internal async Task <string> MakeJSONRequestAsync(string apiUrl, string httpMethod, IDictionary <string, dynamic> Parameters = null, IDictionary <string, dynamic> queryParameters = null)
        {
            const string ResponseType = "json";
            var          url          = string.Format("{0}{1}.{2}", BaseUrl, apiUrl, ResponseType);

            using (var client = new HttpClient())
            {
                ApplyAuth(client);

                HttpContent content = null;
                bool        containsQueryParameters = false;
                switch (httpMethod)
                {
                case "GET":
                    if (Parameters != null && Parameters.Count > 0)
                    {
                        url += "?" + this.DictionaryQuerystring(Parameters);
                        containsQueryParameters = true;
                    }

                    break;

                case "POST":
                case "PUT":
                case "DELETE":
                    content = constructJsonContent(Parameters);
                    break;

                default:
                    throw new ChallongeException("Unknown HttpMethod: " + httpMethod);
                }

                if (queryParameters != null && queryParameters.Count > 0)
                {
                    url += (containsQueryParameters ? "&" : "?") + this.DictionaryQuerystring(queryParameters);
                }
                DebugWriteline?.Invoke(httpMethod + " " + url);

                HttpRequestMessage request = new HttpRequestMessage();
                request.RequestUri = new Uri(url);
                request.Method     = parseMethod(httpMethod);
                if (Parameters != null && Parameters.Count > 0)
                {
                    request.Content = content;
                }

                HttpResponseMessage response = await client.SendAsync(request);

                var responseContent = await response.Content.ReadAsStringAsync();

                DebugResponse(response, responseContent);

                if (response.IsSuccessStatusCode)
                {
                    return(responseContent);
                }

                throw new ChallongeException(responseContent);
            }
            throw new ChallongeException("Something went wrong sending the request.");
        }