示例#1
0
        /// <summary>
        /// Executes the query and returns a responseEntity of type passed </summary>
        /// <param name="url"> </param>
        /// <param name="auth"> </param>
        /// <param name="query"> </param>
        /// <param name="Variables"> </param>
        /// <param name="tClass">
        /// @return </param>
        public async Task <GraphQLResponse <dynamic> > GraphqlExecute(string url, string query, IDictionary <string, object> variables, GraphQLOptions opts)
        {
            GraphQLHttpClient graphClient = new GraphQLHttpClient(url, new NewtonsoftJsonSerializer());

            graphClient.HttpClient.DefaultRequestHeaders.Add("User-Agent", this.userAgent);
            graphClient.HttpClient.DefaultRequestHeaders.Add("Authorization", await this.GetAuthorizationHeader(null));
            GraphQLRequestCamel request = new GraphQLRequestCamel(query, variables);

            try
            {
                return(await graphClient.SendQueryAsync <dynamic>(request));
            }
            catch (GraphQLHttpRequestException e)
            {
                if (opts == null)
                {
                    opts = defaultGraphQLOptions;
                }
                int retry = opts.Retry;
                // handle errors explicitly
                // extract the status from the error response
                // if 401 and retry > 0 then we can retry
                if (e.StatusCode == HttpStatusCode.Unauthorized && retry > 0)
                {
                    // unauthenticated request might be because token expired
                    // clear token and retry
                    this.ClearToken();
                    opts.Retry -= 1;
                    return(await this.GraphqlExecute(url, query, variables, opts));
                }

                // otherwise rethrow
                throw new TraceSdkException(e.Content);
            }
        }
示例#2
0
        /// <summary>
        /// Executes the query and returns a responseEntity of type passed </summary>
        /// <param name="url"> </param>
        /// <param name="auth"> </param>
        /// <param name="query"> </param>
        /// <param name="Variables"> </param>
        /// <param name="tClass">
        /// @return </param>
        public async Task <GraphQLResponse> GraphqlExecute(string url, string auth, string query, IDictionary <string, object> variables, Type tClass)
        {
            GraphQLClientOptions clientOptions = new GraphQLClientOptions();
            HttpMessageHandler   handler       = CreateHttpMessageHandler();

            if (handler != null)
            {
                clientOptions.HttpMessageHandler = handler;
            }
            clientOptions.JsonSerializerSettings = new JsonSerializerSettings()
            {
                ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver()
            };


            GraphQLClient graphClient = new GraphQLClient(url, clientOptions);

            graphClient.DefaultRequestHeaders.Add("Authorization", auth);
            var request = new GraphQLRequestCamel(query, variables);

            return(await graphClient.PostAsync(request));
        }