Skip to content
Robert Wikman edited this page Jul 4, 2018 · 5 revisions

Python client examples

These are just a few examples of what's possible with Redap. Check out the /api-docs for more!

Client class

First, we'll create a custom RedapClient, utilizing the requests and furl libraries.

import requests
from furl import furl


class RedapClient(object):
    URL = 'http://localhost:5000'

    def __init__(self, api_key=None):
        self.session = requests.Session()
        if api_key is not None:
            self.session.headers['x-api-key'] = api_key

    def get_url(self, segments):
        f = furl(self.URL)
        f.path.segments = ['api', *segments]
        return f.url

    def request(self, segments, method='GET', **kwargs):
        url = self.get_url(segments)
        response = self.session.request(method, url, **kwargs)
        return response.json()

# Creates instance of the RedapClient class
c = RedapClient(api_key='<key>')

Get list of users

# Sends a GET request to /api/users
print(c.request(['users']))

Get specific user

# Sends a GET request to /api/users/manager
print(c.request(['users', 'manager']))

Add user

user_new = {
    'id': 'monty',
    'email': 'monty@demo1.freeipa.org',
    'home': '/home/manager',
    'first_name': 'Monty',
    'last_name': 'Python',
    'name': 'Monty Python',
    'shell': '/bin/sh'
}

# Sends a POST request to /api/users, with the user_new payload
c.request(['users'], method='POST', json=new_user)

Update user

user_update = {
    'first_name': 'Ponty',
    'last_name': 'Mighton'
}

# Sends a PUT request to /api/users/monty, with the user_update payload
c.request(['users', 'monty'], method='PUT', json=user_update)

Clone this wiki locally