예제 #1
0
        /// <summary>
        /// Method used to send a request to the configured Manage API.
        /// </summary>
        /// <param name="request">CWRequest object. Can be pre-build with various included modules or can be constructed manually.</param>
        /// <param name="cts">Cancellation Token Source if you would like to be able to cancel mid-request.</param>
        /// <returns>A generic CWResponse object with no deserialization.</returns>
        public async Task <CWResponse> SendAsync(CWRequest request, CancellationTokenSource cts = null)
        {
            if (cts == null)
            {
                return(await SendAsync(request, cancelToken : default).ConfigureAwait(false));
            }

            return(await SendAsync(request, cancelToken : cts.Token).ConfigureAwait(false));
        }
예제 #2
0
        private HttpRequestMessage buildRequest(CWRequest request)
        {
            // Build Request
            var httpRequest = new HttpRequestMessage
            {
                RequestUri = new Uri($"{domain}/{Info.Codebase}/apis/{version}/{request.Endpoint}"),
            };

            // Content
            if (request.Content != null)
            {
                httpRequest.Content = request.Content;
            }

            // Headers
            httpRequest.Headers.Clear();
            httpRequest.Headers.TryAddWithoutValidation("Accept", accept);
            httpRequest.Headers.TryAddWithoutValidation("clientId", clientId);
            httpRequest.Headers.Authorization = auth;
            if (!string.IsNullOrWhiteSpace(cookieValue))
            {
                httpRequest.Headers.TryAddWithoutValidation("Cookie", string.Concat("cw-app-id=", cookieValue));
            }

            // Method
            switch (request.Method)
            {
            case CWHttpMethod.Post:
                httpRequest.Method = HttpMethod.Post;
                break;

            case CWHttpMethod.Put:
                httpRequest.Method = HttpMethod.Put;
                break;

            case CWHttpMethod.Patch:
                httpRequest.Method = new HttpMethod("PATCH");
                break;

            case CWHttpMethod.Delete:
                httpRequest.Method = HttpMethod.Delete;
                break;

            default:
                httpRequest.Method = HttpMethod.Get;
                break;
            }

            return(httpRequest);
        }
예제 #3
0
        /// <summary>
        /// Method used to send a request to the configured Manage API.
        /// </summary>
        /// <param name="request">CWRequest object. Can be pre-build with various included modules or can be constructed manually.</param>
        /// <param name="cts">Cancellation Token if you would like to be able to cancel mid-request.</param>
        /// <returns>A generic CWResponse object with no deserialization.</returns>
        public async Task <CWResponse> SendAsync(CWRequest request, CancellationTokenSource cts = null)
        {
            // Check if we have Company Info
            if (Info == null)
            {
                // Need Company Info
                var infoResponse = await getCompanyInfoAsync(cts);

                if (!infoResponse.IsSuccessful)
                {
                    return(infoResponse);
                }
            }

            // Build Request
            var httpRequest = buildRequest(request);

            // Make Request
            HttpResponseMessage response = null;

            try
            {
                if (cts != null)
                {
                    response = await client.SendAsync(httpRequest, cts.Token);
                }
                else
                {
                    response = await client.SendAsync(httpRequest);
                }
            }
            catch (OperationCanceledException)
            {
                return(new CWResponse("Request cancelled."));
            }
            catch (Exception e)
            {
                return(new CWResponse(e.ToString()));
            }

            // Check Response
            if (response != null)
            {
                return(new CWResponse(response, await response.Content.ReadAsStringAsync()));
            }
            return(new CWResponse("There was an error making the request to the CW Manage API."));
        }
예제 #4
0
        /// <summary>
        /// Method used to send a request to the configured Manage API.
        /// </summary>
        /// <typeparam name="T">The deserialization type. Must be a class.</typeparam>
        /// <param name="request">CWRequest object. Can be pre-build with various included modules or can be constructed manually.</param>
        /// <param name="cancelToken">Cancellation Token if you would like to be able to cancel mid-request.</param>
        /// <returns>A CWResponse object that will attempt deserialization to the specified type.</returns>
        public async Task <CWResponse <T> > SendAsync <T>(CWRequest request, CancellationToken cancelToken = default)
            where T : class
        {
            // Check if we have Company Info
            if (Info == null)
            {
                // Need Company Info
                var infoResponse = await getCompanyInfoAsync(cancelToken).ConfigureAwait(false);

                if (!infoResponse.IsSuccessful)
                {
                    return(new CWResponse <T>(infoResponse.Response, infoResponse.Result, false));
                }
            }

            // Build Request
            var httpRequest = buildRequest(request);

            // Make Request
            HttpResponseMessage response = null;

            try
            {
                response = await client.SendAsync(httpRequest, cancelToken).ConfigureAwait(false);
            }
            catch (OperationCanceledException)
            {
                return(new CWResponse <T>("Request cancelled."));
            }
            catch (Exception e)
            {
                return(new CWResponse <T>(e.ToString()));
            }

            // Check Response
            if (response != null)
            {
                var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                return(new CWResponse <T>(response, content));
            }
            return(new CWResponse <T>("There was an error making the request to the CW Manage API."));
        }