hugapi / hug

Embrace the APIs of the future. Hug aims to make developing APIs as simple as possible, but no simpler.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

I'm failing to implement my use case

Spenhouet opened this issue · comments

The following is mainly a question and maybe contains feature request.
I'm posting this here (instead of Stackoverflow) since I feel the documentation is not providing me with much to go by and everything does not seem to work as I initially expected.

Consider a HTTP request to /test with the parameters token1: str and token2: str
Both parameters are JWT strings.

I did expect the following to work (pseudo code):

@hug.type()
class JWT():
    def __new__(cls, input_token: str, context: MyContext, *args, **kwargs):
        # validate token
        # get payload
        return payload


@hug.directive()
class User():
    def __new__(cls, token1: JWT, context: MyContext, *args, **kwargs):
        # token1 is referring to the HTTP parameter
        # JWT is referring to the hug.type above
        # I would expect the parameters to be supplied to the directive
        # The JWT type should transform the string into the JWT type (payload)
        
        # Request the User object from the database
        return user

@hug.directive()
class Product():
    def __new__(cls, token2: JWT, context: MyContext, *args, **kwargs):
        # Equivalent to the User directive
        return product

@hug.authentication()
def authentication(user: User, product: Product):
    # Further authentication
  
@hug.get()
def test(user: User, product: Product):
    # Some implementation

Problems I have implementing the above with hug:

  • Directives do not seem to provide the parameters (only via request)
  • Using request does not allow for a type definition via hug.type
  • Using middleware has the same two problems
  • Nested directives do not seem to be possible: #861

I would be very happy about an example how the above could be implemented with hug. Pseudo code would be enough. Maybe we are missing something in the documentation. Maybe we are misunderstanding how hug is supposed to be used.