Skip to content

Commit

Permalink
feat(network): add HttpMultiPart class
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Nov 5, 2020
1 parent f145094 commit 087582e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions prettyqt/network/__init__.py
Expand Up @@ -9,10 +9,12 @@
from .networkcookiejar import NetworkCookieJar
from .networkrequest import NetworkRequest
from .httppart import HttpPart
from .httpmultipart import HttpMultiPart
from .networkaccessmanager import NetworkAccessManager

__all__ = [
"HttpPart",
"HttpMultiPart",
"NetworkCookie",
"NetworkCookieJar",
"NetworkRequest",
Expand Down
52 changes: 52 additions & 0 deletions prettyqt/network/httpmultipart.py
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-

from typing import Union

from qtpy import QtNetwork, QtCore

from prettyqt import core
from prettyqt.utils import bidict, InvalidParamError

CONTENT_TYPES = bidict(
mixed=QtNetwork.QHttpMultiPart.MixedType,
related=QtNetwork.QHttpMultiPart.RelatedType,
form=QtNetwork.QHttpMultiPart.FormDataType,
alternative=QtNetwork.QHttpMultiPart.AlternativeType,
)

QtNetwork.QHttpMultiPart.__bases__ = (core.Object,)


class HttpMultiPart(QtNetwork.QHttpMultiPart):
def __add__(self, other: QtNetwork.QHttpPart):
self.append(other)
return self

def set_content_type(self, typ: str):
"""Set content type.
Valid values: "mixed", "related", "form", "alternative"
Args:
typ: content type
Raises:
InvalidParamError: content type does not exist
"""
if typ not in CONTENT_TYPES:
raise InvalidParamError(typ, CONTENT_TYPES)
self.setContentType(CONTENT_TYPES[typ])

def set_boundary(self, boundary: Union[str, bytes, QtCore.QByteArray]):
if isinstance(boundary, str):
boundary = boundary.encode()
if isinstance(boundary, bytes):
boundary = QtCore.QByteArray(boundary)
self.setBoundary(boundary)

def get_boundary(self) -> str:
return bytes(self.boundary()).decode()


if __name__ == "__main__":
part = HttpMultiPart()
9 changes: 9 additions & 0 deletions tests/test_network.py
Expand Up @@ -15,6 +15,15 @@ def test_httppart():
part.set_header("location", "c")


def test_httpmultipart():
part = network.HttpMultiPart()
part.set_content_type("related")
with pytest.raises(InvalidParamError):
part.set_content_type("test")
part.set_boundary("test")
assert part.get_boundary() == "test"


def test_networkrequest():
req = network.NetworkRequest()
headers = {"a": "b"}
Expand Down

0 comments on commit 087582e

Please sign in to comment.