Skip to content

Instantly share code, notes, and snippets.

View Goldziher's full-sized avatar
🐙
Codoctupus

Na'aman Hirschfeld Goldziher

🐙
Codoctupus
  • Philipps & Byrne GmbH
  • Berlin
  • 09:02 (UTC +02:00)
  • LinkedIn in/nhirschfeld
View GitHub Profile
@Goldziher
Goldziher / duck.ts
Last active November 27, 2021 16:50
Duck typing in typescript
interface Duck {
quack(): string;
}
function isDuck(value: unknown): value is Duck {
return !!(
value &&
typeof value === 'object' &&
typeof Reflect.get(value, 'quack') === 'function'
);
@Goldziher
Goldziher / recursiveResolve.ts
Last active November 27, 2021 19:21
Recursive Promise Resolve
function isRecord<T = any>(value: unknown): value is Record<string | symbol, T> {
return !!(value && typeof value === 'object');
}
function isPromise<T = unknown>(value: unknown): value is Promise<T> {
return isRecord(value) && Reflect.has(value, 'then');
}
async function recursiveResolve<T>(
parsedObject: Record<string, any>,
@Goldziher
Goldziher / generate_api_types.sh
Last active November 30, 2021 21:47
Script to generate api specs from a given api using DTSGenerator
#!/bin/bash
SPECS_DOWNLOAD_URL="https://url-of-api.com/specs"
FILE="$PWD/src/my-api-specs.ts"
if [[ -f "$FILE" ]]; then
rm "$FILE"
fi
# use dtsgen to generate the target typescript file
dtsgen --url $SPECS_DOWNLOAD_URL >> "$FILE"
@Goldziher
Goldziher / main.py
Created January 1, 2022 10:02
FastAPI official example
from typing import Optional
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@Goldziher
Goldziher / route_handlers.py
Last active January 2, 2022 08:57
Route Handlers Example
from typing import List
from starlite import Partial, delete, get, patch, post, put
from my_app.models import Resource
@get(path="/resources")
def list_resources() -> List[Resource]:
...
@Goldziher
Goldziher / user_controller.py
Last active January 3, 2022 13:03
Example Starlite Controller
from typing import List
from pydantic import UUID4
from starlite import Controller, Partial, get, post, put, patch, delete
from my_app.models import User
class UserController(Controller):
path = "/users"
from os import environ
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
# we first define a dependency. The dependency is a function or method (async or sync) whose return value will be injected
def create_postgres_connection() -> AsyncEngine:
postgres_connection_string = environ.get("POSTGRES_CONNECTION_STRING", "")
if not postgres_connection_string:
raise ValueError("Missing ENV Variable POSTGRES_CONNECTION_STRING")
@Goldziher
Goldziher / main.py
Created January 3, 2022 06:47
Starlite Init Example
from starlite import Starlite, LoggingConfig
from my_app.users import UserController
app = Starlite(
route_handlers=[UserController],
on_startup=[
LoggingConfig(loggers={
"my_app": {
@Goldziher
Goldziher / main.py
Created January 3, 2022 07:27
Simple app example
from starlite import Starlite, MediaType, get
@get(path="/health-check", media_type=MediaType.TEXT)
def health_check() -> str:
return "healthy"
app = Starlite(route_handlers=[health_check])
@Goldziher
Goldziher / test_health_check.py
Created January 3, 2022 07:28
Example starlite test
from starlette.status import HTTP_200_OK
from starlite import create_test_client
from my_app.main import health_check
def test_health_check():
with create_test_client(route_handlers=[health_check]) as client:
response = client.get("/health-check")
assert response.status_code == HTTP_200_OK