/// <summary> /// Searches for cards based on given filters /// </summary> /// <param name="request"></param> /// returns => results: productIds public async Task <TCGSearchResponse> SearchCategoryProducts(TCGSearchRequest request) { LambdaLogger.Log($"Entering: SearchCategoryProducts({ JsonConvert.SerializeObject(request) })"); TCGSearchResponse response = null; //Need to validate TCGSearchRequest try { string url = $"{TCGConstants.SEARCH_URL}"; //StringContent content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json"); var httpRequest = new HttpRequest { Url = url, Headers = new Dictionary <string, string> { { "Content-Type", "application/json" }, { "Authorization", $"Bearer {await GetToken()}" } }, Content = JsonConvert.SerializeObject(request) }; LambdaLogger.Log($"Http Request: {JsonConvert.SerializeObject(httpRequest)}"); var httpResponse = HttpHelper.Post(httpRequest); LambdaLogger.Log($"Http Response: {JsonConvert.SerializeObject(httpResponse)}"); if (httpResponse != null) { if (httpResponse.IsSuccessStatusCode) { string httpResponseContent = httpResponse.Content; LambdaLogger.Log($"Http Response Content: {httpResponseContent}"); response = JsonConvert.DeserializeObject <TCGSearchResponse>(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: SearchCategoryProducts({ JsonConvert.SerializeObject(response) })"); return(response); }
/// <summary> /// Searches for cards based on given filters /// </summary> /// <param name="request"></param> /// returns => results: productIds public async Task <TCGSearchResponse> SearchCategoryProducts(TCGSearchRequest request) { LambdaLogger.Log($"Entering: SearchCategoryProducts({ JsonConvert.SerializeObject(request) })"); TCGSearchResponse response = null; //Need to validate TCGSearchRequest try { string url = $"{TCGConstants.SEARCH_URL}"; StringContent content = new StringContent(JsonConvert.SerializeObject(request)); content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); using (HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Post, url)) { httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await GetToken()); httpRequest.Content = content; 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 <TCGSearchResponse>(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: SearchCategoryProducts({ JsonConvert.SerializeObject(response) })"); return(response); }
/// <summary> /// Includes market price and productId on the card /// </summary> /// <param name="importList"></param> public async void AddTCGDetails(Card importCard) { LambdaLogger.Log($"Entering: AddTCGDetails({ JsonConvert.SerializeObject(importCard) })"); try { var name = importCard.Name; var filter = new TCGSearchFilter { name = "ProductName", values = new List <string>() { name } }; var request = new TCGSearchRequest { filters = new List <TCGSearchFilter>() { filter }, limit = 10, offset = 0 }; var searchResponse = await Search(request); LambdaLogger.Log($"TCG Search Response: { JsonConvert.SerializeObject(searchResponse) }"); if (searchResponse.success) { var responseCard = searchResponse.results .Where(x => x.name == name) .FirstOrDefault(); if (responseCard != null) { importCard.TCGGroupId = responseCard.groupId; importCard.TCGProductId = responseCard.productId; } } else { LambdaLogger.Log($"TCG Search Response failure: { JsonConvert.SerializeObject(searchResponse) }"); } } catch (Exception exp) { LambdaLogger.Log($"Error: {exp}"); } LambdaLogger.Log($"Leaving: AddTCGDetails({ JsonConvert.SerializeObject(importCard) })"); }
/// <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); }