Created
December 13, 2013 14:33
-
-
Save AdamSaleh/7945116 to your computer and use it in GitHub Desktop.
Mixin api request
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
from lib.api.base as base | |
class ApiCrud: | |
@classmethod | |
def api_path(cls): | |
pass | |
@classmethod | |
def parse_path_arg(cls, args): | |
path = cls.api_path() | |
path_args = [s[1:] | |
for s in path.split('/') | |
if s.startswith(":")] | |
for arg in path_args: | |
if arg in args: | |
path = path.replace(":{0}/".format(arg),args[arg]) | |
del args[arg] | |
else: | |
raise NameError("Expecting {0} as an argument.".format(arg)) | |
return path | |
@classmethod | |
def list(cls, **kwargs): | |
p = cls.parse_path_arg(kwargs) | |
return base.get(path=p, **kwargs) | |
@classmethod | |
def show(cls,uid,**kwargs): | |
p = cls.parse_path_arg(kwargs) | |
p = "{0}/{1}".format(p,uid) | |
return base.get(path=p, **kwargs) | |
@classmethod | |
def create(cls,**kwargs): | |
p = cls.parse_path_arg(kwargs) | |
return base.post(path=p, **kwargs) | |
@classmethod | |
def update(cls,uid,**kwargs) | |
p = cls.parse_path_arg(kwargs) | |
p = "{0}/{1}".format(p,uid) | |
return base.put(path=p, **kwargs) | |
@classmethod | |
def delete(cls,uid,**kwargs) | |
p = cls.parse_path_arg(kwargs) | |
p = "{0}/{1}".format(p,uid) | |
return delete(path=p, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment