public Task <QueryByPostCodeResponse> GetRestaurantInfoByPostCode(string postCode) { var cuisineType = new Cuisinetype() { Id = 1, IsTopCuisine = false, Name = "Sushi", SeoName = "Sushi" }; var response = new QueryByPostCodeResponse() { MetaData = new MetaData() { ResultCount = 1, }, Restaurants = new Restaurant[] { new Restaurant() { Name = "My restaurant", Rating = new Rating() { Average = 1, Count = 1, StarRating = 1, }, CuisineTypes = new Cuisinetype[] { cuisineType }, }, new Restaurant() { Name = "Tunde restuarant", Rating = new Rating() { Average = 1, Count = 1, StarRating = 1, }, CuisineTypes = new Cuisinetype[] { cuisineType }, }, } }; return(Task.FromResult(response)); }
public async Task <QueryByPostCodeResponse> GetRestaurantInfoByPostCode(string postCode) { try { var restaurantInfo = new QueryByPostCodeResponse(); if (string.IsNullOrWhiteSpace(postCode)) { _logger.LogError("postCode is null {postCode}", postCode); throw new ArgumentNullException(nameof(postCode)); } var baseUrl = _configuration["JustEat:baseUrl"]; var key = _configuration["JustEat:authorization"]; var code = _configuration["JustEat:countryCode"]; var request = new HttpRequestMessage(HttpMethod.Get, $"{baseUrl}restaurants/bypostcode//{postCode}"); request.Headers.Add("Authorization", $"Bearer {key}"); request.Headers.Add("Accept-Tenant", code); var client = _clientFactory.CreateClient(); var stopWatch = Stopwatch.StartNew(); var response = await client.SendAsync(request); if (response.IsSuccessStatusCode) { string requestResponse = await response.Content.ReadAsStringAsync(); _logger.LogDebug($"ResponseTime from JustEat {stopWatch.Elapsed.TotalSeconds}"); _logger.LogDebug($"Successful response from JustEatApi {requestResponse}"); restaurantInfo = JsonConvert.DeserializeObject <QueryByPostCodeResponse>(requestResponse); return(restaurantInfo); } else { _logger.LogDebug("Failed response from JustEatApi", response.Content); return(null); } } catch (Exception ex) { _logger.LogError(ex, "Error occurred while trying getRestaurantInfo"); return(null); } }