Skip to content

Instantly share code, notes, and snippets.

@mykwillis
mykwillis / direqt-fetch.js
Created May 26, 2020 02:53
Direqt Fetch for Twilio Functions
var axios = require('axios'); // Add 'axios' to Dependencies at https://www.twilio.com/console/functions/configure
exports.handler = function (context, event, callback) {
delete context[AUTH_TOKEN];
axios
.post(
'https://webhooks.direqt.io/twilio',
{ context, event },
{ params: { apiKey: context.DIREQT_API_KEY || event.DIREQT_API_KEY } }
)
.then(function (response) {
@mykwillis
mykwillis / object-diff.js
Created December 13, 2018 20:08
See what state changed in React componentDidUpdate
componentDidUpdate(_: *, prevState: PlayActionState) {
const diff = Object.keys(this.state).reduce((d, key) => {
if (prevState[key] === this.state[key]) return d;
return {
...d,
[key]: this.state[key],
};
}, {});
console.log(diff);
}
@mykwillis
mykwillis / test_thing.py
Last active April 4, 2018 10:58 — forked from jamielennox/test_thing.py
requests_mock and pytest
import thing
import pytest
import requests_mock as rm_module
@pytest.fixture
def requests_mock(request):
with rm_module.Mocker() as m:
yield m
@mykwillis
mykwillis / index.html
Created November 12, 2015 16:40
HTML drag and drop with Angular
<!doctype html>
<html>
<head>
<script type="text/javascript" a
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script>
<style type="text/css">
.dropTarget {
width: 300px;
height: 100px;
border: 1px solid green;
@mykwillis
mykwillis / redshift-table-sizes.sql
Created September 30, 2015 14:48
Get the size on disk of Redshift tables
select trim(pgdb.datname) as Database,
trim(a.name) as Table, ((b.mbytes/part.total::decimal)*100)::decimal(5,2) as pct_of_total, b.mbytes, b.unsorted_mbytes
from stv_tbl_perm a
join pg_database as pgdb on pgdb.oid = a.db_id
join (select tbl, sum(decode(unsorted, 1, 1, 0)) as unsorted_mbytes, count(*) as mbytes
from stv_blocklist group by tbl) b on a.id=b.tbl
join ( select sum(capacity) as total
from stv_partitions where part_begin=0 ) as part on 1=1
where a.slice=0
order by 3 desc, db_id, name;
@mykwillis
mykwillis / HybridPrimaryKeyRelatedField.py
Created September 21, 2015 13:45
Allow a Django Rest Framework field to be serialized out as a hyperlink, but in as a primary key
class HybridPrimaryKeyRelatedField(serializers.HyperlinkedRelatedField):
"""Serializes out as hyperlink, in as primary key"""
def to_internal_value(self, data):
return self.get_queryset().get(pk=data)
class BreedSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Breed
@mykwillis
mykwillis / django-stormpath.backends.py
Created August 28, 2015 23:02
A Django authenticator that supports Stormpath API keys being used in BASIC HTTP Authentication
import logging
from django.contrib.auth import get_user_model
from stormpath.api_auth import ApiRequestAuthenticator
import base64
from stormpath.error import Error
from django_stormpath.backends import StormpathBackend
log = logging.getLogger(__name__)
def get_application():
/**
* Simple Expressjs application demonstrating difference in routing behavior
* between version 4.12.4 and 4.13.1.
*/
var express = require('express');
var request = require('request');
var http_server = express()
.get('/:foo\\(:bar?\\)', function(req, res, next) {
res.end(JSON.stringify({ 'foo': req.params.foo, 'bar': req.params.bar }));
}).listen(8080, function() {
@mykwillis
mykwillis / import_csv.sql
Last active August 29, 2015 14:18
Populate MSSQL table with CSV
BEGIN TRANSACTION;
TRUNCATE TABLE MultiListingCampaignMappings;
BULK INSERT MultiListingCampaignMappings
FROM 'c:\users\Administrator\multimappings.csv'
WITH
(
FIELDTERMINATOR = ',', --CSV field delimiter
ROWTERMINATOR = '\n' --Use to shift the control to next row