コード例 #1
0
ファイル: HttpServiceClient.cs プロジェクト: t9mike/Mitten
        private static HttpRequestMessage CreateMultipartPostRequest(
            string uri,
            byte[] file,
            string fileName,
            string securityToken,
            params RequestParameter[] parameters)
        {
            Throw.IfArgumentNull(file, "file");
            Throw.IfArgumentNullOrWhitespace(fileName, "fileName");

            HttpRequestMessage request = HttpServiceClient.GetRequestTemplate(HttpMethod.Post, uri, securityToken);

            string boundary = HttpServiceClient.CreateBoundary();
            MultipartFormDataContent multipartContent = new MultipartFormDataContent(boundary);

            StreamContent streamConent = HttpServiceClient.CreateFileContent(file, fileName);

            multipartContent.Add(streamConent);

            if (parameters != null)
            {
                foreach (RequestParameter parameter in parameters)
                {
                    if (!string.IsNullOrWhiteSpace(parameter.Value))
                    {
                        multipartContent.Add(new StringContent(parameter.Value), parameter.ParameterName);
                    }
                }
            }

            request.Content = multipartContent;

            return(request);
        }
コード例 #2
0
ファイル: HttpServiceClient.cs プロジェクト: t9mike/Mitten
        /// <summary>
        /// Makes a POST request.
        /// </summary>
        /// <param name="uri">The request URI.</param>
        /// <param name="securityToken">A security token used by the server to validate the client, or null if making the call anonymously.</param>
        /// <param name="parameters">The parameters to pass as part of a form.</param>
        /// <returns>The result of the POST request.</returns>
        protected Task <ServiceResult> PostAsync(string uri, string securityToken, params RequestParameter[] parameters)
        {
            this.ThrowIfDisposed();

            HttpRequestMessage request = HttpServiceClient.CreateFormPostRequest(uri, securityToken, parameters);

            return(this.MakeServiceRequest(request));
        }
コード例 #3
0
ファイル: HttpServiceClient.cs プロジェクト: t9mike/Mitten
        /// <summary>
        /// Makes a DELETE request.
        /// </summary>
        /// <param name="uri">The request URI.</param>
        /// <param name="securityToken">A security token used by the server to validate the client, or null if making the call anonymously.</param>
        /// <returns>The result of the DELETE request.</returns>
        protected Task <ServiceResult> DeleteAsync(string uri, string securityToken)
        {
            this.ThrowIfDisposed();

            HttpRequestMessage request = HttpServiceClient.GetRequestTemplate(HttpMethod.Delete, uri, securityToken);

            return(this.MakeServiceRequest(request));
        }
コード例 #4
0
ファイル: HttpServiceClient.cs プロジェクト: t9mike/Mitten
        private HttpRequestMessage CreateJsonRequest(HttpMethod method, string uri, string securityToken, string json)
        {
            HttpRequestMessage request = HttpServiceClient.GetRequestTemplate(method, uri, securityToken);

            request.Content = new StringContent(json);
            request.Content.Headers.ContentType = new MediaTypeHeaderValue(Constants.ApplicationJsonContentType);

            return(request);
        }
コード例 #5
0
ファイル: HttpServiceClient.cs プロジェクト: t9mike/Mitten
        private static HttpRequestMessage CreateFormPostRequest(string uri, string securityToken, params RequestParameter[] parameters)
        {
            HttpRequestMessage request = HttpServiceClient.GetRequestTemplate(HttpMethod.Post, uri, securityToken);
            IEnumerable <KeyValuePair <string, string> > items = parameters.Select(parameter => new KeyValuePair <string, string>(parameter.ParameterName, parameter.Value));

            request.Content = new FormUrlEncodedContent(items);

            return(request);
        }
コード例 #6
0
ファイル: HttpServiceClient.cs プロジェクト: t9mike/Mitten
        /// <summary>
        /// Makes a GET request to the service for a resource returned as an array of bytes.
        /// </summary>
        /// <param name="uri">The request URI.</param>
        /// <param name="securityToken">A security token used by the server to validate the client, or null if making the call anonymously.</param>
        /// <param name="parameters">Optional parameters to pass as part of the query string for the URI.</param>
        /// <returns>The result of the GET request.</returns>
        protected Task <ServiceResult <byte[]> > GetResourceAsync(string uri, string securityToken, params RequestParameter[] parameters)
        {
            this.ThrowIfDisposed();

            string             fullUri = uri + HttpServiceClient.BuildQuery(parameters);
            HttpRequestMessage request = HttpServiceClient.CreateGetRequest(fullUri, securityToken);

            return(this.MakeServiceRequestWithBinaryResponse(request));
        }
