httpea


Namehttpea JSON
Version 1.0.0 PyPI version JSON
download
home_pageNone
SummaryHttpea is a python library for abstracting http protocol components.
upload_time2024-07-01 06:11:07
maintainerNone
docs_urlNone
authorDawid Kraczkowski
requires_python<4.0,>=3.8
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Httpea

Httpea is an abstraction library which provides a set of classes that represent different parts of the http communication process. These classes can be used to build custom http clients and servers, or to extend existing ones.

The idea behind this library is to provide an interface for manipulating http requests and responses in a way that is easy to use and understand.

## Features
- HttpRequest and HttpResponse classes for representing http requests and responses
- Multipart body parsing for handling file uploads
- Fast and efficient parsing and building of http messages from scratch
- Fast routing and router for your application
- Query string parsing
- Cookie parsing and building

## Installation

You can install Httpea using pip:
```bash
pip install httpea
```

,or with poetry:
```bash
poetry add httpea
```

## Usage

This is an example guide of how basic usage of the library looks like. More detailed examples can be found in tests.

### HttpRequest

HttpRequest objects can be compared, they support query string parsing, cookies, headers and body parsing, including multipart body parsing.

```python
from httpea import HttpRequest, HttpCookie

request = HttpRequest(HttpRequest.GET, "/" ,query_string="name=John&age=30")
# set headers
request.headers["Content-Type"] = "application/json"
# set body
request.body.write(b"Hello, World!")
# set cookies
request.cookies["session"] = "1234567890"

assert isinstance(request.cookies["session"], HttpCookie)
assert request.query_string["name"] == "John"
assert request.query_string["age"] == 30
assert str(request.query_string) == "name=John&age=30"
```

### HttpResponse

Same like HttpRequest, HttpResponse objects can be compared, they support cookies, headers and body writing.

```python
from httpea import HttpResponse, HttpCookie, HttpStatus

response = HttpResponse("Example response", HttpStatus.OK)

# write body
response.write(b"Hello, World!")

# set cookies
response.cookies.append(HttpCookie("cookie-name", "cookie-value", secure=True, http_only=True))
```

### Route and Router

```python
from httpea import Route

route = Route("/example/{pattern}")
result = route.match("/example/test")

assert result
assert result["pattern"] == "test"
assert not route.match("/example")

# wildcard route
route = Route("/example/*")
assert route.match("/example/test")
assert route.match("/example/test/123")
assert not route.match("/invalid")
```

