/// <summary> /// Validates the response against HTTP errors defined at the API level /// </summary> /// <param name="_response">The response recieved</param> /// <param name="_context">Context of the request and the recieved response</param> internal void ValidateResponse(HttpResponse _response, HttpContext _context) { if ((_response.StatusCode < 200) || (_response.StatusCode > 206)) //[200,206] = HTTP OK throw new APIException(@"HTTP Response Not OK", _context); }
/// <summary> /// Create a new media object to be processed. /// </summary> /// <param name="source">Optional parameter: The source URL of the media.</param> /// <return>Returns the MediaResponse response from the API call</return> public async Task<MediaResponse> CreateMediaAsync(string source = null) { //the base uri for api requestss string _baseUri = Configuration.BaseUri; //prepare query string for API call StringBuilder _queryBuilder = new StringBuilder(_baseUri); _queryBuilder.Append("/media"); //process optional query parameters APIHelper.AppendUrlWithQueryParameters(_queryBuilder, new Dictionary<string, object>() { { "source", source } }); //validate and preprocess url string _queryUrl = APIHelper.CleanUrl(_queryBuilder); //append request with appropriate headers and parameters var _headers = new Dictionary<string,string>() { { "user-agent", "APIMATIC 2.0" }, { "accept", "application/json" } }; _headers.Add("Content-Type", Configuration.ContentType); //prepare the API call request to fetch the response HttpRequest _request = ClientInstance.Post(_queryUrl, _headers, null); //Custom Authentication to be added for authorization AuthUtility.AppendCustomAuthParams(_request); //invoke request and get response HttpStringResponse _response = (HttpStringResponse) await ClientInstance.ExecuteAsStringAsync(_request); HttpContext _context = new HttpContext(_request,_response); //return null on 404 if (_response.StatusCode == 404) return null; //handle errors defined at the API level base.ValidateResponse(_response, _context); try { return APIHelper.JsonDeserialize<MediaResponse>(_response.Body); } catch (Exception _ex) { throw new APIException("Failed to parse the response: " + _ex.Message, _context); } }
/// <summary> /// Initialization constructor /// </summary> /// <param name="reason"> The reason for throwing exception </param> /// <param name="context"> The HTTP context that encapsulates request and response objects </param> public APIException(string reason, HttpContext context) : base(reason) { this.HttpContext = context; }