Skip to content

Instantly share code, notes, and snippets.

@amitu amitu/soa.py
Created Aug 16, 2016

Embed
What would you like to do?
service oriented architecture for django: pseudo code
# -*- coding: utf-8 -*-
from __future__ import division
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
class Settings(object):
def __init__(self):
self.settings = dict(
line.split("=") for line in
open("settings.py").split("\n")
)
def get(self, key):
return self.settings[key]
class MockSettings(Settings):
def __init__(self, settings):
self.settings = settings
class Sessions(object):
def get(self, sessionid, key):
return Session.objects.get(id=sessionid).data[key]
def set(self, sessionid, key, value):
pass
class MockSessions(object):
def __init__(self, store=None):
if store is None:
store = {}
self.store = store
def get(self, sessionid, key):
return self.store[sessionid][key]
def set(self, sessionid, key, value):
pass
class Authenticator(object):
def get_user(self, userid):
pass
def auth_user(self, username, password):
pass
def create_user(self, email, username, password):
pass
class HTTPServer(object):
def add_url(self, pattern, method, decorators):
self.urls[pattern] = method
def handle(self, request):
path = request.path
for p in self.urls:
matched, args, kw = match(p, path)
if matched:
return self.urls[path](request, *args, **kw)
raise HTTP404
def run(self):
global global_http
global_http = self
class CommonApp(object):
def get_user(self, request):
sessionid = request.cookie["sessionid"]
userid = self.session.get(sessionid, "userid")
return self.auth.get_user(userid)
class HealthApp(CommonApp):
def __init__(self, auth, session, http, settings):
self.auth = auth
self.session = session
self.http = http
self.settings = settings
self.http.add_url("/insurer-list", self.insurers_list)
# @login_required
def insurers_list(self, request):
self.login_required()
if not self.get_user(request).is_superuser():
return self.http.Error("not allowed")
return self.http.json(Insurers.objects.values("name"))
class MockHealthApp(HealthApp):
def set_insurers(self, insureres, errors):
self.insurers = insureres
self.errors = errors
def insurers_list(self, request):
if self.errors:
self.http.error(self.errors)
return self.http.json(self.insurers)
def main():
settings = Settings()
auth = Authenticator()
session = Sessions()
http = HTTPServer("127.0.0.1:8000")
health = HealthApp(auth, session, http, settings)
http.run()
global_http = None
def view(request):
global_http.handle(request)
def test_health():
settings = MockSettings()
auth = MockAuthenticator()
session = MockSessions()
http = HTTPServer("127.0.0.1:8000")
health = HealthApp(auth, session, http, settings)
resp = http.handle("/insurer-list")
assert resp.exception == KeyError
assert resp.exception.message = "sessionid not present"
session = MockSessions({"foo": {}})
http = HTTPServer("127.0.0.1:8000")
health = HealthApp(auth, session, http, settings)
resp = http.handle("/insurer-list")
assert resp.exception == KeyError
assert resp.exception.message = "userid not present"
http = HTTPServer("127.0.0.1:8000")
health = HealthApp(auth, session, http, settings)
resp = http.handle("/insurer-list")
assert resp.exception == KeyError
assert resp.exception.message = "userid not present"
def test_chat():
http = HTTPServer("127.0.0.1:8000")
health = MockHealthApp(error=TypeError("foo is bar"))
chat = ChatApp()
resp = http.handle("/chat")
assert resp.excpetion
insurers = ["new india"]
health = MockHealthApp(insurers=insurers)
chat = ChatApp()
ChatSession.objects.count() == 0
resp = http.handle("/chat")
assert resp.body contains all elelments of insurers
ChatSession.objects.count() == 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.