Skip to content

Commit

Permalink
Don't store host to stay stateless
Browse files Browse the repository at this point in the history
  • Loading branch information
fpagnoux committed Aug 7, 2018
1 parent ebe17cb commit d015204
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
22 changes: 10 additions & 12 deletions openfisca_web_api_preview/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,12 @@ def create_app(tax_benefit_system,

data = build_data(tax_benefit_system)

def resolve_host(request):
if data['host'] is not None:
return data['host']
host = request.host_url
data['host'] = host
data['openAPI_spec']['host'] = host
return host

DEFAULT_WELCOME_MESSAGE = "This is the root of an OpenFisca Web API. To learn how to use it, check the general documentation (https://openfisca.org/doc/api) and the OpenAPI specification of this instance ({}spec)."

@app.route('/')
def get_root():
return jsonify({
'welcome': welcome_message or DEFAULT_WELCOME_MESSAGE.format(resolve_host(request))
'welcome': welcome_message or DEFAULT_WELCOME_MESSAGE.format(request.host_url)
}), 300

@app.route('/parameters')
Expand Down Expand Up @@ -103,9 +95,15 @@ def get_variable(id):

@app.route('/spec')
def get_spec():
if data['host'] is None:
resolve_host(request)
return jsonify(data['openAPI_spec'])

# Ugly Python2-compatible way
response = {}
response.update(data['openAPI_spec'])
response.update({'host': request.host_url})
return jsonify(response)

# Nice Python3 syntax, but doesn't work in Python 2
# return jsonify({**data['openAPI_spec'], **{'host': request.host_url}})

def handle_invalid_json(error):
json_response = jsonify({
Expand Down
2 changes: 1 addition & 1 deletion openfisca_web_api_preview/loader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ def build_data(tax_benefit_system):
'parameters_description': extract_description(parameters),
'variables': variables,
'variables_description': extract_description(variables),
'host': None # Will be set at first request
'host': None # Will be set by mirroring requests
}

0 comments on commit d015204

Please sign in to comment.