Skip to content
This repository was archived by the owner on Apr 28, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions marconiclient/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
"""Marconi Client Library Binding"""

# Hoist everything up into marconiclient
from client import *
from misc import *
from exceptions import *
from auth import *
from queue import *
from message import *
from claim import *
from marconiclient.client import Connection
from marconiclient import exceptions
14 changes: 7 additions & 7 deletions marconiclient/auth.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

from exceptions import ClientException

from keystoneclient.v2_0 import client as ksclient
from keystoneclient import exceptions
from keystoneclient.v2_0 import client as ksclient

from marconiclient import exceptions as exc


def authenticate(auth_url, user, key, **kwargs):
Expand Down Expand Up @@ -46,11 +46,11 @@ def authenticate(auth_url, user, key, **kwargs):
insecure=insecure)

except exceptions.Unauthorized as ex:
raise ClientException('Unauthorized. Check username, password'
' and tenant name/id')
raise exc.ClientException('Unauthorized. Check username, password'
' and tenant name/id')

except exceptions.AuthorizationFailure as err:
raise ClientException('Authorization Failure. %s' % err)
raise exc.ClientException('Authorization Failure. %s' % err)

if not endpoint:
# The user did not pass in an endpoint, so we need to
Expand All @@ -68,6 +68,6 @@ def authenticate(auth_url, user, key, **kwargs):
service_type=service_type,
endpoint_type=endpoint_type)
except exceptions.EndpointNotFound as ex:
raise ClientException('Endpoint not found in service catalog')
raise exc.ClientException('Endpoint not found in service catalog')

return (endpoint, _ksclient.auth_token)
41 changes: 0 additions & 41 deletions marconiclient/claim.py

This file was deleted.

75 changes: 39 additions & 36 deletions marconiclient/client.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@

from eventlet.green.urllib import quote
import eventlet
eventlet.monkey_patch(socket=True, select=True)
try:
import simplejson as json
except ImportError:
import json
import urlparse

import json
from functools import wraps
from auth import authenticate
from misc import proc_template
from queue import Queue, NoSuchQueueError
from exceptions import ClientException
from urlparse import urljoin
import eventlet
import requests

from marconiclient import auth
from marconiclient import misc
from marconiclient import resources

eventlet.monkey_patch(socket=True, select=True)


class Connection(object):
def __init__(self, client_id, auth_endpoint, user, key, **kwargs):
Expand Down Expand Up @@ -61,22 +63,19 @@ def connect(self, token=None):
self.auth_token = token
else:
(self._endpoint,
self.auth_token) = authenticate(self._auth_endpoint,
self._user, self._key,
endpoint=self._endpoint,
cacert=self._cacert)
self.auth_token) = auth.authenticate(
self._auth_endpoint,
self._user, self._key,
endpoint=self._endpoint,
cacert=self._cacert)
self._load_homedoc_hrefs()

@property
def auth_token(self):
try:
return self._session.headers['X-Auth-Token']
except KeyError:
return None
return self._session.headers.get('X-Auth-Token')

@auth_token.setter
def auth_token(self, value):
self._token = value
self._session.headers['X-Auth-Token'] = value

def _load_homedoc_hrefs(self):
Expand Down Expand Up @@ -122,27 +121,28 @@ def create_queue(self, queue_name):
:param queue_name: The name of the queue
:param ttl: The default time-to-live for messages in this queue
"""
href = proc_template(self.queue_href, queue_name=queue_name)
href = misc.proc_template(self.queue_href, queue_name=queue_name)
body = {}

self._perform_http(href=href, method='PUT', request_body=body)

return Queue(self, href=href, name=queue_name, metadata=body)
return resources.Queue(self, href=href, name=queue_name, metadata=body)

def get_queue(self, queue_name):
"""
Gets a queue by name

:param queue_name: The name of the queue
"""
href = proc_template(self.queue_href, queue_name=queue_name)
href = misc.proc_template(self.queue_href, queue_name=queue_name)

try:
hdrs, body = self._perform_http(href=href, method='GET')
except ClientException as ex:
raise NoSuchQueueError(queue_name) if ex.http_status == 404 else ex
except exc.ClientException as ex:
raise exc.NoSuchQueueError(queue_name) if \
ex.http_status == 404 else ex

return Queue(self, href=href, name=queue_name, metadata=body)
return resources.Queue(self, href=href, name=queue_name, metadata=body)

def get_queues(self):
href = self.queues_href
Expand All @@ -151,25 +151,28 @@ def get_queues(self):
queues = res["queues"]

for queue in queues:
yield Queue(conn=self._conn, name=queue['name'],
href=queue['href'], metadata=queue['metadata'])
yield resources.Queue(conn=self._conn,
name=queue['name'],
href=queue['href'],
metadata=queue['metadata'])

def delete_queue(self, queue_name):
"""
Deletes a queue

:param queue_name: The name of the queue
"""
href = proc_template(self.queue_href, queue_name=queue_name)
href = misc.proc_template(self.queue_href, queue_name=queue_name)
self._perform_http(href=href, method='DELETE')

def get_queue_metadata(self, queue_name):
href = proc_template(self._queue_href, queue_name=queue_name)
href = misc.proc_template(self._queue_href, queue_name=queue_name)

try:
return self._perform_http(conn, href, 'GET')
except ClientException as ex:
raise NoSuchQueueError(queue_name) if ex.http_status == 404 else ex
except exc.ClientException as ex:
raise exc.NoSuchQueueError(queue_name) if \
ex.http_status == 404 else ex

def _perform_http(self, method, href, request_body='', headers={}):
"""
Expand All @@ -185,7 +188,7 @@ def _perform_http(self, method, href, request_body='', headers={}):
if not isinstance(request_body, str):
request_body = json.dumps(request_body)

url = urljoin(self._endpoint, href)
url = urlparse.urljoin(self._endpoint, href)

response = requests.request(method=method, url=url,
data=request_body, headers={"Client-Id": self._client_id})
Expand All @@ -195,10 +198,10 @@ def _perform_http(self, method, href, request_body='', headers={}):

# Check if the status code is 2xx class
if not response.ok:
raise ClientException(href=href,
method=method,
http_status=response.status_code,
http_response_content=response.content)
raise exc.ClientException(href=href,
method=method,
http_status=response.status_code,
http_response_content=response.content)

resp_body = json.loads(response.content) if response.content else ''

Expand Down
14 changes: 13 additions & 1 deletion marconiclient/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


class ClientException(Exception):
"""Exception for wrapping up Marconi client errors"""
def __init__(self, href='', http_status=0,
Expand All @@ -12,3 +11,16 @@ def __init__(self, href='', http_status=0,

msg = "%s %s returned %d" % (self.method, self.href, self.http_status)
Exception.__init__(self, msg)


class NoSuchQueueError(Exception):
def __init__(self, name):
self._queue_name = name

def __str__(self):
return "Queue '%s' not found" % (self._queue_name)


class NoSuchMessageError(Exception):
def __init__(self, name):
pass
42 changes: 0 additions & 42 deletions marconiclient/message.py

This file was deleted.

Loading