Skip to content

Instantly share code, notes, and snippets.

@JoachimL
Last active March 21, 2017 20:23
Show Gist options
  • Select an option

  • Save JoachimL/dffac22c265e018e5f62471ff35222bf to your computer and use it in GitHub Desktop.

Select an option

Save JoachimL/dffac22c265e018e5f62471ff35222bf to your computer and use it in GitHub Desktop.

Revisions

  1. JoachimL revised this gist Mar 21, 2017. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions HttpTriggeredFunction.csx
    Original 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);

    // Get request body
    // Request product from kolonial
    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.");
    // 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();
  2. JoachimL revised this gist Mar 21, 2017. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions HttpTriggeredFunction.csx
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,4 @@
    #r "Microsoft.WindowsAzure.Storage"
    #r "Newtonsoft.Json"
    using Microsoft.WindowsAzure.Storage.Table;
    using System;
    using System.Configuration;
    using Newtonsoft.Json;
  3. JoachimL created this gist Mar 21, 2017.
    71 changes: 71 additions & 0 deletions HttpTriggeredFunction.csx
    Original 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;
    }