From c1f384fb9ab222fe5d12067b82e595c1864ee097 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sat, 15 Feb 2020 09:38:33 +0100 Subject: [PATCH] so many notes&hacks! --- README-penguinpay.md | 10 +++++ hack.py | 98 ++++++++++++++++++++++++++++++++++++++++++++ notes.org | 1 + requirements.txt | 3 ++ 4 files changed, 112 insertions(+) create mode 100644 README-penguinpay.md create mode 100644 hack.py create mode 100644 notes.org create mode 100644 requirements.txt diff --git a/README-penguinpay.md b/README-penguinpay.md new file mode 100644 index 0000000..769f183 --- /dev/null +++ b/README-penguinpay.md @@ -0,0 +1,10 @@ +## How to place a order with penguin pay + +### Requirements + +* An ungleich account - can be registered for free on + https://account.ungleich.ch +* httpie installed (provides the http command) + +### Get a membership + * diff --git a/hack.py b/hack.py new file mode 100644 index 0000000..c84f9f6 --- /dev/null +++ b/hack.py @@ -0,0 +1,98 @@ +from flask import Flask, request +from flask_restful import Resource, Api +import etcd3 +import json +import logging +from functools import wraps + +def readable_errors(func): + @wraps(func) + def wrapper(*args, **kwargs): + try: + return func(*args, **kwargs) + except etcd3.exceptions.ConnectionFailedError as e: + raise UncloudException('Cannot connect to etcd: is etcd running and reachable? {}'.format(e)) + except etcd3.exceptions.ConnectionTimeoutError as e: + raise UncloudException('etcd connection timeout. {}'.format(e)) + + return wrapper + + +class DB(object): + def __init__(self, config, prefix="/"): + self.config = config + + # Root for everything + self.base_prefix= '/nicohack' + + # Can be set from outside + self.prefix = prefix + + self.connect() + + @readable_errors + def connect(self): + self._db_clients = [] + for endpoint in self.config.etcd_hosts: + client = etcd3.client(host=endpoint, **self.config.etcd_args) + self._db_clients.append(client) + + def realkey(self, key): + return "{}{}/{}".format(self.base_prefix, + self.prefix, + key) + + @readable_errors + def get(self, key, as_json=False, **kwargs): + value, _ = self._db_clients[0].get(self.realkey(key), **kwargs) + + if as_json: + value = json.loads(value) + + return value + + + @readable_errors + def set(self, key, value, as_json=False, **kwargs): + if as_json: + value = json.dumps(value) + + # FIXME: iterate over clients in case of failure ? + return self._db_clients[0].put(self.realkey(key), value, **kwargs) + + +class Membership(Resource): + def __init__(self, config): + self.config = config + + def get(self): + data = request.get_json(silent=True) or {} + print("{} {}".format(data, config)) + return {'message': 'Order successful' }, 200 + + +class Order(Resource): + def __init__(self, config): + self.config = config + + @staticmethod + def post(): + print("{} {}".format(data, config)) + data = request.get_json(silent=True) or {} + + + + +if __name__ == '__main__': + app = Flask(__name__) + + config = {} + + config['etcd_url']="https://etcd1.ungleich.ch" + config['ldap_url']="ldaps://ldap1.ungleich.ch" + + api = Api(app) + api.add_resource(Order, '/order', resource_class_args=( config, )) + api.add_resource(Membership, '/membership', resource_class_args=( config, )) + + app.run(host='::', port=5000, debug=True) diff --git a/notes.org b/notes.org new file mode 100644 index 0000000..72e8ffc --- /dev/null +++ b/notes.org @@ -0,0 +1 @@ +* diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..668fb3f --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +flask-restful +ldap3 +etcd3