Example Client

Once you have generated a bearer token, you can try out the API to list vaults. The following implementation is a non-binding example to experience the API.

  1. Define a RestClient:
import requests
import json

class RestClient:
    def __init__(self, token, domain, timeout=10):
        self.token = token
        self.domain = domain
        self.timeout = timeout  # Default timeout for requests

    def get(self, endpoint, headers=None):
        try:
            res = requests.get(
                f'https://{self.domain}{endpoint}',
                headers=self._common_headers(headers),
                timeout=self.timeout
            )
            res.raise_for_status()
            return res.json()
        except requests.RequestException as e:
            print(f"GET request failed: {e}")
                  # Try to extract the response content if available
            if hasattr(e, 'response') and e.response is not None:
                print(f"Response content: {e.response.text}")  # Print response body
            return None

    def post(self, endpoint, req, headers=None):
        try:
            custom_headers = self._common_headers(headers)
            custom_headers['Content-Type'] = 'application/json'
            res = requests.post(
                f'https://{self.domain}{endpoint}',
                headers=custom_headers,
                json=req,
                timeout=self.timeout
            )
            res.raise_for_status()
            return res.json()
        except requests.RequestException as e:
            print(f"POST request failed: {e}")
            if hasattr(e, 'response') and e.response is not None:
                print(f"Response content: {e.response.text}")  # Print response body
            return None

    def _common_headers(self, additional_headers=None):
        headers = {'Authorization': f'Bearer {self.token}'}
        if additional_headers:
            headers.update(additional_headers)  # Merge with additional headers
        return headers
      
    def get_vaults(self):
       endpoint = "v2/vaults"
       parsed_json = json.dumps(self.get(endpoint),indent=4)
       print(parsed_json)
  1. Setup General Parameters and run the API call:
domain = "api.utila.io/"
endpoint = "v2/vaults"
token= "ADD_YOUR_TOKEN_HERE"
client = RestClient(token,domain)
client.get_vaults()