private void SetupAdapterMock(Mock <IDataAdapter <TestEntity> > adapterMock, TestEntity[] entities = null) { adapterMock.Setup(a => a.GetEntitiesAsync( It.IsAny <string[]>(), It.IsAny <Dictionary <string, AggregateQuery> >(), It.IsAny <string[]>(), It.IsAny <bool>(), It.IsAny <DateTime?>(), It.IsAny <CancellationToken>())) .ReturnsAsync((string[] identifiers, Dictionary <string, AggregateQuery> aggregateQueries, string[] fields, bool live, DateTime? pointInTime, CancellationToken cancellationToken) => { var results = new DataAdapterResult <TestEntity> [identifiers.Length]; for (var i = 0; i < identifiers.Length; i++) { var entity = entities != null && i < entities.Length ? entities[i] : MakeTestEntity(); results[i] = new DataAdapterResult <TestEntity> { Identifier = identifiers[i], Entity = entity, }; } return(results); }); }
public async Task <DataAdapterResult> Read(IDataAdapter dataAdapter, dynamic parameters) { var adapterMetadata = JsonConvert .DeserializeObject <GraphQLDataAdapterMetadata>( dataAdapter.Metadata); var authToken = await GetAuthenticationTokenAsync(adapterMetadata); var graphClient = new GraphQLClient(dataAdapter.Endpoint); graphClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); graphClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken); var request = new GraphQLRequest { Query = adapterMetadata.Query, Variables = adapterMetadata.Variables }; var response = await graphClient.PostAsync(request); var dataResults = response.Data as JToken; var data = dataResults.ToValueDictionary(); var result = new DataAdapterResult { Rows = new[] { new DataAdapterResultRow { Fields = data } } }; return(result); }
protected async Task <DataAdapterResult <T>[]> GetEntitiesFromApi <T>( string[] identifiers, Dictionary <string, AggregateQuery> aggregateQueries, string[] fields, bool live, DateTime?pointInTime, CancellationToken cancellationToken) { var bearerToken = _executionContextManager.SpiExecutionContext.IdentityToken; var entityType = GetPluralUrlEntityName <T>(); if (string.IsNullOrEmpty(entityType)) { throw new Exception($"Unsupported entity type {typeof(T)}"); } using (var client = new HttpClient()) { client.BaseAddress = new Uri(_baseUrl, UriKind.Absolute); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken); client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _subscriptionKey); client.DefaultRequestHeaders.Add(SpiHeaderNames.InternalRequestIdHeaderName, _executionContextManager.SpiExecutionContext.InternalRequestId.ToString()); if (!string.IsNullOrEmpty(_executionContextManager.SpiExecutionContext.ExternalRequestId)) { client.DefaultRequestHeaders.Add(SpiHeaderNames.ExternalRequestIdHeaderName, _executionContextManager.SpiExecutionContext.ExternalRequestId); } var body = JsonConvert.SerializeObject( new SpiAdapterBatchRequest { Identifiers = identifiers, AggregateQueries = aggregateQueries, Fields = fields, Live = live, PointInTime = pointInTime, }, SerializerSettings); _logger.Debug($"Calling {SourceName} adapter at {_baseUrl} with {body}"); var response = await client.PostAsync( entityType, new StringContent(body, Encoding.UTF8, "application/json"), cancellationToken); if (!response.IsSuccessStatusCode) { HttpErrorBody errorBody = null; try { var errorJson = await response.Content.ReadAsStringAsync(); errorBody = JsonConvert.DeserializeObject <HttpErrorBody>(errorJson); } catch (Exception ex) { var fullUrl = new Uri(new Uri(_baseUrl, UriKind.Absolute), new Uri(entityType, UriKind.Relative)); _logger.Warning($"Error reading standard error response from {(int)response.StatusCode} response from {fullUrl}: {ex.Message}"); } throw new DataAdapterException($"Error calling {SourceName} adapter") { AdapterName = SourceName, RequestedEntityName = entityType, RequestedIds = identifiers, RequestedFields = fields, HttpStatusCode = response.StatusCode, HttpErrorBody = errorBody, }; } var json = await response.Content.ReadAsStringAsync(); var entities = JsonConvert.DeserializeObject <T[]>(json); var results = new DataAdapterResult <T> [identifiers.Length]; for (var i = 0; i < identifiers.Length; i++) { results[i] = new DataAdapterResult <T> { Identifier = identifiers[i], Entity = entities[i], }; } return(results); } }