Skip to content

Feature/response encoding - #81

Merged
NEFORCEO merged 5 commits into
masterfrom
feature/response-encoding
Jun 21, 2026
Merged

Feature/response encoding#81
NEFORCEO merged 5 commits into
masterfrom
feature/response-encoding

Conversation

@NEFORCEO

Copy link
Copy Markdown
Member

🚀 Description

Add missing httpx.Response metadata properties and default_encoding support to FastHTTP.

Response

  • history — list of intermediate redirect responses
  • elapseddatetime.timedelta with request timing
  • http_version — protocol version string (HTTP/1.1, HTTP/2)
  • reason_phrase — status text (OK, Not Found)
  • is_success, is_redirect, is_error, is_client_error, is_server_error, is_informational — boolean status classifiers
  • raise_for_status() — raises FastHTTPBadStatusError on 4xx/5xx

FastHTTP

  • default_encoding parameter — controls response text decoding when charset is missing
  • Type: DefaultEncoding (Literal[...]) | Callable[[bytes], str]
  • Reusable type alias DefaultEncoding extracted to fasthttp/types.py

🧩 Type of Change

  • feat: New feature

✅ Checklist

  • Tests added or updated
  • Documentation updated
  • No breaking changes
  • Code follows project style

🔗 Related Issues


📸 Additional Context

All 106 existing tests pass.

Usage examples

from fasthttp import FastHTTP
from fasthttp.response import Response
from fasthttp.types import DefaultEncoding

# default_encoding
app = FastHTTP(default_encoding="cp1251")

# or custom decoder
app = FastHTTP(
    default_encoding=lambda b: "utf-8" if b[:3] == b"\xef\xbb\xbf" else "cp1251"
)

# response metadata
@app.get("https://httpbin.org/status/200")
async def example(resp: Response):
    print(resp.elapsed)          # datetime.timedelta
    print(resp.http_version)     # "HTTP/1.1"
    print(resp.reason_phrase)    # "OK"
    print(resp.history)          # []
    print(resp.is_success)       # True
    print(resp.is_error)         # False

    resp.raise_for_status()      # raises on 4xx/5xx
    return resp.json()

# status classifiers
resp = Response(status=200, text="", headers={})
resp.is_success        # True
resp.is_redirect       # False
resp.is_client_error   # False
resp.is_server_error   # False
resp.is_informational  # False

@github-actions

Copy link
Copy Markdown
Contributor

👋 Thanks for your contribution, @NEFORCEO!

Please make sure:

  • Code is clean and formatted
  • Tests are passing
  • PR description is clear

Maintainers will review it soon.

@fasthttp-bot fasthttp-bot added dependencies Pull requests that update a dependency file feature labels Jun 21, 2026
@codspeed-hq

codspeed-hq Bot commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by 36.99%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 7 improved benchmarks
✅ 5 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
test_response_req_text_from_json 213.2 µs 134.8 µs +58.16%
test_response_repr 196.4 µs 132.1 µs +48.65%
test_response_property_access 194.5 µs 131.9 µs +47.43%
test_response_json_parsing 205.6 µs 141.2 µs +45.53%
test_response_creation 201.3 µs 140 µs +43.8%
test_middleware_after_response 403.1 µs 355.6 µs +13.35%
test_response_json_parsing_large_payload 666.7 µs 605.6 µs +10.09%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing feature/response-encoding (de2855b) with master (6f3d441)1

Open in CodSpeed

Footnotes

  1. No successful run was found on master (fed2357) during the generation of this report, so 6f3d441 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@NEFORCEO
NEFORCEO merged commit caaf1a1 into master Jun 21, 2026
17 checks passed
@NEFORCEO
NEFORCEO deleted the feature/response-encoding branch June 21, 2026 12:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Pull requests that update a dependency file feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants