Pomona.Common.RequestDispatcher.SendRequestAsync C# (CSharp) Method

SendRequestAsync() public method

public SendRequestAsync ( string uri, string httpMethod, object body, ISerializationContextProvider provider, RequestOptions options = null ) : Task
uri string
httpMethod string
body object
provider ISerializationContextProvider
options RequestOptions
return Task
        public async Task<object> SendRequestAsync(string uri,
                                                   string httpMethod,
                                                   object body,
                                                   ISerializationContextProvider provider,
                                                   RequestOptions options = null)
        {
            if (provider == null)
                throw new ArgumentNullException(nameof(provider));
            if (uri == null)
                throw new ArgumentNullException(nameof(uri));
            if (httpMethod == null)
                throw new ArgumentNullException(nameof(httpMethod));
            if (options == null)
                options = new RequestOptions();

            var innerBody = body;
            var proxyBody = body as IExtendedResourceProxy;
            if (proxyBody != null)
                innerBody = proxyBody.WrappedResource;

            // Figure out server side response type
            ExtendedResourceInfo responseExtendedTypeInfo;
            var responseType = options.ExpectedResponseType;
            var innerOptions = options;
            var innerResponseType = options.ExpectedResponseType;
            if (innerResponseType != null)
            {
                if (this.typeMapper.TryGetExtendedTypeInfo(innerResponseType, out responseExtendedTypeInfo))
                {
                    innerResponseType = responseExtendedTypeInfo.ServerType;
                    innerOptions = new RequestOptions(options) { ExpectedResponseType = innerResponseType };
                }
            }

            var innerResult = await SendRequestInnerAsync(uri, httpMethod, innerBody, provider, innerOptions);
            if (innerResponseType == null && proxyBody != null && innerResult != null)
            {
                // Special case: No response type specified, but response has same type as posted body,
                // and the posted body was of an extended type. In this case we will wrap the response
                // to the same type if possible.
                var proxyBodyInfo = proxyBody.UserTypeInfo;
                if (proxyBodyInfo.ServerType.IsInstanceOfType(innerResult))
                {
                    responseType = proxyBodyInfo.ExtendedType;
                    innerResponseType = proxyBodyInfo.ServerType;
                }
            }

            if (responseType != innerResponseType)
                return this.typeMapper.WrapResource(innerResult, innerResponseType, responseType);
            return innerResult;
        }