-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bae1300
commit 8fd8211
Showing
7 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/local/bin/python | ||
# -*- coding: utf-8 -*- | ||
import inspect | ||
|
||
__author__ = 'magnusknutas' | ||
|
||
BASE_URL = '' | ||
|
||
|
||
def caller_name(skip=2): | ||
"""Get a name of a caller in the format module.class.method | ||
`skip` specifies how many levels of stack to skip while getting caller | ||
name. skip=1 means "who calls me", skip=2 "who calls my caller" etc. | ||
An empty string is returned if skipped levels exceed stack height | ||
""" | ||
stack = inspect.stack() | ||
start = 0 + skip | ||
if len(stack) < start + 1: | ||
return '' | ||
parentframe = stack[start][0] | ||
|
||
name = [] | ||
module = inspect.getmodule(parentframe) | ||
|
||
# `modname` can be None when frame is executed directly in console | ||
# TODO(techtonik): consider using __main__ | ||
if module: | ||
name.append(module.__name__) | ||
# detect classname | ||
|
||
if 'self' in parentframe.f_locals: | ||
# I don't know any way to detect call from the object method | ||
# XXX: there seems to be no way to detect static method call - it will | ||
# be just a function call | ||
name.append(parentframe.f_locals['self'].__class__.__name__) | ||
codename = parentframe.f_code.co_name | ||
if codename != '<module>': # top level usually | ||
name.append( codename ) # function or a method | ||
del parentframe | ||
|
||
return ".".join(name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/usr/local/bin/python | ||
# -*- coding: utf-8 -*- | ||
import json | ||
import requests | ||
from restedorm.exeptions import NotFoundException | ||
from restedorm.managers import BaseManager | ||
|
||
__author__ = 'magnusknutas' | ||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class RestObject(object): | ||
uri = str | ||
objects = BaseManager | ||
|
||
class Meta: | ||
endpoint = None | ||
|
||
def __init__(self, *args, **kwargs): | ||
for key in kwargs.keys(): | ||
setattr(self, key, kwargs[key]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/local/bin/python | ||
# -*- coding: utf-8 -*- | ||
__author__ = 'magnusknutas' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/usr/local/bin/python | ||
# -*- coding: utf-8 -*- | ||
from restedorm.managers import BaseManager | ||
|
||
__author__ = 'magnusknutas' | ||
|
||
|
||
class Endpoint(object): | ||
|
||
def __init__(self, endpoint): | ||
self.endpoint = endpoint | ||
|
||
def __call__(self, *args): | ||
if args[0].Meta.endpoint: | ||
args[0].objects = BaseManager(args[0], args[0].Meta.endpoint) | ||
else: | ||
args[0].objects = BaseManager(args[0], self.endpoint) | ||
|
||
return args[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/usr/local/bin/python | ||
# -*- coding: utf-8 -*- | ||
__author__ = 'magnusknutas' | ||
|
||
|
||
class NotFoundException(Exception): | ||
def __init__(self, value): | ||
self.value = value | ||
super(NotFoundException, self).__init__() | ||
|
||
def __str__(self): | ||
return self.value | ||
|
||
|
||
class ApiError(Exception): | ||
def __init__(self, value): | ||
self.value = value | ||
super(ApiError, self).__init__() | ||
|
||
def __str__(self): | ||
return self.value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#!/usr/local/bin/python | ||
# -*- coding: utf-8 -*- | ||
import json | ||
import requests | ||
from restedorm import BASE_URL, caller_name | ||
from restedorm.exeptions import NotFoundException, ApiError | ||
|
||
__author__ = 'magnusknutas' | ||
from restedorm.statics import HTTP_200, HTTP_404 | ||
|
||
|
||
class BaseManager(object): | ||
next = None | ||
previous = None | ||
count = 0 | ||
|
||
def __init__(self, *args, **kwargs): | ||
if len(args) > 0: | ||
self.endpoint = args[1] | ||
self.cls = args[0] | ||
self.module = caller_name(skip=4) | ||
|
||
def filter(self, **kwargs): | ||
uri = self._get_uri() + ('?' if len(kwargs) > 0 else '') | ||
for key in kwargs.keys(): | ||
uri += str(key)+"="+str(kwargs[key]) | ||
print uri | ||
|
||
r = requests.get(uri, auth=('name', 'pass')) # TODO: Fix auth | ||
if r.status_code == 404: | ||
raise NotFoundException("endpoint %s is not found" % self.endpoint) | ||
objs = json.loads(r.text) | ||
ret = [] | ||
for user in objs.get('results', {}): | ||
user.pop('id') | ||
ret.append(self.cls(**user)) | ||
self.next = objs.get('next', None) | ||
self.previous = objs.get('previous', None) | ||
self.count = objs.get('count', None) | ||
return ret | ||
|
||
def all(self): | ||
page = 1 | ||
ret = self.filter(page=page) | ||
while self.next: | ||
page += 1 | ||
ret += self.filter(page=page) | ||
return ret | ||
|
||
def get(self, ident): | ||
uri = self._get_uri() + "%i/" % ident | ||
|
||
r = requests.get(uri, auth=('name', 'pass')) # TODO: Fix auth | ||
|
||
if r.status_code == HTTP_404: | ||
raise NotFoundException("id %i not found in endpoint /%s/" % (ident, self.endpoint)) | ||
|
||
obj = json.loads(r.text) | ||
|
||
if r.status_code == HTTP_200: | ||
return self.cls(**obj) | ||
else: | ||
ApiError("%s, %s" % (r.status_code, r.text)) | ||
|
||
def create(self): | ||
pass | ||
|
||
def _get_uri(self): | ||
if self.endpoint: | ||
return BASE_URL + self.endpoint + '/' | ||
else: | ||
print "oh herro" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/local/bin/python | ||
# -*- coding: utf-8 -*- | ||
__author__ = 'magnusknutas' | ||
|
||
HTTP_404 = 404 | ||
HTTP_200 = 200 |