html-css-to-image 0.2.0__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (18) hide show
  1. {html_css_to_image-0.2.0 → html_css_to_image-0.3.0}/PKG-INFO +1 -1
  2. {html_css_to_image-0.2.0 → html_css_to_image-0.3.0}/pyproject.toml +1 -1
  3. {html_css_to_image-0.2.0 → html_css_to_image-0.3.0}/src/html_css_to_image/__init__.py +6 -0
  4. {html_css_to_image-0.2.0 → html_css_to_image-0.3.0}/src/html_css_to_image/_request_mapper.py +23 -0
  5. {html_css_to_image-0.2.0 → html_css_to_image-0.3.0}/src/html_css_to_image/models.py +41 -1
  6. {html_css_to_image-0.2.0 → html_css_to_image-0.3.0}/tests/test_client.py +29 -0
  7. {html_css_to_image-0.2.0 → html_css_to_image-0.3.0}/.github/workflows/ci.yml +0 -0
  8. {html_css_to_image-0.2.0 → html_css_to_image-0.3.0}/.github/workflows/publish.yml +0 -0
  9. {html_css_to_image-0.2.0 → html_css_to_image-0.3.0}/.gitignore +0 -0
  10. {html_css_to_image-0.2.0 → html_css_to_image-0.3.0}/LICENSE +0 -0
  11. {html_css_to_image-0.2.0 → html_css_to_image-0.3.0}/README.md +0 -0
  12. {html_css_to_image-0.2.0 → html_css_to_image-0.3.0}/src/html_css_to_image/_response_mapper.py +0 -0
  13. {html_css_to_image-0.2.0 → html_css_to_image-0.3.0}/src/html_css_to_image/_transport.py +0 -0
  14. {html_css_to_image-0.2.0 → html_css_to_image-0.3.0}/src/html_css_to_image/_url_builder.py +0 -0
  15. {html_css_to_image-0.2.0 → html_css_to_image-0.3.0}/src/html_css_to_image/_version.py +0 -0
  16. {html_css_to_image-0.2.0 → html_css_to_image-0.3.0}/src/html_css_to_image/client.py +0 -0
  17. {html_css_to_image-0.2.0 → html_css_to_image-0.3.0}/src/html_css_to_image/exceptions.py +0 -0
  18. {html_css_to_image-0.2.0 → html_css_to_image-0.3.0}/src/html_css_to_image/py.typed +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: html-css-to-image
3
- Version: 0.2.0
3
+ Version: 0.3.0
4
4
  Summary: Official Python client for the HTML/CSS to Image API
5
5
  Project-URL: Homepage, https://htmlcsstoimage.com
6
6
  Project-URL: Documentation, https://docs.htmlcsstoimage.com
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "html-css-to-image"
7
- version = "0.2.0"
7
+ version = "0.3.0"
8
8
  description = "Official Python client for the HTML/CSS to Image API"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -34,6 +34,9 @@ from .models import (
34
34
  RenderImageFormat,
35
35
  RenderImageOptions,
36
36
  RenderImageValueUnit,
37
+ RequestOverride,
38
+ RequestOverrideAction,
39
+ RequestOverrideResourceType,
37
40
  ValidationError,
38
41
  )
39
42
 
@@ -69,6 +72,9 @@ __all__ = [
69
72
  "RenderImageFormat",
70
73
  "RenderImageOptions",
71
74
  "RenderImageValueUnit",
75
+ "RequestOverride",
76
+ "RequestOverrideAction",
77
+ "RequestOverrideResourceType",
72
78
  "UnexpectedResponseError",
73
79
  "ValidationError",
74
80
  "__version__",
@@ -15,6 +15,7 @@ from .models import (
15
15
  PDFOptions,
16
16
  PDFValueInput,
17
17
  PDFValueWithUnits,
18
+ RequestOverride,
18
19
  )
19
20
 
20
21
 
@@ -110,6 +111,10 @@ class RequestMapper:
110
111
  in_batch: bool,
111
112
  ) -> dict[str, Any]:
112
113
  payload = cls.common_payload(request, include_dedupe=not in_batch)
114
+ if request.request_overrides is not None:
115
+ payload["request_overrides"] = cls._map_request_overrides(
116
+ request.request_overrides
117
+ )
113
118
  payload["html"] = request.html or None if in_batch else request.html
114
119
  payload["css"] = request.css
115
120
 
@@ -132,6 +137,10 @@ class RequestMapper:
132
137
  in_batch: bool,
133
138
  ) -> dict[str, Any]:
134
139
  payload = cls.common_payload(request, include_dedupe=not in_batch)
