using System.Net; using TradeManageNew.APIClients.FedexApi.Models; using TradeManageNew.APIClients.FedexApi.Models.Ship; // ReSharper disable InconsistentNaming namespace TradeManageNew.APIClients.FedexApi.Services { public class ShipService : RequestService { public ShipService(FedexCredential credential) : base(credential) { } public async System.Threading.Tasks.Task CreateShipmentAsync(CreateShipmentReq body, System.Threading.CancellationToken cancellationToken = default) { var handler = new System.Net.Http.HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }; var client_ = new System.Net.Http.HttpClient(handler); client_.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate"); var disposeClient_ = true; try { using (var request_ = new System.Net.Http.HttpRequestMessage()) { var json_ = Newtonsoft.Json.JsonConvert.SerializeObject(body, JsonSerializerSettings); var content_ = new System.Net.Http.StringContent(json_); content_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json"); request_.Content = content_; request_.Method = new System.Net.Http.HttpMethod("POST"); request_.Headers.Accept.Add( System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json")); var urlBuilder_ = new System.Text.StringBuilder(); if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl); // Operation Path: "ship/v1/shipments" urlBuilder_.Append("ship/v1/shipments"); PrepareRequest(client_, request_, urlBuilder_); var url_ = urlBuilder_.ToString(); request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute); PrepareRequest(client_, request_, url_); var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken) .ConfigureAwait(false); var disposeResponse_ = true; try { var headers_ = new System.Collections.Generic.Dictionary>(); foreach (var item_ in response_.Headers) headers_[item_.Key] = item_.Value; if (response_.Content != null && response_.Content.Headers != null) { foreach (var item_ in response_.Content.Headers) headers_[item_.Key] = item_.Value; } ProcessResponse(client_, response_); var status_ = (int)response_.StatusCode; if (status_ == 200) { var objectResponse_ = await ReadObjectResponseAsync(response_, headers_, cancellationToken).ConfigureAwait(false); if (objectResponse_.Object == null) { throw new FedexAPIException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null); } return objectResponse_.Object; } else if (status_ == 400) { var objectResponse_ = await ReadObjectResponseAsync(response_, headers_, cancellationToken) .ConfigureAwait(false); if (objectResponse_.Object == null) { throw new FedexAPIException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null); } throw new FedexAPIException("Bad Request", status_, objectResponse_.Text, headers_, objectResponse_.Object, null); } else if (status_ == 401) { var objectResponse_ = await ReadObjectResponseAsync(response_, headers_, cancellationToken) .ConfigureAwait(false); if (objectResponse_.Object == null) { throw new FedexAPIException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null); } throw new FedexAPIException("Unauthorized", status_, objectResponse_.Text, headers_, objectResponse_.Object, null); } else if (status_ == 403) { var objectResponse_ = await ReadObjectResponseAsync(response_, headers_, cancellationToken) .ConfigureAwait(false); if (objectResponse_.Object == null) { throw new FedexAPIException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null); } throw new FedexAPIException("Forbidden", status_, objectResponse_.Text, headers_, objectResponse_.Object, null); } else if (status_ == 404) { var objectResponse_ = await ReadObjectResponseAsync(response_, headers_, cancellationToken) .ConfigureAwait(false); if (objectResponse_.Object == null) { throw new FedexAPIException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null); } throw new FedexAPIException("Not Found", status_, objectResponse_.Text, headers_, objectResponse_.Object, null); } else if (status_ == 500) { var objectResponse_ = await ReadObjectResponseAsync(response_, headers_, cancellationToken) .ConfigureAwait(false); if (objectResponse_.Object == null) { throw new FedexAPIException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null); } throw new FedexAPIException("Failure", status_, objectResponse_.Text, headers_, objectResponse_.Object, null); } else if (status_ == 503) { var objectResponse_ = await ReadObjectResponseAsync(response_, headers_, cancellationToken) .ConfigureAwait(false); if (objectResponse_.Object == null) { throw new FedexAPIException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null); } throw new FedexAPIException("Service Unavailable", status_, objectResponse_.Text, headers_, objectResponse_.Object, null); } else { var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false); throw new FedexAPIException( "The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, headers_, null); } } finally { if (disposeResponse_) response_.Dispose(); } } } finally { if (disposeClient_) client_.Dispose(); } } } }