Exemplo n.º 1
0
def set_root_path(
    root_path: str,
    app: WSGIApp,
    *endpoints: Type[RepoEndpointBase],
) -> None:
    for endpoint in endpoints:
        app.set_parcel(endpoint, root_path)
Exemplo n.º 2
0
    def setUpClass(cls) -> None:
        cls.urls_info = [f"http://localhost:800{i}/mock/info" for i in range(3)]
        cls.urls_image = [f"http://localhost:800{i}/mock/image" for i in range(3)]

        app_1 = WSGIApp()
        app_2 = WSGIApp()
        app_3 = WSGIApp()

        for app in (app_1, app_2, app_3):
            app.route("mock", "info")(MockInfoEndpoint)
            app.route("mock", "image")(MockImageEndpoint)

        form_1 = WSGIServerForm("", 8000, app_1, PATH_SERVER_LOG_1)
        form_2 = WSGIServerForm("", 8001, app_2, PATH_SERVER_LOG_2)
        form_3 = WSGIServerForm("", 8002, app_3, PATH_SERVER_LOG_3)
        cls.executor = WSGITestExecutor(form_1, form_2, form_3).start_serve()
Exemplo n.º 3
0
    def test_routing(self):
        # Make different uris
        paths = list(set(rand_strings(30)))

        def client():
            for i, path in enumerate(paths):
                uri = f"http://localhost:{8000 + i}/{path}"
                with http.get(uri) as res:
                    self.assertEqual(res.body, IDEAL_RESNPONSE)

        forms = []
        for i, path in enumerate(paths):
            app = WSGIApp()
            app.route(path)(MockEndpoint)
            forms.append(WSGIServerForm("", 8000 + i, app, PATH_SERVER_LOG))

        WSGITestExecutor(*forms).exec(client)
Exemplo n.º 4
0
def make_repo_branch(username: str, repository: str) -> WSGIApp:
    branch = WSGIApp()

    # Add endpoints required for fetching and pushing a remote repository.
    branch.route(username, repository, "info", "refs")(HandshakeEndpoint)
    branch.route(username, repository,
                 ServiceLiterals.RECEIVE_PACK)(UploadEndpoint)
    branch.route(username, repository,
                 ServiceLiterals.UPLOAD_PACK)(DownloadEndpoint)

    return branch
Exemplo n.º 5
0
    WSGIApp,
    WSGIEndpoint,
    WSGIServerForm,
    WSGITestExecutor,
)
from bamboo.api import JsonApiData
from bamboo.request import http
from bamboo.sticky.http import data_format
from bamboo.util.string import rand_string

from ... import get_log_name
from ...asgi_util import ASGIServerForm, ASGITestExecutor


app_asgi = ASGIApp()
app_wsgi = WSGIApp()
PATH_ASGI_SERVER_LOG = get_log_name(__file__, "asgi")
PATH_WSGI_SERVER_LOG = get_log_name(__file__, "wsgi")


class UserJsonApi(JsonApiData):

    user_id: str
    name: str
    email: str
    age: int


class TestJsonApi(JsonApiData):

    users: t.List[UserJsonApi]
Exemplo n.º 6
0
from bamboo import (
    JsonApiData,
    WSGIApp,
    WSGIEndpoint,
)
from bamboo.sticky.http import data_format

app = WSGIApp()


class HelloData(JsonApiData):

    text: str


@app.route("hello")
class HelloEndpoint(WSGIEndpoint):
    @data_format(input=None, output=HelloData)
    def do_GET(self) -> None:
        self.send_api(HelloData(text="Hello, World!"))
Exemplo n.º 7
0
Arquivo: app.py Projeto: jjj999/bamboo
    WSGIEndpoint,
)
from bamboo.api import JsonApiData
from bamboo.sticky.http import (
    SetCookieValue_t,
    add_preflight,
    allow_simple_access_control,
    data_format,
    has_header_of,
    set_cache_control,
    set_cookie,
)
from bamboo.util.deco import class_property
from bamboo.util.string import rand_string

js_tests_app = WSGIApp()


@js_tests_app.route("cookie")
class TestCookieEndpoint(WSGIEndpoint):

    _last_cookie: t.Optional[str] = None

    @class_property
    def last_cookie(cls) -> t.Optional[str]:
        return cls._last_cookie

    @last_cookie.setter
    def last_cookie(cls, cookie: str) -> None:
        cls._last_cookie = cookie
Exemplo n.º 8
0
import unittest

from bamboo import (
    WSGIApp,
    WSGIEndpoint,
    WSGIServerForm,
    WSGITestExecutor,
)
from bamboo.request import http

from .. import get_log_name

PATH_SERVER_LOG = get_log_name(__file__)
sub_app1 = WSGIApp()
sub_app2 = WSGIApp()
app = WSGIApp()


@sub_app1.route("client", version=(1, 2, 3))
class ClientEndpoint(WSGIEndpoint):
    def do_GET(self) -> None:
        self.send_body(b"Hello, Client!")


@sub_app2.route("hoge")
class HogeEndpoint(WSGIEndpoint):
    def do_GET(self) -> None:
        self.send_body(b"hoge")


@app.route("world")
Exemplo n.º 9
0
Arquivo: app.py Projeto: jjj999/bamboo
        else:
            tweets = [
                SingleTweet(
                    id=m.id,
                    content=m.content,
                    datetime=m.datetime.ctime(),
                ) for m in query
            ]
            return tweets


# --------------------------------------------------------------------------------

# Endpoitns ----------------------------------------------------------------------

app = WSGIApp()
Callback_t = t.Callable[[WSGIEndpoint], None]


@app.route("user")
class UserEndpoint(WSGIEndpoint):
    def setup(self, controller: UserController) -> None:
        self.controller = controller

    @data_format(input=UserRegisterInput, output=None)
    def do_POST(self, req: UserRegisterInput) -> None:
        self.controller.register(req.name, req.email)
        self.send_only_status(HTTPStatus.OK)

    @data_format(input=UserDeleteInput, output=None)
    def do_DELETE(self, req: UserDeleteInput) -> None:
Exemplo n.º 10
0
from bamboo import (AnyStringLocation, WSGIApp, WSGIEndpoint, WSGIServerForm,
                    WSGITestExecutor)
from bamboo.api import JsonApiData
from bamboo.request import http
from bamboo.sticky.http import data_format
from bamboo.util.convert import encode_binary, decode2binary
from bamboo.util.time import get_datetime_rfc822

app = WSGIApp()


class MockResponse(JsonApiData):

    image: str
    datetime: str


@app.route(AnyStringLocation(), "image")
class MockServeImageEndpoint(WSGIEndpoint):
    """Api class sending useless image to clients."""
    def setup(self, path_img: str) -> None:
        self.path_img = path_img

    @data_format(input=None, output=MockResponse)
    def do_GET(self):
        with open(self.path_img, "rb") as f:
            img = f.read()

        self.send_api(
            MockResponse(
                image=encode_binary(img),