Last active
March 21, 2017 20:23
-
-
Save JoachimL/dffac22c265e018e5f62471ff35222bf to your computer and use it in GitHub Desktop.
Revisions
-
JoachimL revised this gist
Mar 21, 2017 . 1 changed file with 6 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -27,12 +27,12 @@ public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceW string barcode = GetBarcodeFromRequest(req); // Request product from kolonial var httpResult = await HttpClient.GetAsync("https://kolonial.no/api/v1/search/?q=" + barcode); if (!httpResult.IsSuccessStatusCode) { // not much we can do here, so just log the error and return an error status code. log.Error($"Error occured when getting data from kolonial."); return req.CreateErrorResponse(HttpStatusCode.InternalServerError, "Product not found"); } else @@ -42,6 +42,9 @@ public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceW { log.Info($"Processing JSON: {json}"); var results = JsonConvert.DeserializeObject<KolonialSearchResponse>(json); // If the search call returns anything but a single product // we have no idea how to handle it. if(results.Products != null && results.Products.Length == 1) { var product = results.Products.First(); -
JoachimL revised this gist
Mar 21, 2017 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,4 @@ #r "Newtonsoft.Json" using System; using System.Configuration; using Newtonsoft.Json; -
JoachimL created this gist
Mar 21, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,71 @@ #r "Microsoft.WindowsAzure.Storage" #r "Newtonsoft.Json" using Microsoft.WindowsAzure.Storage.Table; using System; using System.Configuration; using Newtonsoft.Json; using System.Net; // These are the (interesting parts) of the models returned from the kolonial API. // The actual JSON returned contains more properties, but I see no reason to bother the deserializer // with more than what we actually need. public class KolonialSearchResponse { public KolonialProduct[] Products {get;set;} } public class KolonialProduct { public string Id {get;set;} public string Barcode { get; set; } public string Brand {get; set;} public string Name {get; set;} } static HttpClient HttpClient = null; public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log) { if(HttpClient == null) HttpClient = CreateHttpClient(log); string barcode = GetBarcodeFromRequest(req); // Get request body var httpResult = await HttpClient.GetAsync("https://kolonial.no/api/v1/search/?q=" + barcode); if (!httpResult.IsSuccessStatusCode) { log.Error($"Error occured when getting data from kolonial."); return req.CreateErrorResponse(HttpStatusCode.InternalServerError, "Product not found"); } else { var json = await httpResult.Content.ReadAsStringAsync(); if (json != null) { log.Info($"Processing JSON: {json}"); var results = JsonConvert.DeserializeObject<KolonialSearchResponse>(json); if(results.Products != null && results.Products.Length == 1) { var product = results.Products.First(); product.Barcode = barcode; return req.CreateResponse(HttpStatusCode.OK, product); } } return req.CreateErrorResponse(HttpStatusCode.BadRequest, "Product not found"); } } public static HttpClient CreateHttpClient(TraceWriter log) { log.Info("Instantiating HTTP client."); var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Add("X-Client-Token", ConfigurationManager.AppSettings["KolonialToken"]); httpClient.DefaultRequestHeaders.Add("User-Agent", ConfigurationManager.AppSettings["KolonialUserAgent"]); return httpClient; } public static string GetBarcodeFromRequest(HttpRequestMessage req){ return req.GetQueryNameValuePairs() .FirstOrDefault(q => string.Compare(q.Key, "barcode", true) == 0) .Value; }