Skip to content

API Docs

Alexandru Clapa edited this page Feb 21, 2017 · 28 revisions

USR API Documentation

Users

Creating a new user

To create a new user you will have to create a new POST request to the following URL: https://project-run.herokuapp.com/users. The body of the request should contain the name, email and password. A sample JSON of the request is:

{
   "user":{
      "name":"John Doe",
      "email":"[email protected]",
      "password":"dkjIUm21"
   }
}

The response will be a JSON again. A sample is provided below:

{
  "id": 4,
  "name": "John Doe",
  "email": "[email protected]",
  "password_digest": "$2a$10$XtDtrOH2Pq0fV/g60yNQF.3jafd6I/TX8KeDzL94onQpbCIc53i.2",
  "created_at": "2017-02-21T20:06:44.583Z",
  "updated_at": "2017-02-21T20:06:44.583Z"
}

Authentication

We use JWT for authentication

Getting a token

POST request to https://project-run.herokuapp.com/user_token.

The response will be a JSON again. A sample is provided below:

{
  "jwt": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0ODc3OTcwMDksInN1YiI6MX0.yNXQ5uS2-vx1jIgYD60uNPkZk-qkAndZlBRTocvoAPM"
}

Using authentication

You need to send the JWT token as an Authentication header as following Bearer jwt_token. Example for the JWT from the last response Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0ODc3OTcwMDksInN1YiI6MX0.yNXQ5uS2-vx1jIgYD60uNPkZk-qkAndZlBRTocvoAPM (more details here https://jwt.io/introduction/)

Example authentication route: GET request to https://project-run.herokuapp.com/test Will return 401 status if Unauthorised, otherwise 200 with a success status

Quiz

Creating a quiz

POST request to https://project-run.herokuapp.com/quizzes.

Response:

{
  "id": 6,
  "title": "My quiz",
  "questions": [
    {
      "id": 10,
      "question": "Question 1",
      "type": "multiple_choice",
      "answers": [
        {
          "id": 22,
          "answer": "Answer 5"
        },
        {
          "id": 21,
          "answer": "Answer 4"
        },
        {
          "id": 20,
          "answer": "Answer 3"
        },
        {
          "id": 19,
          "answer": "Answer 2"
        },
        {
          "id": 18,
          "answer": "Answer 1"
        }
      ]
    },
    {
      "id": 11,
      "question": "Question 1",
      "type": "single_choice",
      "answers": [
        {
          "id": 24,
          "answer": "Answer 2"
        },
        {
          "id": 23,
          "answer": "Answer 1"
        }
      ]
    },
    {
      "id": 12,
      "question": "Question 2",
      "left": [
        {
          "id": "WoW0nS0", // Unique ID to be used by the front end
          "answer": "left 1"
        },
        {
          "id": "yjriFaM",
          "answer": "left 2"
        }
      ],
      "right": [
        {
          "id": "QSpKSFs",
          "answer": "right 1"
        },
        {
          "id": "sU5ccVM",
          "answer": "right 2"
        }
      ],
      "type": "match"
    }
  ]
}

Getting a quiz

GET request to https://project-run.herokuapp.com/quizzes/:id. You can also get all the quizzes available by making a GET request to https://project-run.herokuapp.com/quizzes/.

Sample request and response

URL: https://project-run.herokuapp.com/quizzes/6. Response:

{
  "id": 6,
  "title": "My quiz",
  "questions": [
    {
      "id": 10,
      "question": "Question 1",
      "type": "multiple_choice",
      "answers": [
        {
          "id": 22,
          "answer": "Answer 5"
        },
        {
          "id": 21,
          "answer": "Answer 4"
        },
        {
          "id": 20,
          "answer": "Answer 3"
        },
        {
          "id": 19,
          "answer": "Answer 2"
        },
        {
          "id": 18,
          "answer": "Answer 1"
        }
      ]
    },
    {
      "id": 11,
      "question": "Question 1",
      "type": "single_choice",
      "answers": [
        {
          "id": 24,
          "answer": "Answer 2"
        },
        {
          "id": 23,
          "answer": "Answer 1"
        }
      ]
    },
    {
      "id": 12,
      "question": "Question 2",
      "left": [
        {
          "id": "WoW0nS0",
          "answer": "left 1"
        },
        {
          "id": "yjriFaM",
          "answer": "left 2"
        }
      ],
      "right": [
        {
          "id": "QSpKSFs",
          "answer": "right 1"
        },
        {
          "id": "sU5ccVM",
          "answer": "right 2"
        }
      ],
      "type": "match"
    }
  ]
}

Checking the answers of a quiz

POST request to https://project-run.herokuapp.com/quiz/:id/check

Sample JSON request
{
   "questions":[
      // single_choice
      {
         "id": 11, // id of the question
         "answer_id": 8
      },
      // match
      {
         "id": 12,
         "pairs":[
            {
               "left_choice_id": sU5ccVM,
               "right_choice_id": WoW0nS0
            },
            {
               "left_choice_id": yjriFaM,
               "right_choice_id": sU5ccVM
            }
         ]
      },
      // multiple_choice
      {
         "id":15,
         "answer_ids":[
            18,
            19
         ]
      }
   ]
}

And a sample response:

[
  {
    "id": 144,
    "correct": false,
    "correct_answer": 143
  },
  {
    "id": 145,
    "correct": true,
    "correct_answers": [
      145
    ]
  },
  {
    "id": 146,
    "correct": true,
    "correct_pairs": [
      {
        "left_choice_id": "nxY42Io",
        "right_choice_id": "pQgsn1k"
      },
      {
        "left_choice_id": "QszYLpY",
        "right_choice_id": "iCU32PE"
      },
      {
        "left_choice_id": "cPxptoc",
        "right_choice_id": "eLquggI"
      }
    ]
  }
]
Clone this wiki locally