// 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())); } }
// eo GetBytes // /// <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="bytes">String to decode.</param> // private JsonValue GetJsonValue(RequestEncoding encoding, Byte[] bytes) { // switch (encoding) { // case RequestEncoding.UTF8: // return JsonValue.Load(new System.IO.StringReader(new System.Text.UTF8Encoding().GetString(bytes))); // case RequestEncoding.UTF7: // return JsonValue.Load(new System.IO.StringReader(new System.Text.UTF7Encoding().GetString(bytes))); // case RequestEncoding.ASCII: // return JsonValue.Load(new System.IO.StringReader(new System.Text.ASCIIEncoding().GetString(bytes))); // default: // throw new ArgumentException (String.Format("Encoding type {0} is not supported.", encoding.ToString())); // } // // } // eo GetJsonValue /// <summary> /// Returns the encoding type string for the request header. /// </summary> /// <returns>The encoding type string.</returns> /// <param name="encoding">Encoding.</param> private string GetEncodingTypeString(RequestDataFormatType encoding) { switch (encoding) { case RequestDataFormatType.JSON: return("application/json; encoding=utf-8"); case RequestDataFormatType.URLENCODED: return("www-form-urlencoded; encoding=utf-8"); 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)); }
// eo GetBytes // /// <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="bytes">String to decode.</param> // private JsonValue GetJsonValue(RequestEncoding encoding, Byte[] bytes) { // switch (encoding) { // case RequestEncoding.UTF8: // return JsonValue.Load(new System.IO.StringReader(new System.Text.UTF8Encoding().GetString(bytes))); // case RequestEncoding.UTF7: // return JsonValue.Load(new System.IO.StringReader(new System.Text.UTF7Encoding().GetString(bytes))); // case RequestEncoding.ASCII: // return JsonValue.Load(new System.IO.StringReader(new System.Text.ASCIIEncoding().GetString(bytes))); // default: // throw new ArgumentException (String.Format("Encoding type {0} is not supported.", encoding.ToString())); // } // // } // eo GetJsonValue /// <summary> /// Returns the encoding type string for the request header. /// </summary> /// <returns>The encoding type string.</returns> /// <param name="encoding">Encoding.</param> private string GetEncodingTypeString (RequestDataFormatType encoding) { switch (encoding) { case RequestDataFormatType.JSON: return "application/json; encoding=utf-8"; case RequestDataFormatType.URLENCODED: return "www-form-urlencoded; encoding=utf-8"; 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); }