Skip to content

Create samples via REST

DavidSL448 edited this page Aug 25, 2020 · 1 revision

The following instructions are a graphical way to explain what REST services needs to be called to be able to create samples, this is not the suggested way to automate a process to send samples continuously. For that case, it is suggested that the application that will call the create samples service should also acquire the authentication token

Get an authentication token

To be able to call any REST service you need to have an authentication token. This token will allow you to call any service as if you were interacting with the application via web browser.

To get an authentication token, you need to type the following url in your browser: http://xrepo.westus2.cloudapp.azure.com:8080/

Once you are in the landing page of XRepo, please open the Firefox Web Developer Tools by pressing F12. Go to the Network tab as shown in the next screenshot.


Click on the login option and type the credentials provided by the administrators of the system.

When you press the login button, a POST request is sent to the system authentication endpoint with the following information:

URL: http://xrepo.westus2.cloudapp.azure.com:8080/api/authenticate
Method: POST
Body: {"username":"<LOGIN>","password":"<PASSWORD>"}

This POST request has in the body a JSON object that embedded the credentials for the user to be authenticated as shown in the following screenshot


Please go to the "Response" tab at the right side of the Firefox Web Developer Tools. You could see an id_token returned by the application as indicated by the red square in the next screenshot. Copy & paste this id_token in your notepad. The token will allow you to make subsequent calls to the web services

Calling a service with the Authorization Token

Any service that requires authentication, needs to contains an authorization HTTP header. The value must start with the word Bearer. The next screenshot shows how to include the authorization header in a request. The example illustrates a GET request to the account endpoint, however, to create samples you need to refer to another endpoint as indicated in the next section.

    authorization: Bearer <id_token>

Example

    authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ1c2VyIiwiYXV0aCI6IlJPTEVfVVNFUiIsImV4cCI6MTU1Nzg4Njk0OX0.-t5sG5NWI8FybTm9rcNigqW9B3Z2FyggJOZdwIpZyNdNqS-pAI-k51GsA6rZ0ZHU0xQjSZS93-D26XQAl3ttQQ

Calling the create sample service

Now, you can open any Web client (e.g. Postman) and build a valid request to the "create sample" service by taking into account previous explanations. The use of the "create sample" service has a precondition which is the creation of the scaffolding information indicated in the section "Configure the experiment context". If you have not created the scaffolding yet please do it. Make sure you create a sampling with at least a sensor. Once you have created this information, check the samplingId and sensorInternalId (or sensor_id) that the system assigned to the sampling and sensors. You need to include these two parameters in the request for the system to be able to associate the measurements to the right sensor and sampling.

Below, we indicate the url, type of method and body that you need to build to consume the "create sample" endpoint.

URL: http://xrepo.westus2.cloudapp.azure.com:8080/api/samples
Method: POST
Body: {
  "samplingId": "string",
  "sensorInternalId": "string",
  "dateTime": "yyyy-MM-dd'T'HH:mm:ss.SSS",
  "measurements": {
    "var":0.00
  },
  "ts": {
    "epochSeconds": 0, 
    "nanos": 0
  }
}

Example

{
  "samplingId": "5ce611b86334053ef0947233",
  "sensorInternalId": "accelerometer_1",
  "dateTime": "2019-05-25T23:58:06.470Z",  
  "ts": {
    "epochSeconds": 1558812004,
    "nanos": 463000000
  },
  "measurements": {
    "acceleration_x":0.154894,
    "acceleration_y":0.074463
  }
}

Clone this wiki locally