Commit a082d1f7 authored by jens.kleineheismann's avatar jens.kleineheismann
Browse files

Renamed KitGvProvider into ObjectInterface...

KitGvProvider.auth() is ObjectInterface.config.auth() now.
Prepared switches to enable/disable POST and DELETE operations.
parent b14e7ab6
......@@ -18,10 +18,10 @@ USAGE
from kitgvapi import KitGvProvider
gv = KitGvProvider()
gv.auth('myusername', 'mypassword')
import kitgvapi
gv = kitgvapi.ObjectInterface()
gv.config.auth('myusername', 'mypassword')
iism = gv.getOe('IISM')
for g in iism.get_groups():
Library for accessing KIT Gruppenverwaltung REST API
The most importend class is kitgvapi.ObjectInterface
__version__ = '1.1'
__author__ = 'Jens Kleineheismann <>'
__license__ = """
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted.
from .object_interface import ObjectInterface
from .clients import RestClient
from .models import Group, Oe, User
from .provider import KitGvProvider
......@@ -2,7 +2,7 @@ import json
import requests
from .config import DEFAULTS, SYMBOLS
from .exceptions import NotFoundError, MissingConfigError
from .exceptions import NotAllowedError, NotFoundError, MissingConfigError
class RestClientConfig(object):
......@@ -10,39 +10,44 @@ class RestClientConfig(object):
self.url_schema = DEFAULTS['URL_SCHEMA']
self.server = DEFAULTS['SERVER']
self.base_uri = DEFAULTS['BASE_URI']
self._username = DEFAULTS['USERNAME']
self._password = DEFAULTS['PASSWORD']
self._credentials = None
def get_username(self):
self.enable_post = DEFAULTS['ENABLE_POST']
self.enable_delete = DEFAULTS['ENABLE_DELETE']
def username(self):
if isinstance(self._username, MissingConfigError):
raise self._username
return self._username
def set_username(self, username):
def username(self, username):
self._username = username
self._credentials = None
def get_password(self):
def password(self):
if isinstance(self._password, MissingConfigError):
raise self._password
return self._password
def set_password(self, password):
def password(self, password):
self._password = password
self._credentials = None
def get_credentials(self):
def credentials(self):
if self._credentials is None:
u = self.username
if u:
self._credentials = requests.auth.HTTPBasicAuth(u, self.password)
return self._credentials
username = property(get_username, set_username)
password = property(get_password, set_password)
credentials = property(get_credentials)
class RestClient(object):
def __init__(self, config=None):
......@@ -64,6 +69,8 @@ class RestClient(object):
return d
def post(self, path, data=None):
if not self.config.enable_post:
raise NotAllowedError("POST operations are disabled by configuration")
url = self.config.url_schema + self.config.server + self.config.base_uri + path
if data is not None:
data = json.dumps(data)
......@@ -72,6 +79,11 @@ class RestClient(object):
return r
def delete(self, path):
if not self.config.enable_delete:
raise NotAllowedError("DELETE operations are disabled by configuration")
raise NotImplementedError("Not ready")
def get_oes(self, pk_only=False):
path = '/oe/list'
l = self.get(path)
......@@ -138,13 +150,22 @@ class RestClient(object):
path = '/groups/{oe}/{group}/members/{user}'.format(oe=oe_pk, group=group_pk, user=user_pk)
def remove_group_user(self, group_pk, user_pk):
raise NotImplementedError("Not ready")
def add_group_group(self, super_pk, sub_pk):
oe_pk = super_pk.partition('-')[0]
path = '/groups/{oe}/{superg}/subgroups/{subg}'.format(oe=oe_pk, superg=super_pk, subg=sub_pk)
def remove_group_group(self, super_pk, sub_pk):
raise NotImplementedError("Not ready")
def create_group(self, name, description):
oe_pk = name.partition('-')[0]
path = '/groups/{oe}'.format(oe=oe_pk)
data = {'name': name, 'description': description}, data=data)
def delete_group(self, pk):
raise NotImplementedError("Not ready")
......@@ -12,4 +12,6 @@ DEFAULTS = {
'BASE_URI': '/itbportal-rest-war/rest',
'USERNAME': MissingConfigError('No username set'),
'PASSWORD': MissingConfigError('No password set'),
......@@ -14,5 +14,9 @@ class MissingPkError(KitGvApiError):
class NotAllowedError(KitGvApiError):
class NotFoundError(KitGvApiError):
......@@ -2,17 +2,35 @@ from .clients import RestClient
from .models import Group, Oe, User
class KitGvProvider(object):
def __init__(self, config=None, client=None):
class ObjectInterface(object):
class Config(object):
def __init__(self, provider):
self.provider = provider
def auth(self, username, password):
self.provider.client.config.username = username
self.provider.client.config.password = password
def enable_post(self):
self.provider.client.config.enable_post = True
def disable_post(self):
self.provider.client.config.enable_post = False
def enable_delete(self):
self.provider.client.config.enable_delete = True
def disable_delete(self):
self.provider.client.config.enable_delete = False
def __init__(self, client=None, client_config=None):
self.config = self.Config(self)
if client is None:
self.client = RestClient(config=config)
self.client = RestClient(config=client_config)
self.client = client
def auth(self, username, password):
self.client.config.username = username
self.client.config.password = password
def getOe(self, pk):
return Oe(client=self.client, pk=pk)
......@@ -20,7 +38,7 @@ class KitGvProvider(object):
return Group(client=self.client, pk=pk)
def newGroup(self, name, description):
raise NotImplementedError("Not sure. Use KitGvProvider().getOe().create_group() instead.")
raise NotImplementedError("Use kitgvapi.models.Oe.create_group() instead.")
def getUser(self, pk):
return User(client=self.client, pk=pk)
......@@ -8,7 +8,7 @@ def readme():
description=('Python bindings for KIT Gruppenverwaltung REST API'),
author='Jens Kleineheismann',
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment