Skip to content

Instantly share code, notes, and snippets.

@milancermak
milancermak / app-session.ts
Last active May 13, 2021
Custom session storage for a Shopify app in SQL using Prisma
View app-session.ts
import { PrismaClient } from '@prisma/client'
import Shopify from '@shopify/shopify-api'
import { Session } from '@shopify/shopify-api/dist/auth/session';
const prisma = new PrismaClient({ log: ['info', 'warn', 'error'] })
async function storeCallback(session: Session): Promise<boolean> {
const payload: { [key: string]: any } = { ...session }
return prisma.appSession.upsert({
create: { id: session.id, payload: payload },
@milancermak
milancermak / ddbwriter_main.py
Created Mar 7, 2019
Experiments with DynamoDB and Connection HTTP headers
View ddbwriter_main.py
import json
import os
import time
import uuid
import boto3
def set_connection_header(request, operation_name, **kwargs):
# request.headers['Connection'] = 'keep-alive'
View buildspec_container.yml
---
version: 0.2
phases:
pre_build:
commands:
- $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)
- COMMIT_HASH="$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)"
- IMAGE_TAG="${COMMIT_HASH:=latest}"
- printenv
@milancermak
milancermak / timeout.swift
Created Jan 30, 2019
Set custom request timeout for AWSLambdaInovker
View timeout.swift
// my swift is a little bit rusty, hope this works
let configuration = AWSServiceManager.defaultServiceManager().defaultServiceConfiguration.copy();
configuration.timeoutIntervalForRequest = 90;
AWSLambdaInvoker.register(with: configuration!, forKey: "USWest2LambdaInvoker")
let lambdaInvoker = AWSLambdaInvoker(forKey: "USWest2LambdaInvoker")
// lambdaInvoker.invokeFunction ...
@milancermak
milancermak / s3_streaming_upload.py
Created Jun 14, 2018
Streaming upload of a file directly to S3 in AWS Lambda
View s3_streaming_upload.py
from botocore.vendored import requests
import boto3
def main(event, context):
s3 = boto3.resource('s3')
bucket = s3.Bucket('mybucket')
destination = bucket.Object('path/to/destination')
url = 'https://foobar.com'
View gatekeeper.py
class NotReadyError(Exception):
"""
Custom exception signaling to the Step Function
that it cannot move to the next state. Instead,
the Retry block is triggered, pausing the process.
You can pass details about the progress as a
string when initializing it. It will be shown
in the SF console.
"""
@milancermak
milancermak / classMethodIntrospection.swift
Created Dec 21, 2015
A way how to introspect methods in Swift at runtime
View classMethodIntrospection.swift
var methodCount: UInt32 = 0
let methods = class_copyMethodList(anInstance.dynamicType, &methodCount) // replace anInstace
for i in 0..<Int(methodCount) {
NSLog("method: \(NSStringFromSelector(method_getName(methods[i])))")
}
@milancermak
milancermak / gist:e0b959a5195d28133a1f
Created Dec 4, 2014
Drawing a dashed line on GoogleMaps for iOS
View gist:e0b959a5195d28133a1f
- (void)drawDashedLineOnMapBetweenOrigin:(CLLocation *)originLocation destination:(CLLocation *)destinationLocation {
[self.mapView clear];
CGFloat distance = [originLocation distanceFromLocation:destinationLocation];
if (distance < kMinimalDistance) return;
// works for segmentLength 22 at zoom level 16; to have different length,
// calculate the new lengthFactor as 1/(24^2 * newLength)
CGFloat lengthFactor = 2.7093020352450285e-09;
CGFloat zoomFactor = pow(2, self.mapView.camera.zoom + 8);
@milancermak
milancermak / gist:5775152
Created Jun 13, 2013
Question about combining signals in ReactiveCocoa
View gist:5775152
@interface LoginViewController ()
@property (strong, nonatomic) RACSubject *textFieldReturnPressed;
@property (strong, nonatomic) UITextField *usernameTextField;
@property (strong, nonatomic) UITextField *passwordTextField;
@end
@implementation LoginViewController
@milancermak
milancermak / gist:4518064
Last active Dec 11, 2015
An example of a good error message coming from an HTTP API (illustrative for http://milancermak.posterous.com/http-apis-and-errors-doing-it-the-right-way)
View gist:4518064
HTTP/1.1 402 Payment Required
Content-Type: application/json
{
"title": "End of trial",
"description": "Unfortunately, your trial period has ended. To continue using the application, you need to purchase a subscription.",
"explanation_url": "https://example.com/troubleshooting/payment-required",
"trace_url": "https://apilog.example.com/f26a3e1dc"
}