Skip to content

burakkggul/mokapi

Repository files navigation

MokApi

This project is fake REST API for testing and prototyping. Read this in other languages: English, Turkish.

Installation

Prerequisite

  1. JDK 11

  2. Mongo DB 4.4.2

Usage

You can get a sample of requests that can be sent to all end points that can be used in the application from the tests available in /jmeter/load_test.jmx. For more information about Jmeter, click here.

HTTP GET

The HTTP GET method requests a representation of the specified resource. Requests using GET should only be used to request data (they shouldn’t include data).

Mokapi supports sending two different get requests to each endpoint. For example an answer is returned to the /users as follows.:

[
  {
    "id": "1",
    "name": "Burak GUL",
    "username": "burakgul",
    "email": "burakgul@gmail.com",
    "addresses": [
      {
        "addressName": "Test",
        "addressLine": "Test",
        "country": "Turkey",
        "city": "Ankara",
        "zipcode": "06789",
        "latitude": "40.5641",
        "longitude": "40.465461",
        "active": true
      },
      {
        "addressName": "Home",
        "addressLine": "Eryaman Mahallesi Gümüşler Apartmanı",
        "country": "Turkey",
        "city": "Ankara",
        "zipcode": "06789",
        "latitude": "40.5641",
        "longitude": "40.465461",
        "active": true
      }
    ],
    "phone": "+905000000000",
    "website": "www.testburak.com.tr",
    "company": {
      "name": "Özgür Yazılım A.Ş.",
      "catchPhrase": "Şeffaf Teknoloji, Özel Çözümler",
      "bs": "OYAS"
    }
  },
  {
    "id": "2",
    "name": "Burak Test",
    "username": "buraktest",
    "email": "buraktest@gmail.com",
    "addresses": [
      {
        "addressName": "Test",
        "addressLine": "Test",
        "country": "Turkey",
        "city": "Ankara",
        "zipcode": "06789",
        "latitude": "40.5641",
        "longitude": "40.465461",
        "active": true
      },
      {
        "addressName": "Home",
        "addressLine": "Eryaman Mahallesi Gümüşler Apartmanı",
        "country": "Turkey",
        "city": "Ankara",
        "zipcode": "06789",
        "latitude": "40.5641",
        "longitude": "40.465461",
        "active": true
      }
    ],
    "phone": "+905000000000",
    "website": "www.testburak.com.tr",
    "company": {
      "name": "Özgür Yazılım A.Ş.",
      "catchPhrase": "Şeffaf Teknoloji, Özel Çözümler",
      "bs": "OYAS"
    }
  }
]

Another method is /users/{id} The id value given here should be the id of the related data:

{
  "id": "1",
  "name": "Burak GUL",
  "username": "burakgul",
  "email": "burakkggul@gmail.com",
  "addresses": [
    {
      "addressName": "Test",
      "addressLine": "Test",
      "country": "Turkey",
      "city": "Ankara",
      "zipcode": "06789",
      "latitude": "40.5641",
      "longitude": "40.465461",
      "active": true
    },
    {
      "addressName": "Home",
      "addressLine": "Eryaman Mahallesi Gümüşler Apartmanı",
      "country": "Turkey",
      "city": "Ankara",
      "zipcode": "06789",
      "latitude": "40.5641",
      "longitude": "40.465461",
      "active": true
    }
  ],
  "phone": "+905000000000",
  "website": "www.testburak.com.tr",
  "company": {
    "name": "Özgür Yazılım A.Ş.",
    "catchPhrase": "Şeffaf Teknoloji, Özel Çözümler",
    "bs": "OYAS"
  }
}

HTTP POST

POST is used to send data to a server to create a resource. Models should be sent as a list in the body of request.

POST /users for the request, models should be sent in a list as below in the request body:

[
  {
    "name": "Burak GUL",
    "username": "burakgul",
    "email": "burakgul@gmail.com",
    "addresses": [
      {
        "addressName": "Test",
        "addressLine": "Test",
        "country": "Turkey",
        "city": "Ankara",
        "zipcode": "06789",
        "latitude": "40.5641",
        "longitude": "40.465461",
        "active": true
      },
      {
        "addressName": "Home",
        "addressLine": "Eryaman Mahallesi Gümüşler Apartmanı",
        "country": "Turkey",
        "city": "Ankara",
        "zipcode": "06789",
        "latitude": "40.5641",
        "longitude": "40.465461",
        "active": true
      }
    ],
    "phone": "+905000000000",
    "website": "www.testburak.com.tr",
    "company": {
      "name": "Özgür Yazılım A.Ş.",
      "catchPhrase": "Şeffaf Teknoloji, Özel Çözümler",
      "bs": "OYAS"
    }
  },
  {
    "name": "Burak Test",
    "username": "buraktest",
    "email": "buraktest@gmail.com",
    "addresses": [
      {
        "addressName": "Test",
        "addressLine": "Test",
        "country": "Turkey",
        "city": "Ankara",
        "zipcode": "06789",
        "latitude": "40.5641",
        "longitude": "40.465461",
        "active": true
      },
      {
        "addressName": "Home",
        "addressLine": "Eryaman Mahallesi Gümüşler Apartmanı",
        "country": "Turkey",
        "city": "Ankara",
        "zipcode": "06789",
        "latitude": "40.5641",
        "longitude": "40.465461",
        "active": true
      }
    ],
    "phone": "+905000000000",
    "website": "www.testburak.com.tr",
    "company": {
      "name": "Özgür Yazılım A.Ş.",
      "catchPhrase": "Şeffaf Teknoloji, Özel Çözümler",
      "bs": "OYAS"
    }
  }
]

