Пример #1
0
        /// <summary>
        /// Send a <see cref="HttpMethod"/> <b>PATCH</b> <paramref name="json"/> request as an asynchronous operation.
        /// </summary>
        /// <param name="urlSuffix">The url suffix for the operation.</param>
        /// <param name="patchOption">The <see cref="WebApiPatchOption"/>.</param>
        /// <param name="json">The json value.</param>
        /// <param name="requestOptions">The optional <see cref="WebApiRequestOptions"/>.</param>
        /// <param name="args">The operation arguments to be substituted within the <paramref name="urlSuffix"/>.</param>
        /// <returns>The <see cref="WebApiAgentResult{TResult}"/>.</returns>
        public async Task <WebApiAgentResult> PatchAsync(string?urlSuffix, WebApiPatchOption patchOption, JToken json, WebApiRequestOptions?requestOptions = null, WebApiArg[]?args = null)
        {
            if (json == null)
            {
                throw new ArgumentNullException(nameof(json));
            }

            if (patchOption == WebApiPatchOption.NotSpecified)
            {
                throw new ArgumentException("A valid patch option must be specified.", nameof(patchOption));
            }

            var uri = CreateFullUri(urlSuffix, args, requestOptions);

            if (args != null && args.Any(x => x.ArgType == WebApiArgType.FromBody))
            {
                throw new ArgumentException("No arguments can be marked as IsFromBody for a PATCH.", nameof(args));
            }

            return(await WebApiAgentInvoker.Current.InvokeAsync(this, async() =>
            {
                var content = new StringContent(json.ToString());
                content.Headers.ContentType = MediaTypeHeaderValue.Parse(patchOption == WebApiPatchOption.JsonPatch ? "application/json-patch+json" : "application/merge-patch+json");
                var result = new WebApiAgentResult(await Args.HttpClient.SendAsync(await CreateRequestMessageAsync(new HttpMethod("PATCH"), uri, content, requestOptions).ConfigureAwait(false)).ConfigureAwait(false));
                result.Content = await result.Response.Content.ReadAsStringAsync().ConfigureAwait(false);
                return VerifyResult(result);
            }, json).ConfigureAwait(false));
        }
Пример #2
0
        /// <summary>
        /// Send a <see cref="HttpMethod.Delete"/> request as an asynchronous operation.
        /// </summary>
        /// <param name="urlSuffix">The url suffix for the operation.</param>
        /// <param name="requestOptions">The optional <see cref="WebApiRequestOptions"/>.</param>
        /// <param name="args">The operation arguments to be substituted within the <paramref name="urlSuffix"/>.</param>
        /// <returns>The <see cref="WebApiAgentResult{T}"/>.</returns>
        public async Task <WebApiAgentResult> DeleteAsync(string?urlSuffix, WebApiRequestOptions?requestOptions = null, WebApiArg[]?args = null)
        {
            var uri = CreateFullUri(urlSuffix, args, requestOptions);

            return(await WebApiAgentInvoker.Current.InvokeAsync(this, async() =>
            {
                var result = new WebApiAgentResult(await Args.HttpClient.SendAsync(await CreateRequestMessageAsync(HttpMethod.Delete, uri, requestOptions: requestOptions).ConfigureAwait(false)).ConfigureAwait(false));
                result.Content = await result.Response.Content.ReadAsStringAsync().ConfigureAwait(false);
                return VerifyResult(result);
            }, null !).ConfigureAwait(false));
        }
Пример #3
0
        /// <summary>
        /// Send a <see cref="HttpMethod.Get"/> request as an asynchronous operation with an expected response.
        /// </summary>
        /// <typeparam name="TResult">The result <see cref="Type"/>.</typeparam>
        /// <param name="urlSuffix">The url suffix for the operation.</param>
        /// <param name="requestOptions">The optional <see cref="WebApiRequestOptions"/>.</param>
        /// <param name="args">The operation arguments to be substituted within the <paramref name="urlSuffix"/>.</param>
        /// <returns>The <see cref="WebApiAgentResult{TResult}"/>.</returns>
        public async Task <WebApiAgentResult <TResult> > GetAsync <TResult>(string?urlSuffix, WebApiRequestOptions?requestOptions = null, WebApiArg[]?args = null)
        {
            var uri = CreateFullUri(urlSuffix, args, requestOptions);

            return(await WebApiAgentInvoker.Current.InvokeAsync(this, async() =>
            {
                var value = args?.Where(x => x.ArgType == WebApiArgType.FromBody).SingleOrDefault()?.GetValue();
                var result = new WebApiAgentResult(await Args.HttpClient.SendAsync(await CreateRequestMessageAsync(HttpMethod.Get, uri, CreateJsonContentFromValue(value), requestOptions).ConfigureAwait(false)).ConfigureAwait(false));
                result.Content = await result.Response.Content.ReadAsStringAsync().ConfigureAwait(false);
                return new WebApiAgentResult <TResult>(VerifyResult(result));
            }, null !).ConfigureAwait(false));
        }
Пример #4
0
        /// <summary>
        /// Send a <see cref="HttpMethod.Post"/> <paramref name="value"/> request as an asynchronous operation with an expected response.
        /// </summary>
        /// <typeparam name="TResult">The result <see cref="Type"/>.</typeparam>
        /// <param name="urlSuffix">The url suffix for the operation.</param>
        /// <param name="value">The content value.</param>
        /// <param name="requestOptions">The optional <see cref="WebApiRequestOptions"/>.</param>
        /// <param name="args">The operation arguments to be substituted within the <paramref name="urlSuffix"/>.</param>
        /// <returns>The <see cref="WebApiAgentResult{TResult}"/>.</returns>
        public async Task <WebApiAgentResult <TResult> > PostAsync <TResult>(string?urlSuffix, object value, WebApiRequestOptions?requestOptions = null, WebApiArg[]?args = null)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            var uri = CreateFullUri(urlSuffix, args, requestOptions);

            if (args != null && args.Any(x => x.ArgType == WebApiArgType.FromBody))
            {
                throw new ArgumentException("No arguments can be marked as IsFromBody where a content value is used.", nameof(args));
            }

            return(await WebApiAgentInvoker.Current.InvokeAsync(this, async() =>
            {
                var result = new WebApiAgentResult(await Args.HttpClient.SendAsync(await CreateRequestMessageAsync(HttpMethod.Post, uri, CreateJsonContentFromValue(value), requestOptions).ConfigureAwait(false)).ConfigureAwait(false));
                result.Content = await result.Response.Content.ReadAsStringAsync().ConfigureAwait(false);
                return new WebApiAgentResult <TResult>(VerifyResult(result));
            }, value).ConfigureAwait(false));
        }