private async Task<TimelineResult> Send(TimelineRequest request)
        {
            var client = new HttpClient();

            var url = string.Format("{0}{1}", _apiRoot, request.EndPoint);

            HttpResponseMessage response = null;
            switch (request.Method)
            {
                case Methods.DELETE:
                    response = await client.DeleteAsync(url);
                    break;

                case Methods.POST:
                    var postBody = new StringContent(request.Body);
                    if (request.Headers != null && request.Headers.Count > 0)
                    {
                        foreach (var h in request.Headers)
                        {
                            postBody.Headers.Add(h.Key, h.Value);
                        }
                    }
                    postBody.Headers.ContentType.MediaType = "application/json";
                    response = await client.PostAsync(url, postBody);
                    break;

                case Methods.PUT:
                    var putBody = new StringContent(request.Body);
                    if (request.Headers != null && request.Headers.Count > 0)
                    {
                        foreach (var h in request.Headers)
                        {
                            putBody.Headers.Add(h.Key, h.Value);
                        }
                    }
                    putBody.Headers.ContentType.MediaType = "application/json";
                    response = await client.PutAsync(url, putBody);
                    break;
            }
            int code = (int)response.StatusCode;

            string errorMessage = null;
            if (ErrorCodes.ContainsKey(code)) errorMessage = ErrorCodes[code];
            var result = new TimelineResult(code, errorMessage);


#if DEBUG
            Debug.WriteLine(request.Body);
            Debug.WriteLine(code);
            Debug.WriteLine(result.ErrorDescription);

#endif

            return result;
        }
Exemple #2
0
        private async Task <TimelineResult> Send(TimelineRequest request)
        {
            var client = new HttpClient();

            var url = string.Format("{0}{1}", _apiRoot, request.EndPoint);

            HttpResponseMessage response = null;

            switch (request.Method)
            {
            case Methods.DELETE:
                response = await client.DeleteAsync(url);

                break;

            case Methods.POST:
                var postBody = new StringContent(request.Body);
                if (request.Headers != null && request.Headers.Count > 0)
                {
                    foreach (var h in request.Headers)
                    {
                        postBody.Headers.Add(h.Key, h.Value);
                    }
                }
                postBody.Headers.ContentType.MediaType = "application/json";
                response = await client.PostAsync(url, postBody);

                break;

            case Methods.PUT:
                var putBody = new StringContent(request.Body);
                if (request.Headers != null && request.Headers.Count > 0)
                {
                    foreach (var h in request.Headers)
                    {
                        putBody.Headers.Add(h.Key, h.Value);
                    }
                }
                putBody.Headers.ContentType.MediaType = "application/json";
                response = await client.PutAsync(url, putBody);

                break;
            }
            int code = (int)response.StatusCode;

            string errorMessage = null;

            if (ErrorCodes.ContainsKey(code))
            {
                errorMessage = ErrorCodes[code];
            }
            var result = new TimelineResult(code, errorMessage);


#if DEBUG
            Debug.WriteLine(request.Body);
            Debug.WriteLine(code);
            Debug.WriteLine(result.ErrorDescription);
#endif

            return(result);
        }