Example #1
0
 protected async Task <T> DeleteAsync <T>(string resource, object queryParams = null)
 {
     Log.Trace("DELETE " + resource);
     try
     {
         var response = await new Url(TaxJarUrl)
                        .AppendPathSegment(resource)
                        .SetQueryParams(queryParams)
                        .WithDefaults()
                        .WithOAuthBearerToken(_authToken)
                        .DeleteAsync()
                        .ReceiveJson <T>();
         return(response);
     }
     catch (FlurlHttpTimeoutException)
     {
         throw new TaxJarException("timeout", "Request timed out.");
     }
     catch (FlurlHttpException ex)
     {
         var response = ex.Call.ErrorResponseBody;
         var TaxJarEx = new TaxJarException("error", response)
         {
             Method = "DELETE", Resource = resource, HttpStatus = ex.Call.HttpStatus, HttpMessage = ex.Message
         };
         throw TaxJarEx;
     }
 }
Example #2
0
        protected async Task <T> PutAsync <T>(string resource, object body = null)
        {
            Log.Trace("PUT " + resource);
            try
            {
                var request = new Url(TaxJarUrl)
                              .AppendPathSegment(resource)
                              .WithDefaults()
                              .WithOAuthBearerToken(_authToken);
                var response = await request.SendUrlEncodedAsync(HttpMethod.Put, body)
                               .ReceiveJson <T>();

                return(response);
            }
            catch (FlurlHttpTimeoutException)
            {
                throw new TaxJarException("timeout", "Request timed out.");
            }
            catch (FlurlHttpException ex)
            {
                var response = ex.Call.ErrorResponseBody;
                var TaxJarEx = new TaxJarException("error", response)
                {
                    Method = "PUT", Resource = resource, HttpStatus = ex.Call.HttpStatus, HttpMessage = ex.Message, RequestBody = ex.Call.RequestBody
                };
                throw TaxJarEx;
            }
        }
Example #3
0
        protected async Task <T> GetAsync <T>(string resource, object queryParams = null) where T : class
        {
            Log.Trace("GET " + resource);
            try
            {
                var response = await new Url(TaxJarUrl)
                               .AppendPathSegment(resource)
                               .SetQueryParams(queryParams)
                               .WithDefaults()
                               .WithOAuthBearerToken(_authToken)
                               .GetAsync();
                Log.Trace(response.RequestMessage.ToString);
                var responseBody = await response.Content.ReadAsStringAsync();

                var settings = new JsonSerializerSettings
                {
                    Error = (sender, args) =>
                    {
                        if (Debugger.IsAttached)
                        {
                            Debugger.Break();
                        }
                    }
                };
                var responseDeserialized = JsonConvert.DeserializeObject <T>(responseBody, settings);
                return(responseDeserialized);
            }
            catch (FlurlHttpTimeoutException)
            {
                throw new TaxJarException("timeout", "Request timed out.");
            }
            catch (FlurlHttpException ex)
            {
                var response = await ex.Call.Response.Content.ReadAsStringAsync();

                var TaxJarEx = new TaxJarException("error", response)
                {
                    Method = "GET", Resource = resource, HttpStatus = ex.Call.HttpStatus
                };
                throw TaxJarEx;
            }
        }