From 318c8e5dc4c8fbdf629abe1eea4d4292d66c68e5 Mon Sep 17 00:00:00 2001 From: sschoorens <stephane.schoorens@ubicast.eu> Date: Fri, 17 Jan 2020 17:55:23 +0100 Subject: [PATCH] ad podclient and resource object + tests refs #29848 --- Dockerfile | 3 ++ README.md | 20 +++++++ pod-client/tests/test_base.py | 22 -------- pod_client/PodClient.py | 14 +++++ pod_client/Resource.py | 73 ++++++++++++++++++++++++++ {pod-client => pod_client}/__init__.py | 0 pod_client/tests/__init__.py | 0 pod_client/tests/test_pod_client.py | 31 +++++++++++ pod_client/tests/test_resource.py | 50 ++++++++++++++++++ requirements.txt | 1 + tox.ini | 19 +++++-- 11 files changed, 207 insertions(+), 26 deletions(-) create mode 100644 README.md delete mode 100644 pod-client/tests/test_base.py create mode 100644 pod_client/PodClient.py create mode 100644 pod_client/Resource.py rename {pod-client => pod_client}/__init__.py (100%) create mode 100644 pod_client/tests/__init__.py create mode 100644 pod_client/tests/test_pod_client.py create mode 100644 pod_client/tests/test_resource.py create mode 100644 requirements.txt diff --git a/Dockerfile b/Dockerfile index 15aebc8..85dedd4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,5 +7,8 @@ RUN apt update RUN apt -qy install --no-install-recommends python3 python3-pip make RUN pip3 install -U pip +COPY requirements.txt /tmp/requirements.txt +RUN pip install --no-cache-dir -r /tmp/requirements.txt + COPY pip-requirements-docker.txt /tmp/pip-requirements-docker.txt RUN pip install --no-cache-dir -r /tmp/pip-requirements-docker.txt tox \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..9a4fa9d --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# POD client +python3 implementation of a pod client +pod is a video cms for educational https://github.com/EsupPortail/Esup-Pod + + +# build +`make build` + +# lint +`make lint` + +# deadcode +`make deadcode` + +# test +`make test` + +# Project dependencies + +`python3 python3-pip python3-requests make` \ No newline at end of file diff --git a/pod-client/tests/test_base.py b/pod-client/tests/test_base.py deleted file mode 100644 index 37a137a..0000000 --- a/pod-client/tests/test_base.py +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -from unittest import TestCase - - -def setUpModule(): - pass - - -def tearDownModule(): - pass - - -class BaseTest(TestCase): - def setUp(self): - pass - - def tearDown(self): - pass - - def test_init(self): - pass diff --git a/pod_client/PodClient.py b/pod_client/PodClient.py new file mode 100644 index 0000000..990900e --- /dev/null +++ b/pod_client/PodClient.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +from .Resource import API_URLS, Resource + + +class PodClient: + host_url = '' + + def __init__(self, host_url): + self.host_url = host_url + if not self.host_url: + raise Exception('Pod host url required') + for key in API_URLS.keys(): + self.__dict__[key] = Resource(host_url, key) diff --git a/pod_client/Resource.py b/pod_client/Resource.py new file mode 100644 index 0000000..49bbd36 --- /dev/null +++ b/pod_client/Resource.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +import requests +import logging + +API_URLS = { + "users": "/rest/users/", + "groups": "/rest/groups/", + "owners": "/rest/owners/", + "channels": "/rest/channels/", + "themes": "/rest/themes/", + "types": "/rest/types/", + "discipline": "/rest/discipline/", + "videos": "/rest/videos/", + "renditions": "/rest/renditions/", + "encodings_video": "/rest/encodings_video/", + "encodings_audio": "/rest/encodings_audio/", + "playlist_videos": "/rest/playlist_videos/", + "contributors": "/rest/contributors/", + "documents": "/rest/documents/", + "tracks": "/rest/tracks/", + "overlays": "/rest/overlays/", + "chapters": "/rest/chapters/", + "recording": "/rest/recording/", + "recordingfile": "/rest/recordingfile/", + "folders": "/rest/folders/", + "files": "/rest/files/", + "images": "/rest/images/", + "enrichments": "/rest/enrichments/", + "buildings": "/rest/buildings/", + "broadcasters": "/rest/broadcasters/" +} + + +class Resource: + host_url = '' + name = '' + + def __init__(self, host_url, resource_name): + self.host_url = host_url + if not self.host_url: + raise Exception('Pod host url required') + self.name = resource_name + if not API_URLS.get(self.name): + raise Exception('Resource url not supported') + + def _do(self, method='GET', params={}, data={}, **request_params): + url = API_URLS[self.name] + try: + response = requests.request(method, url, params, data, request_params) + except (requests.RequestException, + requests.ConnectionError, + requests.HTTPError, + requests.URLRequired, + requests.TooManyRedirectsas) as e: + logging.error(e) + return + return response + + def get(self, data=None, **request_params): + return self._do('GET', params=data, request_params=request_params) + + def post(self, data=None, **request_params): + return self._do('GET', data=data, request_params=request_params) + + def put(self, data=None, **request_params): + return self._do('GET', data=data, request_params=request_params) + + def patch(self, data=None, **request_params): + return self._do('GET', data=data, request_params=request_params) + + def delete(self, data=None, **request_params): + return self._do('GET', data=data, request_params=request_params) diff --git a/pod-client/__init__.py b/pod_client/__init__.py similarity index 100% rename from pod-client/__init__.py rename to pod_client/__init__.py diff --git a/pod_client/tests/__init__.py b/pod_client/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pod_client/tests/test_pod_client.py b/pod_client/tests/test_pod_client.py new file mode 100644 index 0000000..9cd5fe1 --- /dev/null +++ b/pod_client/tests/test_pod_client.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +from unittest import TestCase +from pod_client.PodClient import PodClient +from pod_client.Resource import Resource, API_URLS + + +HOST = 'http://pod.ubicast.net' + + +def setUpModule(): + pass + + +def tearDownModule(): + pass + + +class PodClientTest(TestCase): + + def test_init(self): + pod_client = PodClient(HOST) + for key in API_URLS.keys(): + self.assertTrue(hasattr(pod_client, key)) + self.assertTrue(isinstance(getattr(pod_client, key), Resource)) + success = True + try: + pod_client = PodClient('') + except Exception: + success = False + self.assertFalse(success) diff --git a/pod_client/tests/test_resource.py b/pod_client/tests/test_resource.py new file mode 100644 index 0000000..ae9d8de --- /dev/null +++ b/pod_client/tests/test_resource.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +from unittest import TestCase +from pod_client.Resource import Resource, API_URLS + +HOST = 'http://pod.ubicast.net' + + +def setUpModule(): + pass + + +def tearDownModule(): + pass + + +class ResourceTest(TestCase): + + def test_init(self): + resource_name = 'files' + resource_file = Resource(HOST, resource_name) + self.assertEqual(HOST, resource_file.host_url) + self.assertEqual(resource_name, resource_file.name) + success = True + try: + resource_file = Resource('', resource_name) + except Exception: + success = False + self.assertFalse(success) + success = True + try: + resource_file = Resource(HOST, 'fake') + except Exception: + success = False + self.assertFalse(success) + + def test_get(self): + pass + + def test_post(self): + pass + + def test_put(self): + pass + + def test_patch(self): + pass + + def test_delete(self): + pass diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0c28759 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +requests~=2.21 \ No newline at end of file diff --git a/tox.ini b/tox.ini index 368b27f..1b69a22 100644 --- a/tox.ini +++ b/tox.ini @@ -3,14 +3,25 @@ envlist = lint, deadcode, test toxworkdir = /tmp/tox_workdir temp_dir = /tmp/tox_tmp + skipsdist = true +[flake8] +# https://pycodestyle.readthedocs.io/en/latest/intro.html#error-codes +# Ignored errors: +# - E501: line too long +# - E265: block comment should start with '# ' (makes it easier to enable/disable code) +# - W503: line break before binary operator (deprecated rule) +# - W505: doc line too long +ignore = E501,E265,W503,W505 +exclude = .tox/,.git/,.virtualenv/,__pycache__/,build/,dist/ + [vulture] -parameters = --exclude *settings.py,*config.py --min-confidence 90 +parameters = --exclude tests/ --min-confidence 90 [pytest] -addopts = --verbose --tb=long --showlocals --color=yes --cov=pod-client -testpaths = pod-client/ +addopts = --verbose --tb=long --showlocals --color=yes --cov=pod_client +testpaths = pod_client [testenv:lint] deps = flake8 @@ -24,5 +35,5 @@ commands = vulture . {[vulture]parameters} deps = pytest >= 4.5.0 pytest-cov >= 2.7.1 - + requests commands = pytest -- GitLab