Skip to content

Commit

Permalink
Merge pull request #10 from pacificclimate/post
Browse files Browse the repository at this point in the history
support POST requests
  • Loading branch information
corviday authored Dec 11, 2023
2 parents c2cc67e + 1dba4e8 commit cbe04d0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
2 changes: 2 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ SCIP backend documentation

This backend serves data about predefined regions and the locations and details of salmon population to the Salmon Climate Impacts Portal (SCIP).

It accepts parameters either as GET or POST requests. Clients should send GET requests when possible, to facilitate caching and fast return of their queries. However, there are cases where the 'overlap' parameter, a WKT string describing the extent of a region the client wishes information about, is so long it will not fit into a standard 4096 character URL. In those cases, the client may send a POST request with parameters in JSON format in the body.

.. toctree::
:maxdepth: 2
:caption: Contents:
Expand Down
12 changes: 7 additions & 5 deletions scip/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ def call(session, request_type):
except KeyError:
return Response("Endpoint {} not recognized".format(request_type), status=400)

# check for required arguments
# check for required arguments - using request.values checkes
# both parameters in URL strings and parameters in JSON-style request
# bodies.
required_params = set(get_required_args(func)).difference(["session"])

provided_params = set(request.args.keys())
provided_params = set(request.values.keys())
optional_params = set(get_keyword_args(func))

missing_params = required_params.difference(provided_params)
Expand All @@ -31,11 +33,11 @@ def call(session, request_type):
"Missing query parameters: {}".format(missing_params), status=400
)

args = {key: request.args.get(key) for key in required_params}
args = {key: request.values.get(key) for key in required_params}
kwargs = {
key: request.args.get(key)
key: request.values.get(key)
for key in optional_params
if request.args.get(key) is not None
if request.values.get(key) is not None
}

args.update(kwargs)
Expand Down
2 changes: 1 addition & 1 deletion scip/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
def add_routes(app):
db = SQLAlchemy(app)

@app.route("/api/<request_type>")
@app.route("/api/<request_type>", methods=["GET", "POST"])
def api_request(*args, **kwargs):
return api.call(db.session, *args, **kwargs)

Expand Down

0 comments on commit cbe04d0

Please sign in to comment.