コード例 #7
0
ファイル: HttpServiceClient.cs プロジェクト: t9mike/Mitten
 private Task <ServiceResult <byte[]> > MakeServiceRequestWithBinaryResponse(HttpRequestMessage request)
 {
     return
         (this.MakeServiceRequest <ServiceResult <byte[]> >(
              request,
              async goodResponse => ServiceResult <byte[]> .Success(await HttpServiceClient.GetResponseAsBytes(goodResponse).ConfigureAwait(false)),
              badResponse => ServiceResult <byte[]> .Failed(HttpServiceClient.GetServiceResultCode(badResponse), HttpServiceClient.GetFailureDetails(badResponse)),
              () => ServiceResult <byte[]> .Failed(ServiceResultCode.NetworkUnavailable, null),
              ServiceResult <byte[]> .Failed));
 }
コード例 #8
0
ファイル: HttpServiceClient.cs プロジェクト: t9mike/Mitten
 private Task <ServiceResult <TResponse> > MakeServiceRequestWithJsonResponse <TResponse, TDTO>(HttpRequestMessage request, JsonSerializerSettings serializerSettings, Func <TDTO, TResponse> convertFromDTO)
 {
     return
         (this.MakeServiceRequest <ServiceResult <TResponse> >(
              request,
              async goodResponse => ServiceResult <TResponse> .Success(await HttpServiceClient.GetResponseFromJson <TResponse, TDTO>(goodResponse, serializerSettings, convertFromDTO).ConfigureAwait(false)),
              badResponse => ServiceResult <TResponse> .Failed(HttpServiceClient.GetServiceResultCode(badResponse), HttpServiceClient.GetFailureDetails(badResponse)),
              () => ServiceResult <TResponse> .Failed(ServiceResultCode.NetworkUnavailable, null),
              ServiceResult <TResponse> .Failed));
 }
コード例 #9
0
ファイル: HttpServiceClient.cs プロジェクト: t9mike/Mitten
 private Task <ServiceResult> MakeServiceRequest(HttpRequestMessage request)
 {
     return
         (this.MakeServiceRequest <ServiceResult>(
              request,
              goodResponse => Task.FromResult(ServiceResult.Success()),
              badResponse => ServiceResult.Failed(HttpServiceClient.GetServiceResultCode(badResponse), HttpServiceClient.GetFailureDetails(badResponse)),
              () => ServiceResult.Failed(ServiceResultCode.NetworkUnavailable, null),
              ServiceResult.Failed));
 }
コード例 #10
0
ファイル: HttpServiceClient.cs プロジェクト: t9mike/Mitten
        /// <summary>
        /// Makes a POST request.
        /// </summary>
        /// <param name="uri">The request URI.</param>
        /// <param name="securityToken">A security token used by the server to validate the client, or null if making the call anonymously.</param>
        /// <param name="convertFromDTO">Converts a received DTO into the expected response type.</param>
        /// <param name="parameters">The parameters to pass as part of a form.</param>
        /// <returns>The result of the POST request.</returns>
        protected Task <ServiceResult <TResponse> > PostAsync <TResponse, TDTO>(
            string uri,
            string securityToken,
            Func <TDTO, TResponse> convertFromDTO,
            params RequestParameter[] parameters)
        {
            this.ThrowIfDisposed();

            HttpRequestMessage request = HttpServiceClient.CreateFormPostRequest(uri, securityToken, parameters);

            return(this.MakeServiceRequestWithJsonResponse <TResponse, TDTO>(request, HttpServiceClient.GetSerializerSettings(), convertFromDTO));
        }
コード例 #11
0
ファイル: HttpServiceClient.cs プロジェクト: t9mike/Mitten
        /// <summary>
        /// Makes a POST request to the service passing a file.
        /// </summary>
        /// <param name="uri">The request URI.</param>
        /// <param name="securityToken">A security token used by the server to validate the client, or null if making the call anonymously.</param>
        /// <param name="file">The file data to post.</param>
        /// <param name="fileName">The name of the file.</param>
        /// <returns>The result of the POST request along with the location for the file.</returns>
        protected Task <ServiceResult <string> > PostFileAsync(string uri, string securityToken, byte[] file, string fileName)
        {
            HttpRequestMessage request = HttpServiceClient.CreateMultipartPostRequest(uri, file, fileName, securityToken);

            return
                (this.MakeServiceRequest <ServiceResult <string> >(
                     request,
                     goodResponse => Task.FromResult(ServiceResult <string> .Success(HttpServiceClient.TryGetLocationFromResponseHeader(goodResponse))),
                     badResponse => ServiceResult <string> .Failed(HttpServiceClient.GetServiceResultCode(badResponse), HttpServiceClient.GetFailureDetails(badResponse)),
                     () => ServiceResult <string> .Failed(ServiceResultCode.NetworkUnavailable, null),
                     ServiceResult <string> .Failed));
        }
