Skip to content

Instantly share code, notes, and snippets.

@JoachimL
JoachimL / XunitContextTest.cs
Created Jan 20, 2021
PoC'ing the use of shared state in Xunit tests
View XunitContextTest.cs
using System;
using System.Threading;
using Xunit;
namespace Xunit.ContextPoC
{
public class UnitTest1 : IClassFixture<Context>
{
public UnitTest1(Context ctx)
{
@JoachimL
JoachimL / SendGrid.csx
Created Mar 25, 2017
Sendgrid through azure function
View SendGrid.csx
#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 Mar 21, 2017
HTTP triggered function
View HttpTriggeredFunction.csx
#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 / AddProductToCartAsync.csx
Created Mar 19, 2017
Add product to kolonial cart
View AddProductToCartAsync.csx
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();
}
View GetKolonialProductAsync.csx
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;
View CreateKolonialHttpClientAsync.csx
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);
View incoming-barcode-run.csx
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);
@JoachimL
JoachimL / barcodereader.py
Last active Mar 22, 2021
Python USB barcode reader
View barcodereader.py
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 / gist:46e90f29f477d3de83ad
Last active Aug 29, 2015
Usage before IoC/Feature Toggle
View gist:46e90f29f477d3de83ad
foreach(var relatedPage in SlowPageRelationRepository.GetPagesRelatedTo(this.PageId, "en"")
{
// ...
}
View gist:5cc150e42294e741fea2
public static class SlowPageRelationRepository
{
private static readonly SlowPageDatabaseHandler PageDatabaseHandler = new SlowPageDatabaseHandler();
public static ICollection<Page> GetPagesRelatedTo(int page, string language)
{
return PageDatabaseHandler.GetRelatedPages(page, language);
}
public static CategorizedPageCollection GetCategorizedPagesRelatedTo(int page, string language)