```python
from httpea import Router, HttpRequest, HttpNotFoundError

router = Router()
router.append("/users/{user_id}", lambda request, response, user_id: response.write(user_id), HttpRequest.GET)

try:
    route, callback = router.match("/users/123", HttpRequest.GET)
except HttpNotFoundError:
    pass

```




            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "httpea",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "Dawid Kraczkowski",
    "author_email": "dawid.kraczkowski@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/30/fc/2be3a1e1843daccb44c5ff93257c95d356fd7d31961edb307a30b14cfd3d/httpea-1.0.0.tar.gz",
    "platform": null,
    "description": "# Httpea\n\nHttpea is an abstraction library which provides a set of classes that represent different parts of the http communication process. These classes can be used to build custom http clients and servers, or to extend existing ones.\n\nThe idea behind this library is to provide an interface for manipulating http requests and responses in a way that is easy to use and understand.\n\n## Features\n- HttpRequest and HttpResponse classes for representing http requests and responses\n- Multipart body parsing for handling file uploads\n- Fast and efficient parsing and building of http messages from scratch\n- Fast routing and router for your application\n- Query string parsing\n- Cookie parsing and building\n\n## Installation\n\nYou can install Httpea using pip:\n```bash\npip install httpea\n```\n\n,or with poetry:\n```bash\npoetry add httpea\n```\n\n## Usage\n\nThis is an example guide of how basic usage of the library looks like. More detailed examples can be found in tests.\n\n### HttpRequest\n\nHttpRequest objects can be compared, they support query string parsing, cookies, headers and body parsing, including multipart body parsing.\n\n```python\nfrom httpea import HttpRequest, HttpCookie\n\nrequest = HttpRequest(HttpRequest.GET, \"/\" ,query_string=\"name=John&age=30\")\n# set headers\nrequest.headers[\"Content-Type\"] = \"application/json\"\n# set body\nrequest.body.write(b\"Hello, World!\")\n# set cookies\nrequest.cookies[\"session\"] = \"1234567890\"\n\nassert isinstance(request.cookies[\"session\"], HttpCookie)\nassert request.query_string[\"name\"] == \"John\"\nassert request.query_string[\"age\"] == 30\nassert str(request.query_string) == \"name=John&age=30\"\n```\n\n### HttpResponse\n\nSame like HttpRequest, HttpResponse objects can be compared, they support cookies, headers and body writing.\n\n```python\nfrom httpea import HttpResponse, HttpCookie, HttpStatus\n\nresponse = HttpResponse(\"Example response\", HttpStatus.OK)\n\n# write body\nresponse.write(b\"Hello, World!\")\n\n# set cookies\nresponse.cookies.append(HttpCookie(\"cookie-name\", \"cookie-value\", secure=True, http_only=True))\n```\n\n### Route and Router\n\n```python\nfrom httpea import Route\n\nroute = Route(\"/example/{pattern}\")\nresult = route.match(\"/example/test\")\n\nassert result\nassert result[\"pattern\"] == \"test\"\nassert not route.match(\"/example\")\n\n# wildcard route\nroute = Route(\"/example/*\")\nassert route.match(\"/example/test\")\nassert route.match(\"/example/test/123\")\nassert not route.match(\"/invalid\")\n```\n\n```python\nfrom httpea import Router, HttpRequest, HttpNotFoundError\n\nrouter = Router()\nrouter.append(\"/users/{user_id}\", lambda request, response, user_id: response.write(user_id), HttpRequest.GET)\n\ntry:\n    route, callback = router.match(\"/users/123\", HttpRequest.GET)\nexcept HttpNotFoundError:\n    pass\n\n```\n\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Httpea is a python library for abstracting http protocol components.",
    "version": "1.0.0",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f6fe6432ec5f6030077af1b265ed2256613239b813c86af8d426ecc2ff5f188a",
                "md5": "9a778b11cb6146642575e46cde1fa189",
                "sha256": "a88778a016396be16f8acd7281d418e4d68dae9c2e354f6082aed792048fc0e6"
            },
            "downloads": -1,
            "filename": "httpea-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9a778b11cb6146642575e46cde1fa189",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.8",
            "size": 19260,
            "upload_time": "2024-07-01T06:11:05",
            "upload_time_iso_8601": "2024-07-01T06:11:05.482915Z",
            "url": "https://files.pythonhosted.org/packages/f6/fe/6432ec5f6030077af1b265ed2256613239b813c86af8d426ecc2ff5f188a/httpea-1.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "30fc2be3a1e1843daccb44c5ff93257c95d356fd7d31961edb307a30b14cfd3d",
                "md5": "6f8dce51bd44b3076b80d16c0f7ad4ee",
                "sha256": "8c224cf5233aedb4024f293d2b0045438233dd1aba70409a236a802b11e10141"
            },
            "downloads": -1,
            "filename": "httpea-1.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "6f8dce51bd44b3076b80d16c0f7ad4ee",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.8",
            "size": 15425,
            "upload_time": "2024-07-01T06:11:07",
            "upload_time_iso_8601": "2024-07-01T06:11:07.457492Z",
            "url": "https://files.pythonhosted.org/packages/30/fc/2be3a1e1843daccb44c5ff93257c95d356fd7d31961edb307a30b14cfd3d/httpea-1.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-01 06:11:07",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "httpea"
}
        
Elapsed time: 1.79573s