Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Implement multiple service support to Bugzilla API #31

Merged
merged 1 commit into from

3 participants

@BYK
Owner

This diff introduces a "Service" class that namespaces each bugzilla
service and sets the proper service_url global on the API module.

Usage is as follows:

from pyresto.apis import bugzilla

bugzilla.mozilla.Bug.get('774141')

Any bugzilla service that is not predefined can be defined as follows:

my_service = bugzilla.Service('my_service', 'https://api.bugzilla.myserver.com')
my_service.Bug.get('3141592')
@berkerpeksag berkerpeksag was assigned
@travisbot

This pull request passes (merged 2fa09746 into b89229c).

pyresto/apis/bugzilla/__init__.py
((10 lines not shown))
__author__ = ('Berker Peksag <berker.peksag@gmail.com>',
'Burak Yigit Kaya <ben@byk.im>')
+
+__models_file__ = os.path.join(os.path.dirname(__file__), 'models.py')
+__models_code__ = compile(open(__models_file__).read(),
+ __models_file__, 'exec')
+
+__services__ = {
+ 'mozilla': 'https://api-dev.bugzilla.mozilla.org/latest/',
+ 'mozilla_test': 'https://api-dev.bugzilla.mozilla.org/test/latest/',
+ 'mozilla11': 'https://api-dev.bugzilla.mozilla.org/1.1/',
+ 'mozilla11_test': 'https://api-dev.bugzilla.mozilla.org/test/1.1/'
+}
+
+__all__ = ('Service', ) + tuple(__services__.iterkeys())
@berkerpeksag Collaborator

('Service',)

@berkerpeksag Collaborator
__services__.keys()
@BYK Owner
BYK added a note

__services__.keys() is a list, we need a tuple.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@berkerpeksag berkerpeksag commented on the diff
pyresto/apis/bugzilla/__init__.py
((14 lines not shown))
+__models_code__ = compile(open(__models_file__).read(),
+ __models_file__, 'exec')
+
+__services__ = dict(
+ mozilla='https://api-dev.bugzilla.mozilla.org/latest/',
+ mozilla_test='https://api-dev.bugzilla.mozilla.org/test/latest/',
+ mozilla11='https://api-dev.bugzilla.mozilla.org/1.1/',
+ mozilla11_test='https://api-dev.bugzilla.mozilla.org/test/1.1/'
+)
+
+__all__ = ('Service', ) + tuple(__services__.iterkeys())
+
+
+class Service(object):
+ def __init__(self, name, url):
+ self.name = name
@berkerpeksag Collaborator

service_name?

@BYK Owner
BYK added a note

I think this version is better. Any reasons?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
pyresto/apis/bugzilla/__init__.py
((22 lines not shown))
+)
+
+__all__ = ('Service', ) + tuple(__services__.iterkeys())
+
+
+class Service(object):
+ def __init__(self, name, url):
+ self.name = name
+ self.module_name = '{0}.{1}'.format(__name__, self.name)
+ self.url = url
+ self.__namespace = None
+
+ @property
+ def namespace(self):
+ if self.__namespace is None:
+ # All these "namespacing tricks" are from (from slides 40+)
@berkerpeksag Collaborator

43?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
pyresto/apis/bugzilla/__init__.py
((24 lines not shown))
+__all__ = ('Service', ) + tuple(__services__.iterkeys())
+
+
+class Service(object):
+ def __init__(self, name, url):
+ self.name = name
+ self.module_name = '{0}.{1}'.format(__name__, self.name)
+ self.url = url
+ self.__namespace = None
+
+ @property
+ def namespace(self):
+ if self.__namespace is None:
+ # All these "namespacing tricks" are from (from slides 40+)
+ # https://speakerdeck.com/u/antocuni/p/python-white-magic
+ self.__namespace = imp.new_module(self.module_name)
@berkerpeksag Collaborator

<3 imp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
pyresto/apis/bugzilla/__init__.py
((36 lines not shown))
+ if self.__namespace is None:
+ # All these "namespacing tricks" are from (from slides 40+)
+ # https://speakerdeck.com/u/antocuni/p/python-white-magic
+ self.__namespace = imp.new_module(self.module_name)
+ self.__namespace.__service_url__ = self.url
+ exec __models_code__ in self.__namespace.__dict__
+
+ return self.__namespace
+
+ def __getattr__(self, item):
+ return getattr(self.namespace, item)
+
+
+# Create services
+globs = globals()
+for name, url in __services__.iteritems():
@berkerpeksag Collaborator
__services__.items()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
pyresto/apis/bugzilla/__init__.py
((35 lines not shown))
+ def namespace(self):
+ if self.__namespace is None:
+ # All these "namespacing tricks" are from (from slides 40+)
+ # https://speakerdeck.com/u/antocuni/p/python-white-magic
+ self.__namespace = imp.new_module(self.module_name)
+ self.__namespace.__service_url__ = self.url
+ exec __models_code__ in self.__namespace.__dict__
+
+ return self.__namespace
+
+ def __getattr__(self, item):
+ return getattr(self.namespace, item)
+
+
+# Create services
+globs = globals()
@berkerpeksag Collaborator

