public async Task <TResult> StartRequest <TPayload, TResult>( Endpoint endpoint, string method, string path, TPayload payload) where TPayload : class where TResult : class { var key = endpoint.KeySelector(Credentials); // creates initial request // TODO: Dispose request. var request = Roundtripper.CreateRequest(method, endpoint.ApiPrefix + path); request.Headers.Accept.Clear(); request.Headers.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); request.Headers.Add("Authorization", key.EncodeForOAuthorizationHeader()); request.Headers.Add("User-Agent", userAgent); if (!string.IsNullOrEmpty(APIVersion)) { request.Headers.Add("Flowaccount-Version", APIVersion); } try { if (payload != null) { request.Content = Serializer.ExtractJsonValue <TPayload>(payload); } ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; var response = await Roundtripper.Roundtrip(request); var stream = await response.Content.ReadAsStreamAsync(); if (!response.IsSuccessStatusCode) { var error = Serializer.JsonDeserialize <ErrorResult>(stream); error.HttpStatusCode = response.StatusCode; throw new FlowAPIError(error, null); } //var apiResponse = Serializer.JsonDeserialize<TResult>(stream); var apiResponse = Serializer.JsonDeserialize <APIResponse <TResult> >(stream); var result = apiResponse.Data; var model = result as BaseModel; if (model != null) { model.Request = this; } return(result); //return null; } catch (HttpRequestException e) { throw new FlowAPIException("Error while making HTTP request", e); } }
public async Task <TResult> StartRequest <TPayload, TResult>( Endpoint endpoint, string method, string path, TPayload payload) where TPayload : class where TResult : class { var key = endpoint.KeySelector(Credentials); // creates initial request // TODO: Dispose request. var request = Roundtripper.CreateRequest(method, endpoint.ApiPrefix + path); request.Headers.Add("Authorization", key.EncodeForAuthorizationHeader()); request.Headers.Add("User-Agent", userAgent); if (!string.IsNullOrEmpty(APIVersion)) { request.Headers.Add("Flowaccount-Version", APIVersion); } if (payload != null) { request.Content = Serializer.ExtractFormValues(payload); } // roundtrips the request try { var response = await Roundtripper.Roundtrip(request); var stream = await response.Content.ReadAsStreamAsync(); if (!response.IsSuccessStatusCode) { var error = Serializer.JsonDeserialize <ErrorResult>(stream); error.HttpStatusCode = response.StatusCode; throw new FlowAPIError(error, null); } var result = Serializer.JsonDeserialize <TResult>(stream); var model = result as BaseModel; if (model != null) { model.Request = this; } return(result); } catch (HttpRequestException e) { throw new FlowAPIException("Error while making HTTP request", e); } }
public async Task <TResult> Request <TPayload, TResult>( Endpoint endpoint, string method, string path, TPayload payload) where TPayload : class where TResult : class { var apiPrefix = Environment.ResolveEndpoint(endpoint); var key = Environment.SelectKey(endpoint, Credentials); // creates initial request // TODO: Dispose request. var request = Roundtripper.CreateRequest(method, apiPrefix + path); request.Headers.Add("Authorization", key.EncodeForAuthorizationHeader()); request.Headers.Add("User-Agent", userAgent); if (!string.IsNullOrEmpty(APIVersion)) { request.Headers.Add("Omise-Version", APIVersion); } if (payload != null) { using (var ms = new MemoryStream()) { Serializer.JsonSerialize(ms, payload); var buffer = ms.ToArray(); var content = Encoding.UTF8.GetString(buffer, 0, buffer.Length); request.Content = new StringContent(content, Encoding.UTF8, "application/json"); } } // roundtrips the request try { var response = await Roundtripper.Roundtrip(request); var stream = await response.Content.ReadAsStreamAsync(); if (!response.IsSuccessStatusCode) { var error = Serializer.JsonDeserialize <ErrorResult>(stream); error.HttpStatusCode = response.StatusCode; throw new OmiseError(error, null); } var result = Serializer.JsonDeserialize <TResult>(stream); var model = result as ModelBase; if (model != null) { model.Requester = this; } return(result); } catch (HttpRequestException e) { throw new OmiseException("Error while making HTTP request", e); } }