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

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 ...@@ -18,10 +18,10 @@ USAGE
===== =====
:: ::
from kitgvapi import KitGvProvider import kitgvapi
gv = KitGvProvider() gv = kitgvapi.ObjectInterface()
gv.auth('myusername', 'mypassword') gv.config.auth('myusername', 'mypassword')
iism = gv.getOe('IISM') iism = gv.getOe('IISM')
for g in iism.get_groups(): 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 <kleineheismann@kit.edu>'
__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 .clients import RestClient
from .models import Group, Oe, User from .models import Group, Oe, User
from .provider import KitGvProvider
...@@ -2,7 +2,7 @@ import json ...@@ -2,7 +2,7 @@ import json
import requests import requests
from .config import DEFAULTS, SYMBOLS from .config import DEFAULTS, SYMBOLS
from .exceptions import NotFoundError, MissingConfigError from .exceptions import NotAllowedError, NotFoundError, MissingConfigError
class RestClientConfig(object): class RestClientConfig(object):
...@@ -10,39 +10,44 @@ class RestClientConfig(object): ...@@ -10,39 +10,44 @@ class RestClientConfig(object):
self.url_schema = DEFAULTS['URL_SCHEMA'] self.url_schema = DEFAULTS['URL_SCHEMA']
self.server = DEFAULTS['SERVER'] self.server = DEFAULTS['SERVER']
self.base_uri = DEFAULTS['BASE_URI'] self.base_uri = DEFAULTS['BASE_URI']
self._username = DEFAULTS['USERNAME'] self._username = DEFAULTS['USERNAME']
self._password = DEFAULTS['PASSWORD'] self._password = DEFAULTS['PASSWORD']
self._credentials = None self._credentials = None
def get_username(self): self.enable_post = DEFAULTS['ENABLE_POST']
self.enable_delete = DEFAULTS['ENABLE_DELETE']
@property
def username(self):
if isinstance(self._username, MissingConfigError): if isinstance(self._username, MissingConfigError):
raise self._username raise self._username
return self._username return self._username
def set_username(self, username): @username.setter
def username(self, username):
self._username = username self._username = username
self._credentials = None self._credentials = None
def get_password(self): @property
def password(self):
if isinstance(self._password, MissingConfigError): if isinstance(self._password, MissingConfigError):
raise self._password raise self._password
return self._password return self._password
def set_password(self, password): @password.setter
def password(self, password):
self._password = password self._password = password
self._credentials = None self._credentials = None
def get_credentials(self): @property
def credentials(self):
if self._credentials is None: if self._credentials is None:
u = self.username u = self.username
if u: if u:
self._credentials = requests.auth.HTTPBasicAuth(u, self.password) self._credentials = requests.auth.HTTPBasicAuth(u, self.password)
return self._credentials return self._credentials
username = property(get_username, set_username)
password = property(get_password, set_password)
credentials = property(get_credentials)
class RestClient(object): class RestClient(object):
def __init__(self, config=None): def __init__(self, config=None):
...@@ -64,6 +69,8 @@ class RestClient(object): ...@@ -64,6 +69,8 @@ class RestClient(object):
return d return d
def post(self, path, data=None): 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 url = self.config.url_schema + self.config.server + self.config.base_uri + path
if data is not None: if data is not None:
data = json.dumps(data) data = json.dumps(data)
...@@ -72,6 +79,11 @@ class RestClient(object): ...@@ -72,6 +79,11 @@ class RestClient(object):
r.raise_for_status() r.raise_for_status()
return r 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): def get_oes(self, pk_only=False):
path = '/oe/list' path = '/oe/list'
l = self.get(path) l = self.get(path)
...@@ -138,13 +150,22 @@ class RestClient(object): ...@@ -138,13 +150,22 @@ class RestClient(object):
path = '/groups/{oe}/{group}/members/{user}'.format(oe=oe_pk, group=group_pk, user=user_pk) path = '/groups/{oe}/{group}/members/{user}'.format(oe=oe_pk, group=group_pk, user=user_pk)
self.post(path) self.post(path)
def remove_group_user(self, group_pk, user_pk):
raise NotImplementedError("Not ready")
def add_group_group(self, super_pk, sub_pk): def add_group_group(self, super_pk, sub_pk):
oe_pk = super_pk.partition('-')[0] oe_pk = super_pk.partition('-')[0]
path = '/groups/{oe}/{superg}/subgroups/{subg}'.format(oe=oe_pk, superg=super_pk, subg=sub_pk) path = '/groups/{oe}/{superg}/subgroups/{subg}'.format(oe=oe_pk, superg=super_pk, subg=sub_pk)
self.post(path) self.post(path)
def remove_group_group(self, super_pk, sub_pk):
raise NotImplementedError("Not ready")
def create_group(self, name, description): def create_group(self, name, description):
oe_pk = name.partition('-')[0] oe_pk = name.partition('-')[0]
path = '/groups/{oe}'.format(oe=oe_pk) path = '/groups/{oe}'.format(oe=oe_pk)
data = {'name': name, 'description': description} data = {'name': name, 'description': description}
self.post(path, data=data) self.post(path, data=data)
def delete_group(self, pk):
raise NotImplementedError("Not ready")
...@@ -12,4 +12,6 @@ DEFAULTS = { ...@@ -12,4 +12,6 @@ DEFAULTS = {
'BASE_URI': '/itbportal-rest-war/rest', 'BASE_URI': '/itbportal-rest-war/rest',
'USERNAME': MissingConfigError('No username set'), 'USERNAME': MissingConfigError('No username set'),
'PASSWORD': MissingConfigError('No password set'), 'PASSWORD': MissingConfigError('No password set'),
'ENABLE_POST': True,
'ENABLE_DELETE': False,
} }
...@@ -14,5 +14,9 @@ class MissingPkError(KitGvApiError): ...@@ -14,5 +14,9 @@ class MissingPkError(KitGvApiError):
pass pass
class NotAllowedError(KitGvApiError):
pass
class NotFoundError(KitGvApiError): class NotFoundError(KitGvApiError):
pass pass
...@@ -2,17 +2,35 @@ from .clients import RestClient ...@@ -2,17 +2,35 @@ from .clients import RestClient
from .models import Group, Oe, User from .models import Group, Oe, User
class KitGvProvider(object): class ObjectInterface(object):
def __init__(self, config=None, client=None): 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: if client is None:
self.client = RestClient(config=config) self.client = RestClient(config=client_config)
else: else:
self.client = client self.client = client
def auth(self, username, password):
self.client.config.username = username
self.client.config.password = password
def getOe(self, pk): def getOe(self, pk):
return Oe(client=self.client, pk=pk) return Oe(client=self.client, pk=pk)
...@@ -20,7 +38,7 @@ class KitGvProvider(object): ...@@ -20,7 +38,7 @@ class KitGvProvider(object):
return Group(client=self.client, pk=pk) return Group(client=self.client, pk=pk)
def newGroup(self, name, description): 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): def getUser(self, pk):
return User(client=self.client, pk=pk) return User(client=self.client, pk=pk)
...@@ -8,7 +8,7 @@ def readme(): ...@@ -8,7 +8,7 @@ def readme():
setup( setup(
name='python-kitgvapi', name='python-kitgvapi',
version='1.0.dev0', version='1.1.dev0',
description=('Python bindings for KIT Gruppenverwaltung REST API'), description=('Python bindings for KIT Gruppenverwaltung REST API'),
long_description=readme(), long_description=readme(),
author='Jens Kleineheismann', 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