140
+ if request.request_overrides is not None:
141
+ payload["request_overrides"] = cls._map_request_overrides(
142
+ request.request_overrides
143
+ )
135
144
  payload.update(
136
145
  {
137
146
  "url": request.url or None if in_batch else request.url,
@@ -154,6 +163,20 @@ class RequestMapper:
154
163
  )
155
164
  return cls.without_none(payload)
156
165
 
166
+ @staticmethod
167
+ def _map_request_overrides(rules: list[RequestOverride]) -> list[dict[str, Any]]:
168
+ return [
169
+ RequestMapper.without_none({
170
+ "action": rule.action.value,
171
+ "url": rule.url,
172
+ "resource_types": (
173
+ [resource_type.value for resource_type in rule.resource_types]
174
+ if rule.resource_types is not None else None
175
+ ),
176
+ })
177
+ for rule in rules
178
+ ]
179
+
157
180
  @classmethod
158
181
  def _map_pdf_options(
159
182
  cls,
@@ -4,6 +4,7 @@ from __future__ import annotations
4
4
 
5
5
  from collections.abc import Mapping
6
6
  from dataclasses import dataclass
7
+ from enum import Enum
7
8
  from typing import Any, Literal, TypeAlias
8
9
 
9
10
  ColorSchemeType: TypeAlias = Literal["light", "dark"]
@@ -15,6 +16,42 @@ RenderImageFormat: TypeAlias = ImageFormat
15
16
  RenderImageValueUnit: TypeAlias = Literal["px", "%"]
16
17
 
17
18
 
19
+ class RequestOverrideResourceType(str, Enum):
20
+ """Browser resource types supported by a request override rule."""
21
+
22
+ BEACON = "beacon"
23
+ DOCUMENT = "document"
24
+ STYLESHEET = "stylesheet"
25
+ IMAGE = "image"
26
+ IMAGE_SET = "image_set"
27
+ MEDIA = "media"
28
+ FONT = "font"
29
+ SCRIPT = "script"
30
+ TEXT_TRACK = "text_track"
31
+ XHR = "xhr"
32
+ FETCH = "fetch"
33
+ EVENT_SOURCE = "event_source"
34
+ MANIFEST = "manifest"
35
+ PING = "ping"
36
+ IMG = "img"
37
+ OTHER = "other"
38
+
39
+
40
+ class RequestOverrideAction(str, Enum):
41
+ """Action applied to a matching browser request."""
42
+
43
+ BLOCK = "block"
44
+
45
+
46
+ @dataclass(slots=True)
47
+ class RequestOverride:
48
+ """Block requests matching a URL wildcard and/or resource types."""
49
+
50
+ action: RequestOverrideAction = RequestOverrideAction.BLOCK
51
+ url: str | None = None
52
+ resource_types: list[RequestOverrideResourceType] | None = None
53
+
54
+
18
55
  def _validate_string_collection(
19
56
  name: str,
20
57
  value: list[str] | tuple[str, ...] | None,
@@ -99,7 +136,8 @@ class BaseCreateImageRequest:
99
136
  ms_delay: Additional delay before capture, in milliseconds.
100
137
  render_when_ready: Wait for ``ScreenshotReady()`` in page JavaScript.
101
138
  max_render_once: Ensure that the image is rendered and saved once.
102
- disable_twemoji: Disable the Twemoji fallback renderer.
139
+ disable_twemoji: HTML/CSS images use Twemoji by default; True disables it.
140
+ URL images inject Twemoji only with explicit False.
103
141
  color_scheme: Emulate the ``light`` or ``dark`` color scheme.
104
142
  timezone: IANA timezone name used by the browser.
105
143
  viewport_mobile: Emulate a mobile viewport.
@@ -107,6 +145,7 @@ class BaseCreateImageRequest:
107
145
  viewport_landscape: Render the viewport in landscape orientation.
108
146
  media_type: Emulate ``print`` or ``screen`` CSS media.
109
147
  proxy_id: Organization proxy identifier used for rendering.
148
+ request_overrides: Browser network request block rules; paid plans only.
110
149
  jumbo_max_width: Maximum width for jumbo rendering.
111
150
  jumbo_max_height: Maximum height for jumbo rendering.
112
151
  dedupe_duration_s: Reuse a matching recently generated image for up
@@ -134,6 +173,7 @@ class BaseCreateImageRequest:
134
173
  viewport_landscape: bool | None = None
135
174
  media_type: MediaType | None = None
136
175
  proxy_id: str | None = None
176
+ request_overrides: list[RequestOverride] | None = None
137
177
  jumbo_max_width: int | None = None
138
178
  jumbo_max_height: int | None = None
139
179
  dedupe_duration_s: int | None = None
@@ -26,6 +26,9 @@ from html_css_to_image import (
26
26
  RenderImageCropSize,
27
27
  RenderImageCropSpan,
28
28
  RenderImageOptions,
29
+ RequestOverride,
30
+ RequestOverrideAction,
31
+ RequestOverrideResourceType,
29
32
  UnexpectedResponseError,
30
33
  __version__,
31
34
  )
@@ -45,6 +48,32 @@ class HtmlCssToImageClientTests(unittest.TestCase):
45
48
  http_client=http_client,
46
49
  )
47
50
 
51
+ def test_request_overrides_serialize_enums_and_stay_out_of_signed_urls(self):
52
+ rules = [RequestOverride(
53
+ action=RequestOverrideAction.BLOCK,
54
+ url="*.js",
55
+ resource_types=[
56
+ RequestOverrideResourceType.SCRIPT,
57
+ RequestOverrideResourceType.FETCH,
58
+ ],
59
+ )]
60
+
61
+ def handler(request: httpx.Request) -> httpx.Response:
62
+ payload = json.loads(request.content)
63
+ self.assertEqual(payload["request_overrides"], [{
64
+ "action": "block", "url": "*.js", "resource_types": ["script", "fetch"],
65
+ }])
66
+ return httpx.Response(200, json={"id": "123", "url": "image-url"})
67
+
68
+ client = self.make_client(handler)
69
+ client.create_image(
70
+ CreateUrlImageRequest(url="https://example.com", request_overrides=rules)
71
+ )
72
+ signed = client.generate_create_and_render_url(
73
+ CreateUrlImageRequest(url="https://example.com", request_overrides=rules)
74
+ )
75
+ self.assertNotIn("request_overrides", signed)
76
+
48
77
  def test_create_image_maps_html_css_fonts_pdf_and_auth(self):
49
78
  def handler(request: httpx.Request) -> httpx.Response:
50
79
  self.assertEqual(request.url, "https://hcti.io/v1/image")