コード例 #1
0
        public async Task Get <TResultContent>(string baseUrl
                                               , string contextUrl
                                               , Action <TResultContent> success
                                               , Action <RestException <TResultContent> > error
                                               , HeaderAccept headerAccept = null
                                               )
        {
            Uri requestUri = new Uri(new Uri(baseUrl), contextUrl);

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();

                var message = await CreateRequestMessage(HttpMethod.Get, headerAccept, requestUri);

                var restResponse = await GetResponse <TResultContent>(client, message);

                if (restResponse.IsSuccess)
                {
                    success(restResponse.Result);
                }
                else
                {
                    error(new RestException <TResultContent>(restResponse));
                }
            }
        }
コード例 #2
0
        public async Task <TResultContent> Delete <TPostContent, TResultContent>(string baseUrl, string contextUrl, TPostContent content,
                                                                                 HeaderAccept headerAccept = null)
        {
            HttpMethod method = HttpMethod.Delete;

            return(await Execute <TPostContent, TResultContent>(baseUrl, contextUrl, content, method, headerAccept));
        }
コード例 #3
0
        private async Task <TResult> Execute <TPost, TResult>(string baseUrl, string contextUrl, TPost content, HttpMethod method,
                                                              HeaderAccept headerAccept = null)
        {
            Uri requestUri = new Uri(new Uri(baseUrl), contextUrl);

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();


                var message = await CreateRequestMessage(method, headerAccept, requestUri);

                CreatePostContent(content, message);

                var restResponse = await GetResponse <TResult>(client, message);

                if (restResponse.IsSuccess)
                {
                    return(restResponse.Result);
                }
                else
                {
                    throw new RestException <TResult>(restResponse);
                }
            }
        }
コード例 #4
0
        public Task Get <TResultContent>(
            string baseUrl,
            string contextUrl,
            object identifier,
            Action <TResultContent> success,
            Action <RestException <TResultContent> > error,
            HeaderAccept headerAccept = null)
        {
            string request = contextUrl;

            if (identifier != null)
            {
                string idPart = identifier.ToString();
                request = Path.Combine(request, idPart);
            }

            return(Get <TResultContent>(baseUrl, request, success, error, headerAccept));
        }
コード例 #5
0
        private async Task <HttpRequestMessage> CreateRequestMessage(HttpMethod method, HeaderAccept headerAccept,
                                                                     Uri requestUri)
        {
            var    message   = new HttpRequestMessage(method, requestUri);
            string authtoken = await GetAuthToken();

            if (!string.IsNullOrEmpty(authtoken))
            {
                message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authtoken);
            }

            if (headerAccept != null && headerAccept != HeaderAccept.None)
            {
                message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(headerAccept.Type));
            }

            return(message);
        }