From 644fa9348cabc80683b16b265c0ea74c03312bd7 Mon Sep 17 00:00:00 2001 From: Serg Melikyan Date: Mon, 11 Mar 2013 18:09:56 +0400 Subject: [PATCH] Added Session operations Change-Id: Ib1280ab5cd1d2c2c273a0a53e5433152cca4ef59 --- .../portasclient/common/base.py | 7 ++- python-portasclient/portasclient/v1/client.py | 5 +- .../portasclient/v1/sessions.py | 62 +++++++++++++++++++ 3 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 python-portasclient/portasclient/v1/sessions.py diff --git a/python-portasclient/portasclient/common/base.py b/python-portasclient/portasclient/common/base.py index 4f5c12f..2b33bb6 100644 --- a/python-portasclient/portasclient/common/base.py +++ b/python-portasclient/portasclient/common/base.py @@ -72,8 +72,11 @@ class Manager(object): return self.resource_class(self, body[response_key]) return self.resource_class(self, body) - def _create(self, url, body, response_key=None, return_raw=False): - resp, body = self.api.json_request('POST', url, body=body) + def _create(self, url, body=None, response_key=None, return_raw=False): + if body: + resp, body = self.api.json_request('POST', url, body=body) + else: + resp, body = self.api.json_request('POST', url) if return_raw: if response_key: return body[response_key] diff --git a/python-portasclient/portasclient/v1/client.py b/python-portasclient/portasclient/v1/client.py index bedc827..98bc6f0 100644 --- a/python-portasclient/portasclient/v1/client.py +++ b/python-portasclient/portasclient/v1/client.py @@ -14,7 +14,7 @@ # under the License. from portasclient.common import http -from portasclient.v1 import environments +from portasclient.v1 import environments, sessions class Client(http.HTTPClient): @@ -30,4 +30,5 @@ class Client(http.HTTPClient): def __init__(self, *args, **kwargs): """ Initialize a new client for the Portas v1 API. """ super(Client, self).__init__(*args, **kwargs) - self.environments = environments.EnvironmentManager(self) \ No newline at end of file + self.environments = environments.EnvironmentManager(self) + self.sessions = sessions.SessionManager(self) \ No newline at end of file diff --git a/python-portasclient/portasclient/v1/sessions.py b/python-portasclient/portasclient/v1/sessions.py new file mode 100644 index 0000000..2d21e55 --- /dev/null +++ b/python-portasclient/portasclient/v1/sessions.py @@ -0,0 +1,62 @@ +# Copyright 2012 OpenMeter LLC. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +import anyjson + +from portasclient.common import base + + +class Session(base.Resource): + def __repr__(self): + return '' % self._info + + def data(self, **kwargs): + return self.manager.data(self, **kwargs) + + +class Status(base.Resource): + def __repr__(self): + return '' % self._info + + def data(self, **kwargs): + return self.manager.data(self, **kwargs) + + +class SessionManager(base.Manager): + resource_class = Session + + def list(self, environment_id): + return self._list('environments/{id}/sessions'.format(id=environment_id), 'sessions') + + def get(self, environment_id, session_id): + return self._get('environments/{id}/sessions/{session_id}'.format(id=environment_id, session_id=session_id)) + + def configure(self, environment_id): + return self._create('environments/{id}/configure'.format(id=environment_id), None) + + def deploy(self, environment_id, session_id): + self.api.json_request('POST', + 'environments/{id}/sessions/{session_id}/deploy'.format(id=environment_id, + session_id=session_id)) + + def reports(self, environment_id, session_id): + resp, body = self.api.json_request('GET', + 'environments/{id}/sessions/{session_id}/reports'. + format(id=environment_id, session_id=session_id)) + + data = body['reports'] + return [Status(self, res, loaded=True) for res in data if res] + + def delete(self, environment_id, session_id): + return self._delete("environments/{id}/sessions/{session_id}".format(id=environment_id, session_id=session_id))