コード例 #12
0
ファイル: HttpServiceClient.cs プロジェクト: t9mike/Mitten
        /// <summary>
        /// Makes a GET request to the service with an expected Json response.
        /// </summary>
        /// <param name="uri">The request URI.</param>
        /// <param name="securityToken">A security token used by the server to validate the client, or null if making the call anonymously.</param>
        /// <param name="serializerSettings">Serializer settings used when deserializing the response content from the server.</param>
        /// <param name="convertFromDTO">Converts a received DTO into the expected response type.</param>
        /// <param name="parameters">Optional parameters to pass as part of the query string for the URI.</param>
        /// <returns>The result of the GET request.</returns>
        protected Task <ServiceResult <TResponse> > GetFromJsonAsync <TResponse, TDTO>(
            string uri,
            string securityToken,
            JsonSerializerSettings serializerSettings,
            Func <TDTO, TResponse> convertFromDTO,
            params RequestParameter[] parameters)
        {
            this.ThrowIfDisposed();

            string             fullUri = uri + HttpServiceClient.BuildQuery(parameters);
            HttpRequestMessage request = HttpServiceClient.CreateGetRequest(fullUri, securityToken);

            return(this.MakeServiceRequestWithJsonResponse <TResponse, TDTO>(request, serializerSettings, convertFromDTO));
        }
コード例 #13
0
ファイル: HttpServiceClient.cs プロジェクト: t9mike/Mitten
        private static HttpRequestMessage CreateGetRequest(string uri, string securityToken)
        {
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, uri);

            HttpServiceClient.SetUserAgent(request);
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));

            if (!string.IsNullOrWhiteSpace(securityToken))
            {
                request.Headers.Authorization = new AuthenticationHeaderValue(Constants.AuthenticationHeaderPrefix, securityToken);
            }

            return(request);
        }
コード例 #14
0
ファイル: HttpServiceClient.cs プロジェクト: t9mike/Mitten
 /// <summary>
 /// Makes a GET request to the service with an expected Json response.
 /// </summary>
 /// <param name="uri">The request URI.</param>
 /// <param name="securityToken">A security token used by the server to validate the client, or null if making the call anonymously.</param>
 /// <param name="convertFromDTO">Converts a received DTO into the expected response type.</param>
 /// <param name="parameters">Optional parameters to pass as part of the query string for the URI.</param>
 /// <returns>The result of the GET request.</returns>
 protected Task <ServiceResult <TResponse> > GetFromJsonAsync <TResponse, TDTO>(
     string uri,
     string securityToken,
     Func <TDTO, TResponse> convertFromDTO,
     params RequestParameter[] parameters)
 {
     return
         (this.GetFromJsonAsync <TResponse, TDTO>(
              uri,
              securityToken,
              HttpServiceClient.GetSerializerSettings(),
              convertFromDTO,
              parameters));
 }
コード例 #15
0
ファイル: HttpServiceClient.cs プロジェクト: t9mike/Mitten
        private static string GetFailureDetails(HttpResponseMessage response)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("Unsuccessful status code returned (" + (int)response.StatusCode + " - " + response.ReasonPhrase + ") for request (" + response.RequestMessage + ").");

            if (response.Content != null &&
                response.Content.Headers.ContentLength > 0 &&
                HttpServiceClient.IsTextContent(response.Content))
            {
                sb.AppendLine();
                sb.Append("Content: " + response.Content.ReadAsStringAsync().Result);
            }

            return(sb.ToString());
        }
コード例 #16
0
ファイル: HttpServiceClient.cs プロジェクト: t9mike/Mitten
        private static HttpRequestMessage GetRequestTemplate(HttpMethod httpMethod, string uri, string securityToken)
        {
            Throw.IfArgumentNull(httpMethod, nameof(httpMethod));
            Throw.IfArgumentNullOrWhitespace(uri, nameof(uri));

            HttpRequestMessage request = new HttpRequestMessage(httpMethod, uri);

            HttpServiceClient.SetUserAgent(request);
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));

            if (!string.IsNullOrWhiteSpace(securityToken))
            {
                request.Headers.Authorization = new AuthenticationHeaderValue(Constants.AuthenticationHeaderPrefix, securityToken);
            }

            return(request);
        }
コード例 #17
0
ファイル: HttpServiceClient.cs プロジェクト: t9mike/Mitten
 private Task <ServiceResult <TResponse> > MakeServiceRequestWithJsonResponse <TResponse>(HttpRequestMessage request)
 {
     return(this.MakeServiceRequestWithJsonResponse <TResponse>(request, HttpServiceClient.GetSerializerSettings()));
 }
コード例 #18
0
ファイル: HttpServiceClient.cs プロジェクト: t9mike/Mitten
 /// <summary>
 /// Serializes the specified object into a json string.
 /// </summary>
 /// <param name="value">The object to serialize.</param>
 /// <returns>A json string.</returns>
 protected string SerializeToJson(object value)
 {
     return(JsonConvert.SerializeObject(value, HttpServiceClient.GetSerializerSettings()));
 }