_globals?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@travisbot

This pull request passes (merged ac029022 into b89229c).

@travisbot

This pull request passes (merged f82df22d into b89229c).

@BYK Implement multiple service support to Bugzilla API
This diff introduces a "Service" class that namespaces each bugzilla
service and sets the proper __service_url__ global on the API module.

Usage is as follows:

    from pyresto.apis import bugzilla

    bugzilla.mozilla.Bug.get('774141')

Any bugzilla service that is not predefined can be defined as follows:

    my_service = bugzilla.Service('my_service', 'https://api.bugzilla.myserver.com')
    my_service.Bug.get('3141592')
34e02f9
@travisbot

This pull request passes (merged 34e02f9 into b89229c).

@berkerpeksag berkerpeksag merged commit ecb2ad5 into master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 29, 2012
  1. Implement multiple service support to Bugzilla API

    authored
    This diff introduces a "Service" class that namespaces each bugzilla
    service and sets the proper __service_url__ global on the API module.
    
    Usage is as follows:
    
        from pyresto.apis import bugzilla
    
        bugzilla.mozilla.Bug.get('774141')
    
    Any bugzilla service that is not predefined can be defined as follows:
    
        my_service = bugzilla.Service('my_service', 'https://api.bugzilla.myserver.com')
        my_service.Bug.get('3141592')
This page is out of date. Refresh to see the latest.
View
46 pyresto/apis/bugzilla/__init__.py
@@ -1,8 +1,50 @@
#!/usr/bin/env python
# coding: utf-8
-from .models import *
+import imp
+import os.path
-__version__ = '0.1'
+__version__ = '0.2'
__author__ = ('Berker Peksag <berker.peksag@gmail.com>',
'Burak Yigit Kaya <ben@byk.im>')
+
+__models_file__ = os.path.join(os.path.dirname(__file__), 'models.py')
+__models_code__ = compile(open(__models_file__).read(),
+ __models_file__, 'exec')
+
+__services__ = dict(
+ mozilla='https://api-dev.bugzilla.mozilla.org/latest/',
+ mozilla_test='https://api-dev.bugzilla.mozilla.org/test/latest/',
+ mozilla11='https://api-dev.bugzilla.mozilla.org/1.1/',
+ mozilla11_test='https://api-dev.bugzilla.mozilla.org/test/1.1/'
+)
+
+__all__ = ('Service',) + tuple(__services__.iterkeys())
+
+
+class Service(object):
+ def __init__(self, name, url):
+ self.name = name
@berkerpeksag Collaborator

service_name?

@BYK Owner
BYK added a note

I think this version is better. Any reasons?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ self.module_name = '{0}.{1}'.format(__name__, self.name)
+ self.url = url
+ self.__namespace = None
+
+ @property
+ def namespace(self):
+ if self.__namespace is None:
+ # All these "namespacing tricks" are from (from slides 43+)
+ # https://speakerdeck.com/u/antocuni/p/python-white-magic?slide=87
+ self.__namespace = imp.new_module(self.module_name)
+ self.__namespace.__service_url__ = self.url
+ exec __models_code__ in self.__namespace.__dict__
+
+ return self.__namespace
+
+ def __getattr__(self, item):
+ return getattr(self.namespace, item)
+
+
+# Create services
+_globals = globals()
+for name, url in __services__.iteritems():
+ _globals[name] = Service(name, url)
View
2  pyresto/apis/bugzilla/models.py
@@ -20,7 +20,7 @@ def __call__(self, r):
class BugzillaModel(Model):
- _url_base = 'https://api-dev.bugzilla.mozilla.org/latest/'
+ _url_base = __service_url__
def __repr__(self):
if hasattr(self, 'ref'):
View
6 pyresto/core.py
@@ -24,7 +24,11 @@
from urllib import quote
-__all__ = ('Error', 'Model', 'Many', 'Foreign')
+__all__ = ('PyrestoException',
+ 'PyrestoServerResponseException',
+ 'PyrestoInvalidRestMethodException',
+ 'PyrestoInvalidAuthTypeException',
+ 'Model', 'Many', 'Foreign')
ALLOWED_HTTP_METHODS = frozenset(('GET', 'POST', 'PUT', 'DELETE', 'PATCH'))
Something went wrong with that request. Please try again.