Ejemplo n.º 1
0
        protected async Task <ExchangeResponse> GetResponse(ExchangeRequestBase request)
        {
            var relativeUrl = request.RequestUrl;
            var absoluteUri = new Uri(new Uri(API_ENDPOINT_URL), relativeUrl);

            var timestamp = (request.TimeStamp).ToString(System.Globalization.CultureInfo.InvariantCulture);
            var body      = request.RequestBody;
            var method    = request.Method;
            var url       = absoluteUri.ToString();

            var passphrase = _authContainer.Passphrase;
            var apiKey     = _authContainer.ApiKey;

            // Caution: Use the relative URL, *NOT* the absolute one.
            var signature = _authContainer.ComputeSignature(timestamp, relativeUrl, method, body);

            using (var httpClient = new HttpClient())
            {
                HttpResponseMessage response;

                httpClient.DefaultRequestHeaders.Add("CB-ACCESS-KEY", apiKey);
                httpClient.DefaultRequestHeaders.Add("CB-ACCESS-SIGN", signature);
                httpClient.DefaultRequestHeaders.Add("CB-ACCESS-TIMESTAMP", timestamp);
                httpClient.DefaultRequestHeaders.Add("CB-ACCESS-PASSPHRASE", passphrase);

                httpClient.DefaultRequestHeaders.Add("User-Agent", "sefbkn.github.io");

                switch (method)
                {
                case "GET":
                    response = await httpClient.GetAsync(absoluteUri);

                    break;

                case "POST":
                    var requestBody = new StringContent(body, Encoding.UTF8, "application/json");
                    response = await httpClient.PostAsync(absoluteUri, requestBody);

                    break;

                default:
                    throw new NotImplementedException("The supplied HTTP method is not supported: " + method ?? "(null)");
                }


                var contentBody = await response.Content.ReadAsStringAsync();

                var headers    = response.Headers.AsEnumerable();
                var statusCode = response.StatusCode;
                var isSuccess  = response.IsSuccessStatusCode;

                var genericExchangeResponse = new ExchangeResponse(statusCode, isSuccess, headers, contentBody);
                return(genericExchangeResponse);
            }
        }
Ejemplo n.º 2
0
        protected ExchangePageableResponseBase(ExchangeResponse response)
        {
            var beforeHeader = response.Headers.LastOrDefault(x => x.Key != null && x.Key.ToUpper() == "CB-BEFORE");
            var afterHeader  = response.Headers.LastOrDefault(x => x.Key != null && x.Key.ToUpper() == "CB-AFTER");

            if (beforeHeader.Value != null)
            {
                BeforePaginationToken = beforeHeader.Value.LastOrDefault();
            }
            if (afterHeader.Value != null)
            {
                AfterPaginationToken = afterHeader.Value.LastOrDefault();
            }
        }
Ejemplo n.º 3
0
 protected ExchangeResponseBase(ExchangeResponse response)
 {
 }