HTTP PUT

PUT is used to send data to a server to update a resource.

PUT `/users' for the request, models should be sent in request body:

{
  "name": "Burak GUL",
  "username": "burakgul",
  "email": "burakgul@gmail.com",
  "addresses": [
    {
      "addressName": "Test",
      "addressLine": "Test",
      "country": "Turkey",
      "city": "Ankara",
      "zipcode": "06789",
      "latitude": "40.5641",
      "longitude": "40.465461",
      "active": true
    },
    {
      "addressName": "Home",
      "addressLine": "Eryaman Mahallesi Gümüşler Apartmanı",
      "country": "Turkey",
      "city": "Ankara",
      "zipcode": "06789",
      "latitude": "40.5641",
      "longitude": "40.465461",
      "active": true
    }
  ],
  "phone": "+905000000000",
  "website": "www.testburak.com.tr",
  "company": {
    "name": "Özgür Yazılım A.Ş.",
    "catchPhrase": "Şeffaf Teknoloji, Özel Çözümler",
    "bs": "OYAS"
  }
}

HTTP PATCH

The HTTP PATCH request body describes how the target resource should be modified to produce a new version. Furthermore, the format used to represent the [description of changes] varies depending on the resource type. For JSON resource types, the format used to describe the changes is JSON Patch.

Simply put, the JSON Patch format uses a “series of operations” to describe how the target resource should be modified. A JSON Patch document is an array of JSON objects. Each object in the array represents exactly one JSON Patch operation.

Here is the HTTP PATCH request to perform a partial update to the user’s name and username using the JSON Patch format:

curl -i -X PATCH http://localhost:8080/users/1 -H "Content-Type: application/json-patch+json" -d '[
    {"op":"replace","path":"/name","value":"Burak GUL"},
    {"op":"add","path":"/username","value":"burakgul"}
]'

Most importantly, the Content-Type for JSON Patch requests is application/json-patch+json. Also, the request body is an array of JSON Patch operation objects:

[
    {"op":"replace","path":"/name","value":"Burak GUL"},
    {"op":"add","path":"/username","value":"burakgul"}
]

JSON PATCH OPERATIONS

A JSON Patch operation is represented by a single op object.

For example, here we’re defining a JSON patch operation to update the user’s name:

{
    "op":"replace",
    "path":"/name",
    "value":"Burak GUL"
}

Each operation must have one path member. Also, some operation objects must contain a from member as well. The value of the path and from members is a JSON Pointer. It refers to a location within the target document. This location can point to a specific key or an array element in the target object.

Let’s now briefly look at the available JSON Patch operations.

The add Operation

We use the add operation to add a new member to an object. Also, we can use it to update an existing member and to insert a new value into the array at the specified index.

For example, let’s add new address to the user’s addresses list at index 0:

{
    "op":"add",
    "path":"/addresses/0",
    "value":{
      "addressName": "Test",
      "addressLine": "Test",
      "country": "Turkey",
      "city": "Ankara",
      "zipcode": "06789",
      "latitude": "40.5641",
      "longitude": "40.465461",
      "active" : true
    }
}

The modified user details after the add operation would be:

{
  "name": "Burak GUL",
  "username": "buraktest",
  "email": "buraktest@gmail.com",
  "addresses": [
    {
      "addressName": "Test",
      "addressLine": "Test",
      "country": "Turkey",
      "city": "Ankara",
      "zipcode": "06789",
      "latitude": "40.5641",
      "longitude": "40.465461",
      "active": true
    },
    {
      "addressName": "Home",
      "addressLine": "Eryaman Mahallesi Gümüşler Apartmanı",
      "country": "Turkey",
      "city": "Ankara",
      "zipcode": "06789",
      "latitude": "40.5641",
      "longitude": "40.465461",
      "active": true
    }
  ],
  "phone": "+905000000000",
  "website": "www.testburak.com.tr",
  "company": {
    "name": "Özgür Yazılım A.Ş.",
    "catchPhrase": "Şeffaf Teknoloji, Özel Çözümler",
    "bs": "OYAS"
  }
}
The remove Operation

The remove operation removes a value at the target location. Besides, it can remove an element from an array at the specified index.

For instance, let’s remove the addresses for our user:

{
    "op":"remove",
    "path":"/addresses"
}

The modified user details after the remove operation would be:

{
  "name": "Burak GUL",
  "username": "buraktest",
  "email": "buraktest@gmail.com",
  "addresses": null,
  "phone": "+905000000000",
  "website": "www.testburak.com.tr",
  "company": {
    "name": "Özgür Yazılım A.Ş.",
    "catchPhrase": "Şeffaf Teknoloji, Özel Çözümler",
    "bs": "OYAS"
  }
}

The replace Operation

The replace operation updates the value at the target location with a new value.

As an example, let’s update the telephone number for our user:

{
    "op":"replace",
    "path":"/phone",
    "value":"+905111111111"
}

The modified user details after the replace operation would be:

{
  "name": "Burak GUL",
  "username": "buraktest",
  "email": "buraktest@gmail.com",
  "addresses": null,
  "phone": "+905111111111",
  "website": "www.testburak.com.tr",
  "company": {
    "name": "Özgür Yazılım A.Ş.",
    "catchPhrase": "Şeffaf Teknoloji, Özel Çözümler",
    "bs": "OYAS"
  }
}
The move Operation

The move operation removes the value at the specified location and adds it to the target location.

For instance, let’s move “Test address” from the top of the user’s address list to the bottom of the list:

{
    "op":"move",
    "from":"/addresses/0",
    "path":"/addresses/-"
}

The modified user details after the move operation would be:

{
  "name": "Burak GUL",
  "username": "buraktest",
  "email": "buraktest@gmail.com",
  "addresses": [
    {
      "addressName": "Home",
      "addressLine": "Eryaman Mahallesi Gümüşler Apartmanı",
      "country": "Turkey",
      "city": "Ankara",
      "zipcode": "06789",
      "latitude": "40.5641",
      "longitude": "40.465461",
      "active": true
    },
    {
      "addressName": "Test",
      "addressLine": "Test",
      "country": "Turkey",
      "city": "Ankara",
      "zipcode": "06789",
      "latitude": "40.5641",
      "longitude": "40.465461",
      "active": true
    }
  ],
  "phone": "+905000000000",
  "website": "www.testburak.com.tr",
  "company": {
    "name": "Özgür Yazılım A.Ş.",
    "catchPhrase": "Şeffaf Teknoloji, Özel Çözümler",
    "bs": "OYAS"
  }
}

The /addresses/0 and /addresses/- in the above example are JSON pointers to the start and end indices of the favorites array.

The copy Operation

The copy operation copies the value at the specified location to the target location.

For example, let’s duplicate “Home Address” in the addresses list:

{
    "op":"copy",
    "from":"/addresses/0",
    "path":"/addresses/-"
}

The modified user details after the copy operation would be:

{
  "name": "Burak GUL",
  "username": "buraktest",
  "email": "buraktest@gmail.com",
  "addresses": [
    {
      "addressName": "Home",
      "addresLine": "Eryaman Mahallesi Gümüşler Apartmanı",
      "country": "Turkey",
      "city": "Ankara",
      "zipcode": "06789",
      "latitude": "40.5641",
      "longitude": "40.465461",
      "active": true
    },
    {
      "addressName": "Test",
      "addressLine": "Test",
      "country": "Turkey",
      "city": "Ankara",
      "zipcode": "06789",
      "latitude": "40.5641",
      "longitude": "40.465461",
      "active":false
    },
    {
      "addressName": "Home",
      "addresLine": "Eryaman Mahallesi Gümüşler Apartmanı",
      "country": "Turkey",
      "city": "Ankara",
      "zipcode": "06789",
      "latitude": "40.5641",
      "longitude": "40.465461",
      "active": true
    }
  ],
  "phone": "+905000000000",
  "website": "www.testburak.com.tr",
  "company": {
    "name": "Özgür Yazılım A.Ş.",
    "catchPhrase": "Şeffaf Teknoloji, Özel Çözümler",
    "bs": "OYAS"
  }
}
The test Operation

The test operation tests that the value at the “path” is equal to the “value”. Because the PATCH operation is atomic, the PATCH should be discarded if any of its operations fail. The test operation can be used to validate that the preconditions and post-conditions have been met.

For instance, let’s test that the update to the user’s phone field has been successful:

{
    "op":"test",
    "path":"/phone",
    "value":"+905111111111"
}

HTTP DELETE

It is used to delete resources collectively or by an id. To delete according to the id information, id information must be sent to the url as the path parameter. When the path parameter is not given, it means that all resources are wanted to be deleted. For example:

DELETE /users
DELETE /users/1

Contributing

Soon…​

License