Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError with route_scope in endpoints.py #7

Closed
matrixise opened this issue Mar 10, 2019 · 2 comments
Closed

TypeError with route_scope in endpoints.py #7

matrixise opened this issue Mar 10, 2019 · 2 comments

Comments

@matrixise
Copy link
Contributor

Hi,

Here is a very small example when I try to construct an API with starlette-api but I get an error, could you tell me where my error is?

Thank you

import pytest
from marshmallow import Schema
from marshmallow import fields
from starlette.testclient import TestClient
from starlette_api.applications import Starlette
from starlette_api.endpoints import HTTPEndpoint
from starlette_api.routing import Router

class RunnerSchema(Schema):
    id = fields.Integer()
    name = fields.String()
    token = fields.String()

class RunnerEndpoint(HTTPEndpoint):
    async def get(self) -> RunnerSchema():
        return {"id": 1, "name": "name", "token": "token"}

def create_app():
    app = Starlette(
        components=[],
        title="Foo",
        version="0.1",
        description="Foo",
        schema=False,
        docs=False,
        debug=False,
    )

    api = Router()
    api.add_route("/runners", RunnerEndpoint, methods=["GET"])
    app.mount("/api/v4", api)

    return app

@pytest.fixture(scope="function")
def app():
    return create_app()


@pytest.fixture(scope="function")
def client(app):
    return TestClient(app)

def test_get_runners(client):
    response = client.get("/api/v4/runners")

    assert response.status_code == 200
    assert response.json() == {"id": 1, "name": "name", "token": "token"}

Result of pytest

self = <test_app.RunnerEndpoint object at 0x7f5dcb6c5588>, receive = <function _ASGIAdapter.send.<locals>.receive at 0x7f5dcb16d8c8>
send = <function ExceptionMiddleware.__call__.<locals>.app.<locals>.sender at 0x7f5dcb6c7620>

    async def __call__(self, receive: Receive, send: Send):
        request = Request(self.scope, receive=receive)
        app = self.scope["app"]
        kwargs = self.scope.get("kwargs", {})
    
        route, route_scope = app.router.get_route_from_scope(self.scope)
    
        state = {
            "scope": self.scope,
            "receive": receive,
            "send": send,
            "exc": None,
            "app": app,
>           "path_params": route_scope["path_params"],
            "route": route,
            "request": request,
        }
E       TypeError: 'NoneType' object is not subscriptable

app        = <starlette_api.applications.Starlette object at 0x7f5dcb6ab860>
kwargs     = {}
receive    = <function _ASGIAdapter.send.<locals>.receive at 0x7f5dcb16d8c8>
request    = <starlette.requests.Request object at 0x7f5dcb702a90>
route      = <bound method Router.not_found of <starlette_api.routing.Router object at 0x7f5dcb6ab978>>
route_scope = None
self       = <test_app.RunnerEndpoint object at 0x7f5dcb6c5588>
send       = <function ExceptionMiddleware.__call__.<locals>.app.<locals>.sender at 0x7f5dcb6c7620>

Version of starlette

starlette==0.11.3
starlette-api==0.5.0
@perdy
Copy link
Collaborator

perdy commented Mar 11, 2019

It seems a bug on get_route_from_scope method from Router class so I created a test to reproduce it and it seems fixed now.

I'll push the fix along with the new version today/tomorrow.

Thanks !

@perdy perdy closed this as completed in 54405c9 Mar 11, 2019
@perdy
Copy link
Collaborator

perdy commented Mar 14, 2019

Yesterday I pushed version 0.6 that should have a fix for that along with some new features.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants