Exemple #1
0
        /* ***** */

        internal async Task <TResponse> PostAsync <TResponse>(string path, SerializableContent request) where TResponse : ResponseBase, new()
        {
            SetCredentials(request);

            string endpoint    = GetEndpoint(path);
            string requestData = request.ToJson();

            HttpContent body = new StringContent(requestData, Encoding.UTF8, "application/json");

            body.Headers.Add("Plaid-Version", _apiVersion);

            WriteToDebugger(requestData, $"POST: '{endpoint}'");
            _logger?.LogTrace("Sent http request. POST: {0}\r\n{1}", endpoint, requestData);

            HttpClient http = _httpClientFactory.CreateClient();

            using (HttpResponseMessage response = await http.PostAsync(endpoint, body))
            {
#if DEBUG
                requestData = await response.Content.ReadAsStringAsync();

                WriteToDebugger(requestData, $"RESPONSE ({response.StatusCode})");
#endif
                _logger?.LogTrace("Received response ({1}) from 'POST: {0}'", endpoint, (int)response.StatusCode);

                return(await CreateResponse <TResponse>(response));
            }
        }
Exemple #2
0
        internal async Task <TResponse> PostAsync <TResponse>(string path, SerializableContent request) where TResponse : ResponseBase
        {
            EnsureCredentials(request);

            using (var http = new HttpClient())
            {
                string url  = GetEndpoint(path);
                string json = request.ToJson();
                Log(json, $"POST: '{url}'");

                var body = Body(json);
                body.Headers.Add("Plaid-Version", this._apiVersion);
                using (HttpResponseMessage response = await http.PostAsync(url, Body(json)))
                {
                    json = await response.Content.ReadAsStringAsync();

                    Log(json, $"RESPONSE ({response.StatusCode})");
                    TResponse result = JsonConvert.DeserializeObject <TResponse>(json);
                    result.StatusCode = response.StatusCode;

                    if (response.IsSuccessStatusCode == false)
                    {
                        var error = JObject.Parse(json);
                        result.Exception = new Exceptions.PlaidException(error["error_message"].Value <string>())
                        {
                            HelpLink       = "https://plaid.com/docs/api/#errors-overview",
                            DisplayMessage = error["display_message"].Value <string>(),
                            ErrorType      = error["error_type"].Value <string>(),
                            ErrorCode      = error["error_code"].Value <string>(),
                            Source         = url,
                        };
                    }
#if DEBUG
                    result.RawJsonForDebugging = json;
#endif
                    return(result);
                }
            }
        }