/// <summary> /// Combines searching for products and details for the products /// returned from the search. /// </summary> /// <param name="request"></param> /// <returns></returns> public async Task <TCGProductResponse> Search(TCGSearchRequest request) { LambdaLogger.Log($"Entering: Search({ JsonConvert.SerializeObject(request) })"); TCGProductResponse response = new TCGProductResponse { success = false, errors = new List <string> { { "Default Error: Not set from TCG responses." } }, results = new List <TCGProduct>() }; try { TCGSearchResponse searchResponse = await SearchCategoryProducts(request); if (searchResponse == null) { throw new Exception("TCG Search response was null..."); } if (searchResponse.results.Count == 0) { throw new Exception("TCG Search response contains no results..."); } TCGProductRequest productRequest = new TCGProductRequest { productIds = searchResponse.results }; response = await GetProductDetails(productRequest); } catch (Exception exp) { LambdaLogger.Log($"Error: {exp}"); throw; } LambdaLogger.Log($"Leaving: Search({ JsonConvert.SerializeObject(response) })"); return(response); }
/// <summary> /// A wrapper function for TCG's search API. /// - Will return list of card details instead of ids based on search criteria /// </summary> /// <param name="request"></param> /// <param name="context"></param> /// <returns></returns> public async Task <APIGatewayProxyResponse> Search(APIGatewayProxyRequest request, ILambdaContext context) { LambdaLogger.Log($"Entering: Search({JsonConvert.SerializeObject(request)})"); APIGatewayProxyResponse response = new APIGatewayProxyResponse { StatusCode = (int)HttpStatusCode.OK, Headers = new Dictionary <string, string> { { "Content-Type", "application/json" } } }; try { try { TCGSearchRequest searchRequest = JsonConvert.DeserializeObject <TCGSearchRequest>(request.Body); if (searchRequest == null) { throw new Exception($"Request Body sucks...look at this: {request.Body} ...does that look right to you?"); } LambdaLogger.Log($"Search Response: { JsonConvert.SerializeObject(searchRequest) }"); //TODO: Come up with response //- TCGResponse will contain ids for cards //- Need to use TCGResponse ids to retrieve actual card information //- Return list of this card information TCGProductResponse searchResponse = await new TCGService().Search(searchRequest); LambdaLogger.Log($"Search Response: { JsonConvert.SerializeObject(searchResponse) }"); if (searchResponse == null) { throw new Exception($"TCG Response is null...WTF, like why?"); } response.Body = JsonConvert.SerializeObject(searchResponse); } catch (JsonSerializationException jsonExp) { LambdaLogger.Log($"Deserialization Exception: {jsonExp}"); throw new Exception($"Request Body stinks...look at this: {request.Body} ...does that look right to you?"); } } catch (Exception exp) { LambdaLogger.Log($"Exception: {exp}"); Dictionary <string, string> body = new Dictionary <string, string>() { { "Message", "rly dude/lady?lady/dude? y u breaking things?" }, { "Error1", "It's probably your fault" }, { "Error2", $"It might be this -> { exp.Message }" } }; response = new APIGatewayProxyResponse { StatusCode = (int)HttpStatusCode.InternalServerError, Headers = new Dictionary <string, string> { { "Content-Type", "application/json" } }, Body = JsonConvert.SerializeObject(body) }; } LambdaLogger.Log($"Leaving: Search({ JsonConvert.SerializeObject(response) })"); return(response); }
/// <summary> /// Gets card details from given productIds /// </summary> /// <param name="request"></param> /// returns => results: List of card details public async Task <TCGProductResponse> GetProductDetails(TCGProductRequest request) { LambdaLogger.Log($"Entering: GetProductDetails({ JsonConvert.SerializeObject(request) })"); TCGProductResponse response = null; try { //Need to validate the product request here as well!!! if (request == null) { throw new Exception("Product Details request is null..."); } if (request.productIds.Count == 0) { throw new Exception("Product Details request contains no product ids."); } string url = string.Format(TCGConstants.GET_PRODUCT_URL_TEMPLATE, string.Join(',', request.productIds)); using (HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Get, url)) { httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await GetToken()); LambdaLogger.Log($"Http Request: {JsonConvert.SerializeObject(httpRequest)}"); HttpResponseMessage httpResponse = await httpClient.SendAsync(httpRequest); LambdaLogger.Log($"Http Response: {JsonConvert.SerializeObject(httpResponse)}"); if (httpResponse != null) { if (httpResponse.IsSuccessStatusCode) { string httpResponseContent = await httpResponse.Content.ReadAsStringAsync(); LambdaLogger.Log($"Http Response Content: {httpResponseContent}"); response = JsonConvert.DeserializeObject <TCGProductResponse>(httpResponseContent); LambdaLogger.Log($"SearchCategoryProducts Response: {JsonConvert.SerializeObject(response)}"); if (!response.success) { throw new Exception($"TCG Error(s): { string.Join(" - ", response.errors) }"); } } } } } catch (Exception exp) { LambdaLogger.Log($"Error: {exp}"); throw; } LambdaLogger.Log($"Leaving: GetProductDetails({ JsonConvert.SerializeObject(request) })"); return(response); }