Skip to content

Instantly share code, notes, and snippets.

@AdamSaleh
Created December 13, 2013 14:33
Show Gist options
  • Save AdamSaleh/7945116 to your computer and use it in GitHub Desktop.
Save AdamSaleh/7945116 to your computer and use it in GitHub Desktop.
Mixin api request
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