Exemple #1
0
        public IDisqusResponseMessage Request <T>(DisqusRequest <T> request)
        {
            DisqusResponseMessage responseMessage = null;

            using (HttpClientHandler gzipHandler = new HttpClientHandler())
            {
                if (gzipHandler.SupportsAutomaticDecompression)
                {
                    gzipHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                }

                using (var client = new HttpClient(gzipHandler))
                {
                    client.BaseAddress = new Uri("https://disqus.com/api/3.0/");
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    var resourceUri = request.GetRequestUri();

                    var endPoint = client.BaseAddress.AbsoluteUri + resourceUri;
                    PublicKey.IfSome(v => endPoint += "&api_key=" + v.Key);
                    //AccessToken.IfSome(v => endPoint += "&access_token=" + v);

                    Task <HttpResponseMessage> response = client.GetAsync(endPoint);

                    response.Wait();

                    responseMessage = new DisqusResponseMessage(response.Result);

                    // TODO Deal with failure here?
                }
            }

            return(responseMessage);
        }
Exemple #2
0
        public Either <ErrorResponseMessage, DisqusApiResponse <T> > Request <T>(DisqusRequest <T> request)
        {
            _disqusRequester.AccessToken = _accessToken;
            _disqusRequester.PublicKey   = _publicKey;

            IDisqusResponseMessage result = _disqusRequester.Request(request);

            if (result.ApplicationRateLimit.IsNone)
            {
                return(Left <ErrorResponseMessage, DisqusApiResponse <T> >(new ErrorResponseMessage(result.Body)));
            }

            DisqusApiResponse <T> apiResponse;

            int code = -1;

            ApplicationRateLimit rateLimit = null;

            result.ApplicationRateLimit.Match(
                Some: v => rateLimit = v,
                None: () => { });

            T deserialized = default(T);

            JObject jsonBody = null;

            try
            {
                jsonBody = JObject.Parse(result.Body);

                var codeFragment = jsonBody["code"];

                if (codeFragment != null)
                {
                    code = (int)codeFragment;
                }

                MemoryTraceWriter traceWriter = new MemoryTraceWriter();

                deserialized = JsonConvert.DeserializeObject <T>(result.Body, new JsonSerializerSettings
                {
                    Formatting            = Formatting.Indented,
                    MissingMemberHandling = ApplyStrictResponseParsing
                        ? MissingMemberHandling.Error
                        : MissingMemberHandling.Ignore,
                    TraceWriter = traceWriter
                });

                apiResponse = new DisqusApiResponse <T>(rateLimit, code, Right <ErrorResponseMessage, T>(deserialized));
            }
            catch (Exception ex)
            {
                string errorMessage = "Unknown error";

                var codeFragment = jsonBody?["response"];

                if (codeFragment != null)
                {
                    errorMessage = (string)codeFragment;
                }

                apiResponse = new DisqusApiResponse <T>(rateLimit, code,
                                                        Left <ErrorResponseMessage, T>(new ErrorResponseMessage(errorMessage)
                {
                    Exception = ex
                }));
            }

            return(apiResponse);
        }