// eo GetStringResponse /// <summary> /// Gets the string response from the passed endpoint. /// </summary> /// <returns>The string response, if any.</returns> /// <param name="url">Endpoint URL.</param> /// <param name="verb">HTTP Verb.</param> /// <param name="authToken">Auth token, if any.</param> /// <param name="requestBody">Request body.</param> /// <param name="format">Request Format.</param> /// <param name="encoding">Request Encoding.</param> /// <param name="additionalHeaders">Additional headers.</param> public async Task <WebServiceStringResult> GetStringResponse( String url, HttpVerb verb, String authToken, AuthorizationType authType = AuthorizationType.NOT_APPLICABLE, String requestBody = null, RequestDataFormatType format = RequestDataFormatType.JSON, RequestEncodingType encoding = RequestEncodingType.UTF8, List <HttpRequestHeader> additionalHeaders = null ) { // Set up the call details. if (String.IsNullOrWhiteSpace(url)) { throw new ArgumentException("URL cannot be null or whitespace."); } else { this.EndpointUri = new Uri(url); } this.RequestDataFormat = format; this.ResponseDataFormat = ResponseDataFormatType.JSON; this.RequestEncoding = encoding; this.AdditionalHeaders = additionalHeaders; this.RequestBody = requestBody; if (String.IsNullOrWhiteSpace(authToken)) { this.AuthenticationToken = null; this.AuthenticationTokenType = AuthorizationType.NOT_APPLICABLE; } else { this.AuthenticationToken = authToken; this.AuthenticationTokenType = authType; } // Make the call and return the result. switch (verb) { case HttpVerb.GET: return(await GetCall()); case HttpVerb.POST: return(await PostCall()); case HttpVerb.PUT: return(await PutCall()); case HttpVerb.DELETE: return(await DeleteCall()); default: throw new NotImplementedException(String.Format("Support for verb {0} is not yet implemented.", verb.ToString())); } }
/// <summary> /// Converts this class' encoding to the regular .NET encoding type. /// </summary> /// <returns>The dot net encoding.</returns> private Encoding GetDotNetEncoding(RequestEncodingType encoding) { switch (encoding) { case RequestEncodingType.UTF8: return(Encoding.UTF8); default: throw new ArgumentException(String.Concat("Encoding type ", this.RequestEncoding.ToString(), " not supported.")); } }
// eo GetStringResponse #endregion #region Utility /// <summary> /// Gets a byte representation of the passed string in the passed encoding type. /// </summary> /// <returns>The bytes.</returns> /// <param name="encoding">Encoding type.</param> /// <param name="stringToEncode">String to encode.</param> private byte[] GetBytes(RequestEncodingType encoding, String stringToEncode) { switch (encoding) { case RequestEncodingType.UTF8: return(new System.Text.UTF8Encoding().GetBytes(stringToEncode)); // case RequestEncodingType.UTF7: // return new System.Text.UTF7Encoding().GetBytes(stringToEncode); // case RequestEncodingType.ASCII: // return new System.Text.ASCIIEncoding().GetBytes(stringToEncode); default: throw new ArgumentException(String.Format("Encoding type {0} is not supported.", encoding.ToString())); } }
/// <summary> /// Gets the string response from the passed endpoint. First converts the request parameters hashtable into a URLEncoded request body. /// </summary> /// <returns>The string response, if any.</returns> /// <param name="url">Endpoint URL.</param> /// <param name="verb">HTTP Verb.</param> /// <param name="authToken">Auth token, if any.</param> /// <param name="requestParameters">Parameters to convert into a URLEncoded string in the request body.</param> /// <param name="format">Request Format.</param> /// <param name="encoding">Request Encoding.</param> /// <param name="additionalHeaders">Additional headers.</param> public async Task <WebServiceStringResult> GetStringResponse( String url, HttpVerb verb, String authToken, AuthorizationType authType = AuthorizationType.NOT_APPLICABLE, Dictionary <String, String> requestParameters = null, RequestDataFormatType format = RequestDataFormatType.JSON, RequestEncodingType encoding = RequestEncodingType.UTF8, List <HttpRequestHeader> additionalHeaders = null ) { // Make sure they actually passed parameters for non-GET requests. if (verb != HttpVerb.GET && (requestParameters == null || requestParameters.Count == 0)) { throw new ArgumentException("Request parameter list cannot be null or empty."); } // Convert the parameters to a URLEncoded request body. string requestBody = GetUrlEncodedParameterList(requestParameters); // Call the webservice. return(await GetStringResponse(url, verb, authToken, authType, requestBody, format, encoding, additionalHeaders)); }
/// <summary> /// Converts this class' encoding to the regular .NET encoding type. /// </summary> /// <returns>The dot net encoding.</returns> private Encoding GetDotNetEncoding (RequestEncodingType encoding) { switch (encoding) { case RequestEncodingType.UTF8: return Encoding.UTF8; default: throw new ArgumentException (String.Concat ("Encoding type ", this.RequestEncoding.ToString (), " not supported.")); } }
// eo GetStringResponse #endregion #region Utility /// <summary> /// Gets a byte representation of the passed string in the passed encoding type. /// </summary> /// <returns>The bytes.</returns> /// <param name="encoding">Encoding type.</param> /// <param name="stringToEncode">String to encode.</param> private byte[] GetBytes (RequestEncodingType encoding, String stringToEncode) { switch (encoding) { case RequestEncodingType.UTF8: return new System.Text.UTF8Encoding ().GetBytes (stringToEncode); // case RequestEncodingType.UTF7: // return new System.Text.UTF7Encoding().GetBytes(stringToEncode); // case RequestEncodingType.ASCII: // return new System.Text.ASCIIEncoding().GetBytes(stringToEncode); default: throw new ArgumentException (String.Format ("Encoding type {0} is not supported.", encoding.ToString ())); } }
// eo GetStringResponse /// <summary> /// Gets the string response from the passed endpoint. /// </summary> /// <returns>The string response, if any.</returns> /// <param name="url">Endpoint URL.</param> /// <param name="verb">HTTP Verb.</param> /// <param name="authToken">Auth token, if any.</param> /// <param name="requestBody">Request body.</param> /// <param name="format">Request Format.</param> /// <param name="encoding">Request Encoding.</param> /// <param name="additionalHeaders">Additional headers.</param> public async Task<WebServiceStringResult> GetStringResponse ( String url, HttpVerb verb, String authToken, AuthorizationType authType = AuthorizationType.NOT_APPLICABLE, String requestBody = null, RequestDataFormatType format = RequestDataFormatType.JSON, RequestEncodingType encoding = RequestEncodingType.UTF8, List<HttpRequestHeader> additionalHeaders = null ) { // Set up the call details. if (String.IsNullOrWhiteSpace (url)) throw new ArgumentException ("URL cannot be null or whitespace."); else this.EndpointUri = new Uri (url); this.RequestDataFormat = format; this.ResponseDataFormat = ResponseDataFormatType.JSON; this.RequestEncoding = encoding; this.AdditionalHeaders = additionalHeaders; this.RequestBody = requestBody; if (String.IsNullOrWhiteSpace (authToken)) { this.AuthenticationToken = null; this.AuthenticationTokenType = AuthorizationType.NOT_APPLICABLE; } else { this.AuthenticationToken = authToken; this.AuthenticationTokenType = authType; } // Make the call and return the result. switch (verb) { case HttpVerb.GET: return await GetCall (); case HttpVerb.POST: return await PostCall (); case HttpVerb.PUT: return await PutCall (); case HttpVerb.DELETE: return await DeleteCall (); default: throw new NotImplementedException (String.Format ("Support for verb {0} is not yet implemented.", verb.ToString ())); } }
/// <summary> /// Gets the string response from the passed endpoint. First converts the request parameters hashtable into a URLEncoded request body. /// </summary> /// <returns>The string response, if any.</returns> /// <param name="url">Endpoint URL.</param> /// <param name="verb">HTTP Verb.</param> /// <param name="authToken">Auth token, if any.</param> /// <param name="requestParameters">Parameters to convert into a URLEncoded string in the request body.</param> /// <param name="format">Request Format.</param> /// <param name="encoding">Request Encoding.</param> /// <param name="additionalHeaders">Additional headers.</param> public async Task<WebServiceStringResult> GetStringResponse ( String url, HttpVerb verb, String authToken, AuthorizationType authType = AuthorizationType.NOT_APPLICABLE, Dictionary<String, String> requestParameters = null, RequestDataFormatType format = RequestDataFormatType.JSON, RequestEncodingType encoding = RequestEncodingType.UTF8, List<HttpRequestHeader> additionalHeaders = null ) { // Make sure they actually passed parameters for non-GET requests. if (verb != HttpVerb.GET && (requestParameters == null || requestParameters.Count == 0)) throw new ArgumentException ("Request parameter list cannot be null or empty."); // Convert the parameters to a URLEncoded request body. string requestBody = GetUrlEncodedParameterList (requestParameters); // Call the webservice. return await GetStringResponse (url, verb, authToken, authType, requestBody, format, encoding, additionalHeaders); }