Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from keotl/develop
0.6.0 release
- Loading branch information
Showing
74 changed files
with
1,183 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from jivago.inject.annotation import Component, RequestScoped | ||
from jivago.lang.annotations import Inject, Override | ||
from jivago.wsgi.annotations import Resource | ||
from jivago.wsgi.filter.filter import Filter | ||
from jivago.wsgi.filter.filter_chain import FilterChain | ||
from jivago.wsgi.request.request import Request | ||
from jivago.wsgi.request.response import Response | ||
|
||
|
||
@Component | ||
@RequestScoped | ||
class UserSession(object): | ||
"""A single instance will be shared across the request lifecycle, | ||
from the filter chain to the resource class and any synchronous call it makes.""" | ||
|
||
def __init__(self): | ||
self.user_id = None | ||
|
||
def set(self, user_id: str): | ||
self.user_id = user_id | ||
|
||
def get(self) -> str: | ||
return self.user_id | ||
|
||
|
||
@Component | ||
class UserSessionInitializationFilter(Filter): | ||
|
||
@Inject | ||
def __init__(self, session: UserSession): | ||
self.session = session | ||
|
||
@Override | ||
def doFilter(self, request: Request, response: Response, chain: FilterChain): | ||
self.session.set(request.headers["Authorization"]) | ||
chain.doFilter(request, response) | ||
|
||
|
||
@Resource("/") | ||
class MyResourceClass(object): | ||
|
||
@Inject | ||
def __init__(self, user_session: UserSession): | ||
# This is the same instance that was initialized in the request filter class. | ||
self.user_session = user_session | ||
|
||
# ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from jivago.wsgi.annotations import Resource | ||
from jivago.wsgi.methods import POST | ||
from jivago.wsgi.request.request import Request | ||
from jivago.wsgi.request.streaming_request_body import StreamingRequestBody | ||
|
||
|
||
@Resource("/stream") | ||
class MyStreamingResource(object): | ||
|
||
@POST | ||
def post_stream(self, body: StreamingRequestBody): | ||
content = body.read() | ||
print(content) | ||
return "OK" | ||
|
||
@POST | ||
def post_stream2(self, request: Request): | ||
# When Transfer-Encoding is set to 'chunked', the request body will be an instance of StreamingRequestBody | ||
if isinstance(request.body, StreamingRequestBody): | ||
print(request.body.read()) | ||
return "OK" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from jivago.wsgi.annotations import Resource | ||
from jivago.wsgi.methods import GET | ||
from jivago.wsgi.request.headers import Headers | ||
from jivago.wsgi.request.response import Response | ||
from jivago.wsgi.request.streaming_response_body import StreamingResponseBody | ||
|
||
|
||
@Resource("/stream") | ||
class MyStreamingResource(object): | ||
|
||
@GET | ||
def get_stream(self) -> StreamingResponseBody: | ||
# Returning the body object automatically sets the status code to 200 OK | ||
return StreamingResponseBody(self.generate_bytes()) | ||
|
||
@GET | ||
def get_stream(self) -> Response: | ||
# A Response object can also be manually created to provide further control over transport parameters. | ||
return Response(202, Headers(), StreamingResponseBody(self.generate_bytes())) | ||
|
||
def generate_bytes(self) -> bytes: | ||
for i in range(0, 5): | ||
yield b"my bytes" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import anachronos | ||
from anachronos import Anachronos | ||
|
||
from e2e_test.testing_messages import ASYNC_INSTANTIATED_EVENT_HANDLER, \ | ||
ASYNC_FUNCTION_EVENT_HANDLER, ASYNC_RUNNABLE_EVENT_HANDLER | ||
from jivago.event.config.annotations import EventHandler, EventHandlerClass | ||
from jivago.lang.annotations import Inject, Override | ||
from jivago.lang.runnable import Runnable | ||
|
||
|
||
@EventHandler("async-event") | ||
class MyHandler(Runnable): | ||
|
||
@Inject | ||
def __init__(self, anachronos: Anachronos): | ||
self.anachronos = anachronos | ||
|
||
@Override | ||
def run(self): | ||
self.anachronos.store(ASYNC_RUNNABLE_EVENT_HANDLER) | ||
|
||
|
||
@EventHandlerClass | ||
class MyHandlerClass(object): | ||
|
||
@Inject | ||
def __init__(self, anachronos: Anachronos): | ||
self.anachronos = anachronos | ||
|
||
@EventHandler("async-event") | ||
def handle(self): | ||
self.anachronos.store(ASYNC_INSTANTIATED_EVENT_HANDLER) | ||
|
||
|
||
@EventHandler("async-event") | ||
def my_event_handler_function(): | ||
anachronos.get_instance().store(ASYNC_FUNCTION_EVENT_HANDLER) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,25 @@ | ||
from jivago.event.async_event_bus import AsyncEventBus | ||
from jivago.event.event_bus import EventBus | ||
from jivago.lang.annotations import Inject | ||
from jivago.wsgi.annotations import Resource | ||
from jivago.wsgi.methods import GET | ||
from jivago.wsgi.annotations import Resource, Path | ||
from jivago.wsgi.methods import POST | ||
|
||
|
||
@Resource("/event") | ||
class EventResource(object): | ||
|
||
@Inject | ||
def __init__(self, event_bus: EventBus): | ||
def __init__(self, event_bus: EventBus, async_event_bus: AsyncEventBus): | ||
self.async_event_bus = async_event_bus | ||
self.event_bus = event_bus | ||
|
||
@GET | ||
@POST | ||
def send_event(self) -> str: | ||
self.event_bus.emit("event") | ||
return "OK" | ||
|
||
@POST | ||
@Path("/async") | ||
def send_event_async(self) -> str: | ||
self.async_event_bus.emit("async-event") | ||
return "OK" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import time | ||
from anachronos import Anachronos | ||
|
||
from e2e_test.testing_messages import POST_HTTP_STREAM | ||
from jivago.lang.annotations import Inject | ||
from jivago.wsgi.annotations import Resource | ||
from jivago.wsgi.methods import GET, POST | ||
from jivago.wsgi.request.streaming_request_body import StreamingRequestBody | ||
from jivago.wsgi.request.streaming_response_body import StreamingResponseBody | ||
|
||
|
||
@Resource("/stream") | ||
class StreamResource(object): | ||
|
||
@Inject | ||
def __init__(self, anachronos: Anachronos): | ||
self.anachronos = anachronos | ||
|
||
@GET | ||
def get_stream(self) -> StreamingResponseBody: | ||
return StreamingResponseBody(self.generate_bytes()) | ||
|
||
def generate_bytes(self) -> bytes: | ||
for i in range(0, 5): | ||
yield f"test-{i}\r\n".encode("utf-8") | ||
time.sleep(0.1) | ||
|
||
@POST | ||
def post_stream(self, body: StreamingRequestBody) -> str: | ||
self.anachronos.store(POST_HTTP_STREAM + f" {body.readall()}") | ||
return "OK" |
Oops, something went wrong.