Backend DevelopmentEasyOutputHttp apis

Status Code Court

An API gateway must answer every request with exactly one HTTP status code. Apply the rubric and pick the right one, in the right order.

50 ptsEstimated time: 15 min

Statement

Story

The Bookshelf API answers every failure with 500, and its clients have given up guessing. You are asked to write the decision function of the new error mapper: given the facts about one request, return the single status code a well-behaved REST API sends.

The rubric

Check the rules from top to bottom. The first rule that applies decides the answer.

  • 1. route=unknown (no such URL) → 404
  • 2. route=wrong-method (the URL exists, not for this method) → 405
  • 3. rate=exceeded (too many requests) → 429
  • 4. auth=none or auth=invalid (no or bad credentials) → 401
  • 5. perm=no (known user, not allowed) → 403
  • 6. item=missing → 404
  • 6. item=deleted (it existed, it is gone for good) → 410
  • 7. method is POST, PUT or PATCH and media=other (unsupported Content-Type) → 415
  • 8. method is POST, PUT or PATCH and body=malformed (not even parseable) → 400
  • 8. method is POST, PUT or PATCH and body=invalid (parseable, but breaks the validation rules) → 422
  • 9. conflict=yes (duplicate, or the state forbids it) → 409
  • 10. otherwise: POST → 201
  • 10. otherwise: DELETE → 204
  • 10. otherwise: GET, PUT or PATCH → 200

Input

The first line holds N (1 ≤ N ≤ 100). Each of the next N lines describes one request with the nine fields below, always in this order, separated by single spaces:

method=<GET|POST|PUT|PATCH|DELETE> route=<found|unknown|wrong-method> rate=<ok|exceeded> auth=<valid|none|invalid>
perm=<yes|no> media=<json|other|n/a> body=<ok|malformed|invalid|n/a> item=<exists|missing|deleted|n/a> conflict=<yes|no>

(Printed on two rows here for reading: in the input all nine fields are on one line.) media and body are n/a for GET and DELETE, and item is n/a for POST because it targets a collection.

Output

N lines: the status code of each request, in order.

Example

Input:

3
method=GET route=found rate=ok auth=valid perm=yes media=n/a body=n/a item=exists conflict=no
method=POST route=found rate=ok auth=valid perm=yes media=json body=ok item=n/a conflict=no
method=DELETE route=found rate=ok auth=none perm=no media=n/a body=n/a item=exists conflict=no

Output:

200
201
401

The third request is both unauthenticated and forbidden, but rule 4 comes before rule 5.

Sample input

3
method=GET route=found rate=ok auth=valid perm=yes media=n/a body=n/a item=exists conflict=no
method=POST route=found rate=ok auth=valid perm=yes media=json body=ok item=n/a conflict=no
method=DELETE route=found rate=ok auth=none perm=no media=n/a body=n/a item=exists conflict=no

Hints

Stuck? Hints are optional, and each one lowers the points you earn for solving this challenge.

  1. Hint 1Costs 5 points

    Sign in to reveal hints.

  2. Hint 2Costs 5 points

    Reveal the previous hint first

  3. Hint 3Costs 8 points

    Reveal the previous hint first

Lessons that teach this

Stuck on the ideas rather than the puzzle? These lessons cover what you need.