예제 #1
0
        public void TryParseJson_EmptyString()
        {
            UserResult result;
            var        isSuccess = JsonEntity.TryParseJson(string.Empty, out result);

            Assert.IsTrue(isSuccess);
            Assert.IsNull(result);
        }
예제 #2
0
        public void TryParseJson_InvalidJson()
        {
            UserResult result;
            var        isSuccess = JsonEntity.TryParseJson("this is not JSON", out result);

            Assert.IsFalse(isSuccess);
            Assert.IsNull(result);
        }
예제 #3
0
        public void TryParseJson_Null()
        {
            UserResult result;
            var        isSuccess = JsonEntity.TryParseJson(null, out result);

            Assert.IsFalse(isSuccess);
            Assert.IsNull(result);
        }
예제 #4
0
            protected async override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
            {
                HttpResponseMessage response;

                try
                {
                    response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    throw new GogsKitRequestException(ex.Message, ex)
                          {
                              RequestUri = request?.RequestUri,
                          };
                }

                if (response == null)
                {
                    throw new GogsKitRequestException("No response")
                          {
                              RequestUri = request?.RequestUri,
                          };
                }

                if (!response.IsSuccessStatusCode)
                {
                    string responseString = null;
                    if (response.Content != null)
                    {
                        try
                        {
                            responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                        }
                        catch (Exception)
                        {
                            responseString = null;
                        }
                    }

                    ErrorResultWrapper errorResultWrapper = null;
                    if (!string.IsNullOrEmpty(responseString) && response.Content.Headers.ContentType?.MediaType == "application/json")
                    {
                        JsonEntity.TryParseJson(responseString, out errorResultWrapper);
                    }

                    try
                    {
                        response.EnsureSuccessStatusCode();
                    }
                    catch (HttpRequestException ex)
                    {
                        var message = errorResultWrapper?.Error ?? ex.Message;

                        GogsKitResponseException responseException;
                        if (response.StatusCode == HttpStatusCode.Unauthorized)
                        {
                            responseException = new GogsKitUnauthorizedException(message, ex);
                        }
                        else if (response.StatusCode == HttpStatusCode.NotFound)
                        {
                            responseException = new GogsKitNotFoundException(message, ex);
                        }
                        else
                        {
                            responseException = new GogsKitResponseException(message, ex);
                        }
                        responseException.RequestUri         = request?.RequestUri;
                        responseException.ResponseStatusCode = (int)response.StatusCode;
                        responseException.ResponseContent    = responseString;
                        throw responseException;
                    }
                }
                return(response);
            }