reviewboard.hostingsvcs.base.http¶
Base HTTP support for hosting services.
New in version 6.0: This replaces the HTTP code in the old
reviewboard.hostingsvcs.service
module.
- class UploadedFileInfo[source]¶
Bases:
TypedDict
Information on an uploaded file.
New in version 6.0.
- __annotations__ = {'content': ForwardRef('Union[bytes, str]', module='reviewboard.hostingsvcs.base.http'), 'filename': ForwardRef('Union[bytes, str]', module='reviewboard.hostingsvcs.base.http')}¶
- __optional_keys__ = frozenset({})¶
- __orig_bases__ = (<function TypedDict>,)¶
- __required_keys__ = frozenset({'content', 'filename'})¶
- __total__ = True¶
- FormFields¶
Form field data to set when performing a POST or PUT.
New in version 6.0.
- HTTPHeaders¶
Type for a mapping of HTTP headers for a request or response.
New in version 6.0.
- UploadedFiles¶
Form field data to set when performing a POST or PUT.
New in version 6.0.
alias of
Dict
[Union
[bytes
,str
],UploadedFileInfo
]
- class HostingServiceHTTPRequest(url: str, query: Optional[QueryArgs] = None, body: Optional[bytes] = None, headers: Optional[HTTPHeaders] = None, method: str = 'GET', hosting_service: Optional[BaseHostingService] = None, **kwargs)[source]¶
Bases:
object
A request that can use any HTTP method.
This provides some additional type checking and utilities for working with HTTP requests, headers, URL openers, and SSL certification management.
Changed in version 6.0:
Moved from
reviewboard.hostingsvcs.service
toreviewboard.hostingsvcs.base.http
.
New in version 4.0.
- __init__(url: str, query: Optional[QueryArgs] = None, body: Optional[bytes] = None, headers: Optional[HTTPHeaders] = None, method: str = 'GET', hosting_service: Optional[BaseHostingService] = None, **kwargs) None [source]¶
Initialize the request.
- Parameters:
url (
str
) – The URL to make the request against.query (
dict
, optional) –Query arguments to add onto the URL.
These will be mixed with any query arguments already in the URL, and the result will be applied in sorted order, for cross-Python compatibility.
body (
bytes
, optional) – The payload body for the request, if using aPOST
orPUT
request.headers (
dict
, optional) – Additional headers to attach to the request.method (
str
, optional) – The request method. If not provided, it defaults to aGET
request.hosting_service (
reviewboard.hostingsvcs.base.hosting_service.BaseHostingService
, optional) – The hosting service this request is associated with.**kwargs (
dict
, unused) – Additional keyword arguments for the request. This is unused, but allows room for expansion by subclasses.
- hosting_service: Optional[BaseHostingService]¶
The hosting service this request is associated with.
- Type:
HostingService
- property data: Optional[bytes][source]¶
The payload data for the request.
Deprecated since version 4.0: This is deprecated in favor of the
body
attribute.
- get_header(name: str, default: Optional[str] = None) Optional[str] [source]¶
Return a header from the request.
- add_basic_auth(username: Union[bytes, str], password: Union[bytes, str]) None [source]¶
Add HTTP Basic Authentication headers to the request.
- add_digest_auth(username: str, password: str) None [source]¶
Add HTTP Digest Authentication support to the request.
- add_urlopen_handler(handler: BaseHandler) None [source]¶
Add a handler to invoke for the urlopen call.
Note
This is dependent on a
urllib
-backed request. While that is the default today, it may not be in the future. This method should be used with the knowledge that it may someday be deprecated, or may not work at all with special subclasses.- Parameters:
handler (
urllib.request.AbstractHTTPHandler
) – The handler to add.
- open() HostingServiceHTTPResponse [source]¶
Open the request to the server, returning the response.
- Returns:
The response information from the server.
- Return type:
- Raises:
urllib.error.URLError – An error occurred talking to the server, or an HTTP error (400+) was returned.
- __annotations__ = {'_urlopen_handlers': 'List[BaseHandler]', 'body': 'Optional[bytes]', 'headers': 'HTTPHeaders', 'hosting_service': 'Optional[BaseHostingService]', 'method': 'str', 'query': 'Optional[QueryArgs]', 'url': 'str'}¶
- class HostingServiceHTTPResponse(request: HostingServiceHTTPRequest, url: str, data: bytes, headers: Dict[str, str], status_code: int)[source]¶
Bases:
object
An HTTP response from the server.
This stores the URL, payload data, headers, and status code from an HTTP response.
Changed in version 6.0:
Moved from
reviewboard.hostingsvcs.service
toreviewboard.hostingsvcs.base.http
.Deprecated legacy tuple representations of this class now emit deprecation warnings and will be removed in Review Board 7.
New in version 4.0.
- __annotations__ = {'data': 'bytes', 'headers': 'HTTPHeaders', 'request': 'HostingServiceHTTPRequest', 'status_code': 'int', 'url': 'str'}¶
- __init__(request: HostingServiceHTTPRequest, url: str, data: bytes, headers: Dict[str, str], status_code: int) None [source]¶
Initialize the response.
- Parameters:
request (
HostingServiceHTTPRequest
) – The request this is in response to.url (
str
) –The URL serving the response.
If redirected, this may differ from the request URL.
data (
bytes
) – The response payload.headers (
dict
) – The response headers.status_code (
int
) – The response HTTP status code.
- request: HostingServiceHTTPRequest¶
The HTTP request this is in response to.
- headers: Dict[str, str]¶
The HTTP response headers.
It’s recommended to call
get_header()
to request a header.- Type:
- json()[source]¶
A JSON representation of the payload data.
- Raises:
ValueError – The data is not valid JSON.
- get_header(name: str, default: Optional[str] = None) Optional[str] [source]¶
Return the value of a header as a Unicode string.
This accepts a header name with any form of capitalization. The header name will be normalized.
- __getitem__(i: int) Any [source]¶
Return an indexed item from the response.
This is used to emulate the older 2-tuple response returned by hosting service HTTP request methods.
- Parameters:
i (
int
) – The index of the item.- Returns:
The object at the specified index.
If 0, this will return
data
.If 1, this will return
headers
.- Return type:
- Raises:
IndexError – An index other than 0 or 1 was requested.