generated from jCloud/repository-template
Move make_request function to a separate utils module
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
# Copyright 2026 jCloud
|
||||
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from starlette.requests import Request
|
||||
|
||||
__all__ = [
|
||||
'call_next',
|
||||
'make_request'
|
||||
]
|
||||
|
||||
class _DummyResponse:
|
||||
def __init__(self):
|
||||
self.called = True
|
||||
|
||||
async def call_next(request):
|
||||
return _DummyResponse()
|
||||
|
||||
def make_request(body: bytes, path: str, headers: dict = None) -> Request:
|
||||
'''
|
||||
Makes a request.
|
||||
|
||||
:param body: The request body.
|
||||
:type body: bytes
|
||||
:param path: The path.
|
||||
:type path: str
|
||||
:param headers: The headers.
|
||||
:type headers: dict
|
||||
|
||||
:return: The request.
|
||||
:rtype: Request
|
||||
'''
|
||||
|
||||
scope = {
|
||||
'type': 'http',
|
||||
'method': 'POST',
|
||||
'path': path,
|
||||
'headers': [
|
||||
(k.lower().encode(), v.encode())
|
||||
for k, v in (headers or {}).items()
|
||||
]
|
||||
}
|
||||
|
||||
async def receive():
|
||||
return {
|
||||
'type': 'http.request',
|
||||
'body': body
|
||||
}
|
||||
|
||||
return Request(scope, receive)
|
||||
Reference in New Issue
Block a user