public async void SendQueryAsyncShouldPreserveUriParametersFact() { var endpoint = new Uri("http://localhost/api/graphql?code=my-secret-api-key"); var handlerStub = new HttpHandlerStub(); GraphQLHttpClientOptions options = new GraphQLHttpClientOptions() { EndPoint = endpoint, HttpMessageHandler = handlerStub }; GraphQLHttpClient systemUnderTest = new GraphQLHttpClient(options); var response = await systemUnderTest.SendQueryAsync(new GraphQLRequest { Query = @" { person(personID: ""1"") { name } }" }); var actualRequestUri = handlerStub.LastRequest.RequestUri; var queryParams = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(actualRequestUri.Query); Assert.True(queryParams.ContainsKey("code"), "Expected code query parameter to be preserved"); Assert.Equal("my-secret-api-key", queryParams["code"]); }
public override HttpRequestMessage ToHttpRequestMessage(GraphQLHttpClientOptions options, GraphQL.Client.Abstractions.IGraphQLJsonSerializer serializer) { var result = base.ToHttpRequestMessage(options, serializer); if (_credentials.UseToken) { result.Headers.Add(HeaderKeys.XAmzSecurityTokenHeader, _credentials.Token); } var signingRequest = new AmazonServiceRequest(result, _clientConfig) { Content = result.Content.ReadAsByteArrayAsync().Result, }; Console.WriteLine(result.Content.ReadAsStringAsync().Result); new AWS4Signer().Sign(signingRequest, _clientConfig, null, _credentials.AccessKey, _credentials.SecretKey); foreach (var header in signingRequest.Headers) { if (header.Key != HeaderKeys.HostHeader && header.Key != HeaderKeys.XAmzSecurityTokenHeader && header.Key != HeaderKeys.UserAgentHeader) { result.Headers.TryAddWithoutValidation(header.Key, header.Value); } } return(result); }
public GraphQLHttpHandler(GraphQLHttpClientOptions options) { this.Options = options ?? throw new ArgumentNullException(nameof(options)); if (options.EndPoint == null) { throw new ArgumentNullException(nameof(options.EndPoint)); } if (options.JsonSerializerSettings == null) { throw new ArgumentNullException(nameof(options.JsonSerializerSettings)); } if (options.HttpMessageHandler == null) { throw new ArgumentNullException(nameof(options.HttpMessageHandler)); } if (options.MediaType == null) { throw new ArgumentNullException(nameof(options.MediaType)); } if (IsDefaultHandler(this.Options.HttpMessageHandler as HttpClientHandler)) { this.HttpClient = new HttpClient(); } else { this.HttpClient = new HttpClient(this.Options.HttpMessageHandler); } }
//--- Methods --- public override HttpRequestMessage ToHttpRequestMessage(GraphQLHttpClientOptions options, Client.Abstractions.IGraphQLJsonSerializer serializer) { var result = base.ToHttpRequestMessage(options, serializer); result.Headers.Add("X-Api-Key", _authorization); return(result); }
/// <inheritdoc/> protected override async Task InitializeAsync(CancellationToken cancellationToken) { await base.InitializeAsync(cancellationToken); await this.HttpClient.ConfigureAuthorizationAsync(this.ServiceProvider, this.Authentication, cancellationToken); var operationComponents = this.Function.Operation.Split('#', StringSplitOptions.RemoveEmptyEntries); if (operationComponents.Length != 3) { throw new FormatException($"The 'operation' property of the GraphQL function with name '{this.Function.Name}' has an invalid value '{this.Function.Operation}'. GraphQL functions expect a value in the following format: <url_to_graphql_endpoint>#<literal 'mutation' or 'query'>#<mutation_or_query_field>"); } if (!Uri.TryCreate(operationComponents[0], new(), out var endpointUri) || endpointUri == null) { throw new FormatException($"The value specified as GraphQL endpoint operation component '{operationComponents[0]}' is not a valid uri"); } this.EndpointUri = endpointUri; this.OperationType = operationComponents[1].ToLower(); if (this.OperationType != "query" && this.OperationType != "mutation") { throw new FormatException($"The value specified as GraphQL request type component '{operationComponents[1]}' is not supported"); } this.OperationName = operationComponents[2]; await this.BuildArgumentsAsync(cancellationToken); var options = new GraphQLHttpClientOptions() { EndPoint = this.EndpointUri }; this.GraphQLClient = new GraphQLHttpClient(options, this.Serializer, this.HttpClient); }
public static void Configure(IConfiguration configuration, IServiceCollection serviceCollection) { serviceCollection.AddOptions <AniListOptions>().Bind(configuration.GetSection(AniListOptions.AniList)); serviceCollection.AddHttpClient(Constants.NAME).AddHttpMessageHandler(provider => { var rlLogger = provider.GetRequiredService <ILogger <HeaderBasedRateLimitMessageHandler> >(); var rl = new HeaderBasedRateLimitMessageHandler(rlLogger); return(rl); }); serviceCollection.AddSingleton <AniListClient>(provider => { var httpClientFactory = provider.GetRequiredService <IHttpClientFactory>(); var httpClient = httpClientFactory.CreateClient(Constants.NAME); httpClient.Timeout = TimeSpan.FromSeconds(200); var logger = provider.GetRequiredService <ILogger <AniListClient> >(); var options = new GraphQLHttpClientOptions() { EndPoint = new Uri(Wrapper.Constants.BASE_URL) }; var gqlc = new GraphQLHttpClient(options, new SystemTextJsonSerializer(new JsonSerializerOptions() { Converters = { new JsonStringEnumConverter(new JsonUpperPolicyCase()) } }), httpClient); return(new(gqlc, logger)); }); serviceCollection.AddSingleton <IExecuteOnStartupService, AniListExecuteOnStartupService>(); serviceCollection.AddSingleton <IUserFeaturesService <AniListUserFeatures>, AniListUserFeaturesService>(); serviceCollection.AddSingleton <AniListUserService>(); serviceCollection.AddSingleton <IUpdateProvider, AniListUpdateProvider>(); }
static async Task Main(string[] args) { var options = new GraphQLHttpClientOptions { EndPoint = new Uri("http://echo.somee.com/netflixgraph/graphql"), }; var client = new GraphQLHttpClient(options, new NewtonsoftJsonSerializer()); var query = "{ actionShows { casts } }"; var request = new GraphQLHttpRequest { Query = query }; var graphQLResponse = await client.SendQueryAsync <object>(request); var stringResult = graphQLResponse.Data.ToString(); //stringResult = stringResult.Replace($"\"actionShows\":", string.Empty); //stringResult = stringResult.Remove(0, 1); //stringResult = stringResult.Remove(stringResult.Length - 1, 1); var result = JsonConvert.DeserializeObject <Data>(stringResult.ToString()); for (int i = 0; i < result.ActionShows.Length; i++) { Console.WriteLine(result.ActionShows[i].Casts); } Console.Read(); }
static async Task Main(string[] args) { var options = new GraphQLHttpClientOptions { UseWebSocketForQueriesAndMutations = false, EndPoint = new System.Uri("https://*****:*****@" query northwindQuery { customers { customerId, companyName, contactName } }" }; var response = await client.SendQueryAsync <CustomerResponse>(request); var x = response.Data.Customers.FirstOrDefault(); }
public Client(HttpClient httpClient) { var options = new GraphQLHttpClientOptions { EndPoint = new Uri("https://graphql.anilist.co") }; _graphQlClient = new GraphQLHttpClient(options, new SystemTextJsonSerializer(), httpClient); }
public AppSyncService() { var options = new GraphQLHttpClientOptions { EndPoint = new Uri(ApiKey.AppSyncApiUrl), }; this.GraphQLHttpClient = new GraphQLHttpClient(options, new NewtonsoftJsonSerializer()); this.GraphQLHttpClient.HttpClient.DefaultRequestHeaders.Add("x-api-key", ApiKey.AppSyncApiKey); }
public void Initialize() { GraphQLHttpClientOptions options = new GraphQLHttpClientOptions() { EndPoint = new System.Uri("http://www.example.com/graph/api/graphql"), HttpMessageHandler = new MockHttpHandler() }; _client = new GraphQLHttpClient(options); }
public void Initialize() { var options = new GraphQLHttpClientOptions() { EndPoint = new Uri("http://www.example.com/graph/api/graphql"), HttpMessageHandler = new MockHttpHandler() }; _client = new GraphQLHttpClient(options, new NewtonsoftJsonSerializer()); }
public PolicyExampleGraphQLClient(HttpClient httpClient, string?endpoint = null) { var options = new GraphQLHttpClientOptions { EndPoint = new Uri(endpoint ?? "https://localhost:5001/graphql") }; Client = new GraphQLHttpClient(options, new NewtonsoftJsonSerializer(), httpClient); }
private IGraphQLClient CreateGraphQLClient(string configurationKeyUrlLiveOrEditMode) { GraphQLHttpClientOptions graphQLHttpClientOptions = new GraphQLHttpClientOptions() { EndPoint = new Uri( $"{_configuration.GetValue<string>(configurationKeyUrlLiveOrEditMode)}?sc_apikey={_configuration.GetValue<string>("Sitecore:ApiKey")}"), }; return(new GraphQLHttpClient(graphQLHttpClientOptions, new NewtonsoftJsonSerializer(), _httpClient)); }
public GraphQLHttpClient CreateGraphQLClient() { var httpClient = CreateClient(); var clientOptions = new GraphQLHttpClientOptions { EndPoint = new Uri(httpClient.BaseAddress, "api/graphql"), }; return(new GraphQLHttpClient(clientOptions, new NewtonsoftJsonSerializer(), httpClient)); }
public GraphQLHttpClient GetUnauthenticatedGraphQLClient() { var client = _factory.CreateClient(); var clientOptions = new GraphQLHttpClientOptions { HttpMessageHandler = GetHandler(), EndPoint = new Uri("http://localhost/graphql") }; return(client.AsGraphQLClient(clientOptions)); }
static Query() { var uri = new Uri("https://api.spacex.land/graphql"); var graphQLOptions = new GraphQLHttpClientOptions { EndPoint = uri, HttpMessageHandler = new NativeMessageHandler(), }; graphQLHttpClient = new GraphQLHttpClient(graphQLOptions, new NewtonsoftJsonSerializer()); }
public GraphQLHttpClient GetGraphQlApiClient() { var endpoint = "https://userdemographql.hasura.app/v1/graphql"; var httpClientOption = new GraphQLHttpClientOptions { EndPoint = new Uri(endpoint) }; return(new GraphQLHttpClient(httpClientOption, new NewtonsoftJsonSerializer(), new HttpClient())); }
static Mutation() { var uri = new Uri("https://localhost:44303/graphql"); var graphQLOptions = new GraphQLHttpClientOptions { EndPoint = uri, HttpMessageHandler = new NativeMessageHandler(), }; graphQLHttpClient = new GraphQLHttpClient(graphQLOptions, new NewtonsoftJsonSerializer()); }
public async Task <GraphQLHttpClient> GetAuthenticatedGraphQLClient(string userName = "******", string password = "******") { var client = await GetAuthenticatedClient(userName, password); var clientOptions = new GraphQLHttpClientOptions { HttpMessageHandler = GetHandler(), EndPoint = new Uri("http://localhost/graphql") }; return(client.AsGraphQLClient(clientOptions)); }
static void Main(string[] args) { GraphQLHttpClient graphQLHttpClient; var httpClientOption = new GraphQLHttpClientOptions { EndPoint = new Uri("https://moviesg.azurewebsites.net/api") }; graphQLHttpClient = new GraphQLHttpClient(httpClientOption, new NewtonsoftJsonSerializer()); GetAllMovies(graphQLHttpClient).Wait(); }
static GraphQLHttpClient CreateGraphQLClient() { var options = new GraphQLHttpClientOptions { EndPoint = new Uri(BackendConstants.GraphQLApiUrl), #if !DEBUG HttpMessageHandler = new ModernHttpClient.NativeMessageHandler() #endif }; return(new GraphQLHttpClient(options, new NewtonsoftJsonSerializer())); }
public GraphQLDataFetcher(string accessToken) { // Setup the HTTP client to always pass the authorization header, as per the API documentation. HttpClient http = new HttpClient(); http.DefaultRequestHeaders.Add("Authorization", $"bearer {accessToken}"); GraphQLHttpClientOptions graphql = new GraphQLHttpClientOptions() { EndPoint = new Uri("https://api.github.com/graphql"), }; _client = new GraphQLHttpClient(graphql, new SystemTextJsonSerializer(), http); }
public TibberService( HttpClient client, IGraphQLJsonSerializer graphQLJsonSerializer, IOptions <TibberOptions> options ) { _client = client; _options = options.Value; _graphQLHttpClientOptions = new GraphQLHttpClientOptions { EndPoint = new Uri(_options.BaseUrl) }; _graphQLJsonSerializer = graphQLJsonSerializer; }
private static GraphQLHttpClient SetupClient() { var testHttpClient = SetupTestHostAndClient(); var options = new GraphQLHttpClientOptions { EndPoint = new Uri("https://localhost:5001/graphql") }; var client = new GraphQLHttpClient(options, new NewtonsoftJsonSerializer(), testHttpClient); return(client); }
static GraphQLHttpClient CreateGitHubGraphQLClient() { var graphQLOptions = new GraphQLHttpClientOptions { EndPoint = new Uri(GitHubConstants.GraphQLApiUrl), HttpMessageHandler = new NativeMessageHandler(), }; var client = new GraphQLHttpClient(graphQLOptions, new NewtonsoftJsonSerializer()); client.HttpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(new ProductHeaderValue(nameof(SimpleXamarinGraphQL)))); client.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", GitHubConstants.PersonalAccessToken); return(client); }
public static void AddUniswap(this IServiceCollection services) { services.AddSingleton <IGraphQLClient>(ctx => { var graphQLOptions = new GraphQLHttpClientOptions { EndPoint = new Uri("https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2") }; return(new GraphQLHttpClient(graphQLOptions, new SystemTextJsonSerializer())); }); services.AddSingleton <IUniswap>(ctx => { IGraphQLClient graphQLClient = ctx.GetRequiredService <IGraphQLClient>(); return(new Uniswap(graphQLClient)); }); }
public GraphQLHttpClient GetGraphQlApiClient() { var endpoint = "https://testproject1.hasura.app/v1/graphql"; var httpClientOption = new GraphQLHttpClientOptions { EndPoint = new Uri(endpoint) }; var httpClient = new HttpClient(); var defaultHeader = httpClient.DefaultRequestHeaders; defaultHeader.Add("X-Hasura-Role", "tenantuser"); defaultHeader.Add("X-Hasura-Tenant-Id", "zyxv-abcd"); return(new GraphQLHttpClient(httpClientOption, new NewtonsoftJsonSerializer(), httpClient)); }
private async static Task LerTodos() { var req = new GraphQLRequest { Query = @"{clientes { id, nome }}", OperationName = null, Variables = null }; var graphQLOptions = new GraphQLHttpClientOptions { EndPoint = new Uri("http://localhost:49152/graphql/", UriKind.Absolute), }; var client = new GraphQLHttpClient(graphQLOptions, new NewtonsoftJsonSerializer()); var graphQLResponse = await client.SendQueryAsync <dynamic>(req); Console.WriteLine(graphQLResponse.Data); Console.ReadKey(); }
public async static Task <GraphQLHttpClientOptions> ConfigureAppSync( this GraphQLHttpClientOptions options, string graphQlEndpoint, IClientConfig clientConfig ) { var appSyncHost = new Uri(graphQlEndpoint).Host.Split('.'); // set GraphQL endpoint options.EndPoint = new Uri(graphQlEndpoint); ImmutableCredentials tempCredentials = await FallbackCredentialsFactory.GetCredentials().GetCredentialsAsync(); //set pre-processor to add authentication HTTP header to request options.PreprocessRequest = (request, client) => { return(Task.FromResult((GraphQLHttpRequest) new AuthorizedAppSyncHttpRequest(request, clientConfig, tempCredentials))); }; return(options); }