Skip to content

Example application

Examples for Bash are shown in the usage section. In this section we provide code snippets for Java and Python.

The first step is to import the necessary dependencies:

```Python tab= import json import requests

from requests.auth import HTTPDigestAuth

```Java tab=
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

To perform the request against the RESTful API you will need to define its URL and headers:

```Python tab= instance = "https://:" endpoint = '/api/records/' headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': 'Bearer ' }

```Java tab=
URL url = new URL("https://<host>:<port>/api/records/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "Bearer <ACCESS_TOKEN>");

The following code snippet will create a new document (insert, using a POST operation) and then pretty print the result:

```Python tab= url = '{0}{1}'.format(instance, endpoint) access ={ "owner": ["Egroup-One@cern.ch"], "read": ["Egroup-One@cern.ch"], "update": ["Egroup-One@cern.ch"], "delete": ["Egroup-One@cern.ch"] } data = { "_access": access, "description": "Document inserted via python", "title": "PyDoc" }

response = requests.post(url, json=data, headers=headers) print('Response code {0}\n'.format(response.status_code))

For a successful API call, response code will be 200 (OK)

if(response.ok): content = json.loads(response.content) print("The response contains {0} properties\n".format(len(content))) print(json.dumps(content, indent=2, sort_keys=True))

else: # If response code is not ok (200), print the resulting http error code with description print(response.text) response.raise_for_status()

```Java tab=
try {

    String input = "{\"_access\": {" +
                        "\"owner\": [\"Egroup-One@cern.ch\"]," +
                        "\"read\": [\"Egroup-One@cern.ch\"]," +
                        "\"update\": [\"Egroup-One@cern.ch\"]," +
                        "\"delete\": [\"Egroup-One@cern.ch\"]" +
                    "}," +
                    "\"description\": \"Document inserted via Java\"," +
                    "\"title\": \"JDoc\"" +
                    "}";

    OutputStream os = conn.getOutputStream();
    os.write(input.getBytes());
    os.flush();

    if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
        throw new RuntimeException("Failed : HTTP error code : "
                + conn.getResponseCode());
    }

    BufferedReader br = new BufferedReader(
        new InputStreamReader((conn.getInputStream()))
    );

    String output;
    System.out.println("Response code 200 \n");
    while ((output = br.readLine()) != null) {
        System.out.println(output);
    }

    conn.disconnect();

} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

Then you can query (search) for the previous document:

```Python tab=

Search: GET the list of documents

url = '{0}{1}'.format(instance, endpoint) access={'access': 'Egroup-One'} response = requests.get(url, headers=headers, params=access) print('Response code {0}\n'.format(response.status_code))

For successful API call, response code will be 200 (OK)

if(response.ok): content = json.loads(response.content) print("The response contains {0} properties\n".format(len(content))) print(json.dumps(content, indent=2, sort_keys=True))

else: # If response code is not ok (200), print the resulting http error code with description print(response.text) response.raise_for_status()

```Java tab=
try{
    //Note that the URL is built in a different way to add paramters
    String access = "Egroup-One";
    StringBuilder stringBuilder = new StringBuilder("https://<host>:<port>/api/records/");
    stringBuilder.append("?access=");
    stringBuilder.append(URLEncoder.encode(access, "UTF-8"));
    URL url = new URL(stringBuilder.toString());

    if (conn.getResponseCode() != 200) {
        throw new RuntimeException("Failed : HTTP error code : "
                + conn.getResponseCode());
    }

    BufferedReader br = new BufferedReader(
        new InputStreamReader((conn.getInputStream()))
    );

    String output;
    System.out.println("Response code 200\n");
    while ((output = br.readLine()) != null) {
        System.out.println(output);
    }

    conn.disconnect();

} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}