API Server - nathantheinventor/open-contest GitHub Wiki

Introduction

All dynamic data on OpenContest is sent through AJAX to the API Server. The API server code is located in src/main/web/.

How To Create an API Endpoint

First, import the register library:

from code.util import register

Second, write the API function. This function must take the following three parameters:

  • params: dict object of the POST request parameters
  • setHeader: function to set a response header
  • user: object describing the current user

For example, you could write the function

def apiFunc(params, setHeader, user):
    data = params["data"]
    return "ok"

The return value of this function will be used based on its type. The return value should have one of the following types:

  • str The string will be passed straight to the requester.
  • dict or list The object will be converted to JSON before being passed to the requester.
  • int This integer will be used as the return code.

Finally, you must register your API endpoint with the following method:

register.[method]([url], [authorization], [function])

These parameters have the following values:

  • method The HTTP method for this endpoint. Either "get" or "post"
  • url The URL for this endpoint
  • authorization The type of user who can call this endpoint. Either "any", "loggedin", or "admin"
  • function The function to call when the user requests this endpoint

For example, you could create an endpoint

register.post("/apiMethod", "loggedin", apiFunc)

List of Existing API Endpoints

URL Description Parameters Authorization
/login Log the user in or reject for bad password username: string, password: string Any
/logout Log the user out none Any
/ Redirect to home page none Logged In
/submissions Redirect to the submissions page for the user none Logged In
/getMessages Return a list of messages for the user after a given date timestamp: integer Logged In
/sendMessage Send a message message: string, to: string Logged In
/submit Submit a test run or full submission for a problem problem: string, language: string, . . . Logged In
/getContests Get a list of contests none Admin
/getContest Get the details of a contest id: string Admin
/deleteContest Delete a contest id: string Admin
/editContest Create or edit a contest id: string, name: string, . . . Admin
/getProblems Get a list of problems none Admin
/getProblem Get the details of a problem id: string Admin
/deleteProblem Delete a problem id: string Admin
/editProblem Create or edit a problem id: string, title: string, . . . Admin
/getUsers Get a list of users none Admin
/createUser Create a new user username: string, type: string ("admin" or "participant") Admin
/deleteUser Delete a user username: string Admin