/// <summary> /// Get response from ShipStation's endpoint async /// </summary> /// <typeparam name="T"></typeparam> /// <param name="command"></param> /// <param name="commandParams"></param> /// <param name="token"></param> /// <param name="operationTimeout"></param> /// <returns></returns> public async Task< T > GetResponseAsync< T >( ShipStationCommand command, string commandParams, CancellationToken token, int? operationTimeout = null ) { var url = string.Concat( this._credentials.Host, command.Command, commandParams ); var response = await GetRawResponseAsync( url, token, operationTimeout ).ConfigureAwait( false ); if ( !string.IsNullOrWhiteSpace( response ) ) return this.ParseResponse< T >( response ); return default( T ); }
/// <summary> /// Post data to ShipStation's endpoint and read response async /// </summary> /// <typeparam name="T"></typeparam> /// <param name="command"></param> /// <param name="jsonContent"></param> /// <param name="token"></param> /// <param name="shouldGetExceptionMessage"></param> /// <param name="operationTimeout"></param> /// <returns></returns> public async Task< T > PostDataAndGetResponseAsync< T >( ShipStationCommand command, string jsonContent, CancellationToken token, bool shouldGetExceptionMessage = false, int? operationTimeout = null ) { var url = string.Concat( this._credentials.Host, command.Command ); var response = await PostRawDataAsync( url, jsonContent, token, shouldGetExceptionMessage, operationTimeout ); if ( !string.IsNullOrWhiteSpace( response ) ) return this.ParseResponse< T >( response ); return default( T ); }
/// <summary> /// Post data to ShipStation's endpoint with specific partner header value /// </summary> /// <typeparam name="T"></typeparam> /// <param name="command"></param> /// <param name="jsonContent"></param> /// <param name="token"></param> /// <param name="shouldGetExceptionMessage"></param> /// <param name="operationTimeout"></param> /// <returns></returns> public T PostDataAndGetResponseWithShipstationHeader< T >( ShipStationCommand command, string jsonContent, CancellationToken token, bool shouldGetExceptionMessage = false, int? operationTimeout = null ) { var url = string.Concat( this._credentials.Host, command.Command ); int numberRequest = 0; while( numberRequest < 20 ) { numberRequest++; var data = PostRawDataAsync( url, jsonContent, token, shouldGetExceptionMessage, operationTimeout, useShipStationPartnerHeader: true ).Result; if ( !string.IsNullOrWhiteSpace( data ) ) return this.ParseResponse< T >( data ); } throw new Exception( "More 20 attempts" ); }
private HttpWebRequest CreateServicePostRequest(ShipStationCommand command, string content) { var uri = new Uri(string.Concat(this._credentials.Host, command.Command)); var request = ( HttpWebRequest )WebRequest.Create(uri); request.Method = WebRequestMethods.Http.Post; request.ContentType = "application/json"; this.CreateRequestHeaders(request); using (var writer = new StreamWriter(request.GetRequestStream())) writer.Write(content); return(request); }
public async Task PostDataAsync(ShipStationCommand command, string jsonContent) { var request = this.CreateServicePostRequest(command, jsonContent); this.LogPostInfo(this._credentials.ApiKey, request.RequestUri.AbsoluteUri, jsonContent); try { using (var response = ( HttpWebResponse )await request.GetResponseAsync()) this.LogUpdateInfo(this._credentials.ApiKey, request.RequestUri.AbsoluteUri, response.StatusCode, jsonContent); } catch (WebException x) { this.LogPostError(this._credentials.ApiKey, request.RequestUri.AbsoluteUri, x.Response.GetHttpStatusCode(), jsonContent, x); throw; } }
public async Task <T> GetResponseAsync <T>(ShipStationCommand command, string commandParams) { while (true) { var request = this.CreateGetServiceRequest(string.Concat(this._credentials.Host, command.Command, commandParams)); var resetDelay = 0; try { using (var response = await request.GetResponseAsync()) { var shipStationResponse = ProcessResponse(response); if (!shipStationResponse.IsThrottled) { return(this.ParseResponse <T>(shipStationResponse.Data)); } resetDelay = shipStationResponse.ResetInSeconds; } } catch (WebException x) { var response = x.Response; var statusCode = Convert.ToInt32(response.GetHttpStatusCode()); switch (statusCode) { case 429: resetDelay = GetLimitReset(response); break; default: throw; } } await this.CreateDelay(resetDelay); } }
/// <summary> /// Get response from ShipStation's endpoint /// </summary> /// <typeparam name="T"></typeparam> /// <param name="command"></param> /// <param name="commandParams"></param> /// <param name="token"></param> /// <param name="operationTimeout"></param> /// <returns></returns> public T GetResponse< T >( ShipStationCommand command, string commandParams, CancellationToken token, int? operationTimeout = null ) { return GetResponseAsync< T >( command, commandParams, token, operationTimeout ).GetAwaiter().GetResult(); }
/// <summary> /// Post data to ShipStation's endpoint and read response /// </summary> /// <typeparam name="T"></typeparam> /// <param name="command"></param> /// <param name="jsonContent"></param> /// <param name="token"></param> /// <param name="shouldGetExceptionMessage"></param> /// <param name="operationTimeout"></param> /// <returns></returns> public T PostDataAndGetResponse< T >( ShipStationCommand command, string jsonContent, CancellationToken token, bool shouldGetExceptionMessage = false, int? operationTimeout = null ) { return PostDataAndGetResponseAsync< T >( command, jsonContent, token, shouldGetExceptionMessage, operationTimeout ).GetAwaiter().GetResult(); }
/// <summary> /// Post data to ShipStation's endpoint async /// </summary> /// <param name="command"></param> /// <param name="jsonContent"></param> /// <param name="token"></param> /// <param name="operationTimeout"></param> /// <returns></returns> public Task PostDataAsync( ShipStationCommand command, string jsonContent, CancellationToken token, int? operationTimeout = null ) { var url = string.Concat( this._credentials.Host, command.Command ); return PostRawDataAsync( url, jsonContent, token, false, operationTimeout ); }
/// <summary> /// Post data to ShipStation's endpoint /// </summary> /// <param name="command"></param> /// <param name="jsonContent"></param> /// <param name="token"></param> /// <param name="operationTimeout"></param> public void PostData( ShipStationCommand command, string jsonContent, CancellationToken token, int? operationTimeout = null ) { PostDataAsync( command, jsonContent, token, operationTimeout ).Wait(); }