Skip to content

Instantly share code, notes, and snippets.

View JoachimL's full-sized avatar

Joachim Løvf JoachimL

View GitHub Profile
@JoachimL
JoachimL / barcodereader.py
Last active November 8, 2022 07:33
Python USB barcode reader
import evdev
from evdev import *
from azure.storage.queue import QueueService, QueueMessageFormat
import threading
import time
from queue import *
import datetime
# responsible for uploading the barcodes to the azure storage queue.
class BarcodeUploader:
@JoachimL
JoachimL / XunitContextTest.cs
Created January 20, 2021 11:32
PoC'ing the use of shared state in Xunit tests
using System;
using System.Threading;
using Xunit;
namespace Xunit.ContextPoC
{
public class UnitTest1 : IClassFixture<Context>
{
public UnitTest1(Context ctx)
{
@JoachimL
JoachimL / SendGrid.csx
Created March 25, 2017 10:00
Sendgrid through azure function
#r "SendGrid"
using System.Net;
using SendGrid.Helpers.Mail;
public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log, out Mail message)
{
log.Info("C# HTTP trigger function processed a request.");
// parse query parameter
string name = req.GetQueryNameValuePairs()
@JoachimL
JoachimL / HttpTriggeredFunction.csx
Last active March 21, 2017 20:23
HTTP triggered function
#r "Newtonsoft.Json"
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 {
@JoachimL
JoachimL / CreateKolonialHttpClientAsync.csx
Last active March 19, 2017 07:02
Set up of kolonial client
public static async Task<HttpClient> CreateKolonialHttpClientAsync(){
var httpClient = new HttpClient();
// The kolonial API requires a client token and a user agent (supplied by kolonial) to work
httpClient.DefaultRequestHeaders.Add("X-Client-Token", ConfigurationManager.AppSettings["KolonialToken"]);
httpClient.DefaultRequestHeaders.Add("User-Agent", ConfigurationManager.AppSettings["KolonialUserAgent"]);
// Modifying the cart requires a valid, active session
string sessionId = await GetSessionIdAsync(httpClient);
@JoachimL
JoachimL / AddProductToCartAsync.csx
Created March 19, 2017 06:54
Add product to kolonial cart
public static async Task AddProductToCartAsync(HttpClient httpClient, KolonialProduct kolonialProduct, TraceWriter log)
{
var productsJson = JsonConvert.SerializeObject(
new { items = new []{ new { product_id = kolonialProduct.Id, quantity = 1 } }});
log.Info($"Updating Kolonial with {productsJson}");
var response = await httpClient.PostAsync(
"https://kolonial.no/api/v1/cart/items/",
new StringContent(productsJson, Encoding.UTF8, "application/json"));
response.EnsureSuccessStatusCode();
}
@JoachimL
JoachimL / GetKolonialProductAsync.csx
Created March 19, 2017 06:51
Get kolonial product
public static async Task<KolonialProduct> GetKolonialProductAsync(HttpClient client, string barcode)
{
var httpResult = await client.GetAsync(ConfigurationManager.AppSettings["GetKolonialProductUri"] + "&barcode=" + barcode);
if (httpResult.IsSuccessStatusCode)
{
var json = await httpResult.Content.ReadAsStringAsync();
if (json != null)
return JsonConvert.DeserializeObject<KolonialProduct>(json);
}
return null;
@JoachimL
JoachimL / incoming-barcode-run.csx
Created March 18, 2017 07:14
Main entry point
public static async Task Run(string message, TraceWriter log)
{
log.Info($"Processing incoming barcode: {message}");
var incoming = IncomingBarcode.FromMessage(message);
var httpClient = await CreateKolonialHttpClientAsync();
var kolonialProduct = await GetKolonialProductAsync(httpClient, incoming.Barcode);
if(kolonialProduct == null)
log.Warning($"Product with barcode {incoming.Barcode} is not available at Kolonial.");
else
await AddProductToCartAsync (httpClient, kolonialProduct, log);
public class Movie
{
public int Id { get; set; }
public string Title { get; set; }
public ICollection<Actor> Actors { get;set; }
}
public class MovieModel
{
public string Title { get; set; }
namespace WebApi.OData.Models
{
public class MoviePresentationModel
{
public int Id { get; set; }
public string Title { get; set; }
public int YearOfRelease { get; set; }
public double AverageRating { get; set; }
public string[] Actors { get; set; }
}