Skip to content
/ echo Public

An API that allows clients to create/access/modify their own endpoints

Notifications You must be signed in to change notification settings

JadwigaFR/echo

Repository files navigation

README

How to run the application

Dependencies

  • ruby 3.1.2
  • rails 7.0.3
  • postgresql

Get the application running

  • Install dependencies
    bundle install
  • Configure database (copy sample file and replace values)
    cp config/database.sample.yml config/database.yml
  • Create and migrate database
    rails db:create rails db:migrate
  • Get the server running
    rails s

Running the tests

bundle exec rspec

Getting the api token

  • create a master.key file
    touch config/master.key
  • insert shared_master_key in file
    echo <shared_master_key> >> config/master.key
  • get the api_token from a rails console
    Rails.application.credentials.api_token

The API

Authorization

All API requests require the use of an API key. To authenticate an API request, you should provide your API key in the Authorization header as a bearer token. e.g. "Bearer asdwvwq-wer-xc4-34534-12312"

The endpoints

List endpoints #### Request
GET v1/endpoints HTTP/1.1
Accept: application/vnd.api+json

Response

HTTP/1.1 200 OK
Content-Type: application/vnd.api+json

{
    "data": [
        {
            "type": "endpoints",
            "id": "12345",
            "attributes": [
                "verb": "GET",
                "path": "/greeting",
                "response": {
                  "code": 200,
                  "headers": {},
                  "body": "\"{ \"message\": \"Hello, world\" }\""
                }
            ]
        }
    ]
}
Create endpoint #### Request
POST v1/endpoints HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json

{
    "data": {
        "type": "endpoints",
        "attributes": {
            "verb": "GET",
            "path": "/greeting",
            "response": {
              "code": 200,
              "headers": {},
              "body": "\"{ \"message\": \"Hello, world\" }\""
            }
        }
    }
}

Response

HTTP/1.1 201 Created
Location: http://localhost:3000/greeting
Content-Type: application/vnd.api+json

{
    "data": {
        "type": "endpoints",
        "id": "12345",
        "attributes": {
            "verb": "GET",
            "path": "/greeting",
            "response": {
              "code": 200,
              "headers": {},
              "body": "\"{ \"message\": \"Hello, world\" }\""
            }
        }
    }
}
Update endpoint #### Request
PATCH v1/endpoints/12345 HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json

{
    "data": {
        "type": "endpoints",
        "id": "12345"
        "attributes": {
            "verb": "POST",
            "path": "/greeting",
            "response": {
              "code": 201,
              "headers": {},
              "body": "\"{ \"message\": \"Hello, everyone\" }\""
            }
        }
    }
}

Response

HTTP/1.1 200 OK
Content-Type: application/vnd.api+json

{
    "data": {
        "type": "endpoints",
        "id": "12345",
        "attributes": {
            "verb": "POST",
            "path": "/greeting",
            "response": {
              "code": 201,
              "headers": {},
              "body": "\"{ \"message\": \"Hello, everyone\" }\""
            }
        }
    }
}
Delete endpoint #### Request
DELETE v1/endpoints/12345 HTTP/1.1
Accept: application/vnd.api+json

Response

HTTP/1.1 204 No Content
Error response In case client makes unexpected response or server encountered an internal problem, the api will provide an error response.

Request

DELETE v1/endpoints/1234567890 HTTP/1.1
Accept: application/vnd.api+json

Response

HTTP/1.1 404 Not found
Content-Type: application/vnd.api+json

{
    "errors": [
        {
            "code": "not_found",
            "detail": "Requested Endpoint with ID `1234567890` does not exist"
        }
    ]
}
Sample scenario

1. Client requests non-existing path

> GET /hello HTTP/1.1
> Accept: application/vnd.api+json

HTTP/1.1 404 Not found
Content-Type: application/vnd.api+json

{
    "errors": [
        {
            "code": "not_found",
            "detail": "Requested page `/hello` does not exist"
        }
    ]
}

2. Client creates an endpoint

POST /endpoints HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json

{
     "data": {
         "type": "endpoints",
         "attributes": {
             "verb": "GET",
             "path": "/hello",
             "response": {
                 "code": 200,
                 "headers": {
                     "Content-Type": "application/json"
                 },
                 "body": "\"{ \"message\": \"Hello, world\" }\""
             }
         }
     }
 }

HTTP/1.1 201 Created
Location: http://localhost:3000/hello
Content-Type: application/vnd.api+json

{
    "data": {
        "type": "endpoints",
        "id": "12345",
        "attributes": {
            "verb": "GET",
            "path": "/hello",
            "response": {
                "code": 200,
                "headers": {
                    "Content-Type": "application/json"
                },
                "body": "\"{ \"message\": \"Hello, world\" }\""
            }
        }
    }
}

3. Client requests the recently created endpoint

GET /hello HTTP/1.1
Accept: application/json

HTTP/1.1 200 OK
Content-Type: application/json

{ "message": "Hello, world" }

4. Client requests the endpoint on the same path, but with different HTTP verb

The server responds with HTTP 404 because only GET /hello endpoint is defined.

> POST /hello HTTP/1.1
> Accept: application/vnd.api+json

HTTP/1.1 404 Not found
Content-Type: application/vnd.api+json

{
    "errors": [
        {
            "code": "not_found",
            "detail": "Requested page `/hello` does not exist"
        }
    ]
}

About

An API that allows clients to create/access/modify their own endpoints

Resources

Stars

Watchers

Forks

Languages