so many notes&hacks!
This commit is contained in:
parent
200a7672f2
commit
c1f384fb9a
4 changed files with 112 additions and 0 deletions
10
README-penguinpay.md
Normal file
10
README-penguinpay.md
Normal file
|
@ -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
|
||||||
|
*
|
98
hack.py
Normal file
98
hack.py
Normal file
|
@ -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)
|
1
notes.org
Normal file
1
notes.org
Normal file
|
@ -0,0 +1 @@
|
||||||
|
*
|
3
requirements.txt
Normal file
3
requirements.txt
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
flask-restful
|
||||||
|
ldap3
|
||||||
|
etcd3
|
Loading…
Reference in a new issue