Skip to content

Instantly share code, notes, and snippets.

@sudojunior
Last active August 19, 2019 18:31
Show Gist options
  • Save sudojunior/34ecceda5e85a0f85953dddc53839622 to your computer and use it in GitHub Desktop.
Save sudojunior/34ecceda5e85a0f85953dddc53839622 to your computer and use it in GitHub Desktop.
A python port of the router from discordjs/discord.js#1667
# Achieved in python 3.7.x (last tested in 3.7.2)
# A script port of discord.js@master router, partly accurate as of August 19th 2019
# https://github.com/discordjs/discord.js/pull/1667
# https://github.com/discordjs/discord.js/blob/master/src/rest/APIRouter.js
"""
Issues that remain in this method of manual proxy definition:
- Calling .prop or ['prop'] subsequently adds the query to the existing state.
Not that this is so much a problem as it is a solution,
but when running in a sandbox 1 mistake and the whole class trips on itself.
- Manual build method exists for compatibility purposes inside of a class context...
without having to use enclosed private variables.
- Using .build() does not return an enclosed lambda expression or prebuilt definition
for the purposes of passing options through to a parent manager to then handle the query.
"""
METHODS = ['get', 'post', 'delete', 'patch', 'put']
class Router:
def __init__(self, *args, base: str = ""):
self.base = base
self.route = [self.base]
def __call__(self, *args):
self.route.extend(args)
return self
def __getattr__(self, name):
if name.lower() in METHODS:
return self.build()
self.route.append(name)
return self
def __getitem__(self, key):
if key.lower() in METHODS:
return self.build()
self.route.append(key)
return self
def build(self, separator: str = "/"):
return separator.join(self.route)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment