Add ApiClient for for inter-service comms
This commit is contained in:
35
atst/api_client.py
Normal file
35
atst/api_client.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import requests
|
||||
import tornado.gen
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from tornado.httpclient import AsyncHTTPClient
|
||||
|
||||
|
||||
class ApiClient(object):
|
||||
|
||||
def __init__(self, base_url):
|
||||
self.base_url = base_url
|
||||
self.session = requests.Session()
|
||||
self.executor = ThreadPoolExecutor()
|
||||
|
||||
@tornado.gen.coroutine
|
||||
def get(self, path, **kwargs):
|
||||
return (yield self.make_request('GET', self.base_url + path, **kwargs))
|
||||
|
||||
@tornado.gen.coroutine
|
||||
def put(self, path, **kwargs):
|
||||
return self.make_request('PUT', self.base_url + path, **kwargs)
|
||||
|
||||
@tornado.gen.coroutine
|
||||
def post(self, path, **kwargs):
|
||||
return self.make_request('POST', self.base_url + path, **kwargs)
|
||||
|
||||
@tornado.gen.coroutine
|
||||
def delete(self, path, **kwargs):
|
||||
return self.make_request('DELETE', self.base_url + path, **kwargs)
|
||||
|
||||
@tornado.gen.coroutine
|
||||
def make_request(self, method, url, **kwargs):
|
||||
def _make_request(_method, _url, **kwargs):
|
||||
return requests.request(_method, _url, **kwargs)
|
||||
|
||||
return (yield self.executor.submit(_make_request, 'GET', url))
|
||||
36
atst/app.py
36
atst/app.py
@@ -1,16 +1,27 @@
|
||||
import os
|
||||
from configparser import ConfigParser
|
||||
import tornado.web
|
||||
from tornado.web import url
|
||||
|
||||
from atst.handlers.main import MainHandler
|
||||
from atst.handlers.workspace import Workspace
|
||||
from atst.handlers.request import Request
|
||||
from atst.handlers.request_new import RequestNew
|
||||
from atst.home import home
|
||||
from tornado.web import url
|
||||
from atst.api_client import ApiClient
|
||||
|
||||
|
||||
def make_app(config):
|
||||
|
||||
authz_client = ApiClient(config['default']['AUTHZ_BASE_URL'])
|
||||
|
||||
def make_app(**kwargs):
|
||||
app = tornado.web.Application([
|
||||
url( r"/", MainHandler, {'page': 'login'}, name='login' ),
|
||||
url( r"/home", MainHandler, {'page': 'home'}, name='home' ),
|
||||
url( r"/workspaces", Workspace, {'page': 'workspaces'}, name='workspaces' ),
|
||||
url( r"/workspaces",
|
||||
Workspace,
|
||||
{'page': 'workspaces', 'authz_client': authz_client},
|
||||
name='workspaces'),
|
||||
url( r"/requests", Request, {'page': 'requests'}, name='requests' ),
|
||||
url( r"/requests/new", RequestNew, {'page': 'requests_new'}, name='request_new' ),
|
||||
url( r"/requests/new/([0-9])", RequestNew, {'page': 'requests_new'}, name='request_form' ),
|
||||
@@ -20,6 +31,23 @@ def make_app(**kwargs):
|
||||
],
|
||||
template_path = home.child('templates'),
|
||||
static_path = home.child('static'),
|
||||
**kwargs
|
||||
DEBUG=config['default']['DEBUG']
|
||||
)
|
||||
return app
|
||||
|
||||
|
||||
def make_config():
|
||||
BASE_CONFIG_FILENAME = os.path.join(
|
||||
os.path.dirname(__file__),
|
||||
'../config/base.ini',
|
||||
)
|
||||
ENV_CONFIG_FILENAME = os.path.join(
|
||||
os.path.dirname(__file__),
|
||||
'../config/',
|
||||
'{}.ini'.format(os.getenv('TORNADO_ENV', 'dev').lower())
|
||||
)
|
||||
config = ConfigParser()
|
||||
|
||||
# ENV_CONFIG will override values in BASE_CONFIG.
|
||||
config.read([BASE_CONFIG_FILENAME, ENV_CONFIG_FILENAME])
|
||||
return config
|
||||
|
||||
@@ -1,19 +1,26 @@
|
||||
from atst.handler import BaseHandler
|
||||
import requests
|
||||
import tornado.gen
|
||||
|
||||
mock_workspaces = [
|
||||
{
|
||||
'name' : 'Unclassified IaaS and PaaS for Defense Digital Service (DDS)',
|
||||
'task_order' : {
|
||||
'number' : 123456,
|
||||
},
|
||||
'user_count' : 23,
|
||||
}
|
||||
]
|
||||
{
|
||||
'name' : 'Unclassified IaaS and PaaS for Defense Digital Service (DDS)',
|
||||
'id': '5966187a-eff9-44c3-aa15-4de7a65ac7ff',
|
||||
'task_order' : {
|
||||
'number' : 123456,
|
||||
},
|
||||
'user_count' : 23,
|
||||
}
|
||||
]
|
||||
|
||||
session = requests.Session()
|
||||
|
||||
class Workspace(BaseHandler):
|
||||
|
||||
def initialize(self, page):
|
||||
def initialize(self, page, authz_client):
|
||||
self.page = page
|
||||
self.authz_client = authz_client
|
||||
|
||||
@tornado.gen.coroutine
|
||||
def get(self):
|
||||
self.render( 'workspaces.html.to', page = self.page, workspaces = mock_workspaces )
|
||||
|
||||
Reference in New Issue
Block a user