Skip to content

Commit 1a51641

Browse files
committed
chore: Add parentRequestId to server().request method
1 parent f5a545c commit 1a51641

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

‎docs/02_concepts/14_server_actors.mdx‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ The headers you pass in `headers` reach the Actor's server as they are. If the A
7878

7979
For details, see [How do I authenticate my requests](https://docs.apify.com/platform/actors/running/standby#how-do-i-authenticate-my-requests) in the platform documentation.
8080

81+
## Calling a Server Actor from a Server Actor
82+
83+
A multi-tenant Server Actor shares the developer's runs among its users, so the platform tells requests apart by an ID it sends in the `X-Actor-Request-ID` header of each request it forwards to the Actor. The Actor uses that ID to charge the request's caller, and to pass the caller's pricing tier on to Actors it calls.
84+
85+
When your multi-tenant Server Actor calls another one while handling a request, pass the incoming `X-Actor-Request-ID` header in `parent_request_id`. The client sends it as the same header, so the called Actor charges at the pricing tier of your incoming request. Use a client created with the run's `APIFY_TOKEN`. For details, see [Compose multi-tenant Server Actors](https://docs.apify.com/platform/actors/development/programming-interface/standby#compose-server-actors).
86+
87+
```python
88+
server_client = client.actor('username/other-actor').server()
89+
response = await server_client.request(
90+
'/search',
91+
params={'q': 'apify'},
92+
parent_request_id=request.headers['x-actor-request-id'],
93+
)
94+
```
95+
8196
## Timeouts, retries, and errors
8297

8398
When no server run is ready, the first request waits for the platform to start one. The method uses the `long` timeout tier by default for that reason. You can pass a different `timeout`, the same way as for API calls. For details, see [Timeouts](./11_timeouts.mdx).

‎src/apify_client/_resource_clients/actor_server.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ def request(
159159
headers: dict[str, str] | None = None,
160160
data: str | bytes | None = None,
161161
json: JsonSerializable | None = None,
162+
parent_request_id: str | None = None,
162163
compression: HttpCompressionAlgorithm | HttpCompressor | None = None,
163164
stream: bool = False,
164165
timeout: Timeout = 'long',
@@ -190,6 +191,8 @@ def request(
190191
headers: Additional headers of the request.
191192
data: Raw request body. Cannot be used together with `json`.
192193
json: JSON-serializable request body. Cannot be used together with `data`.
194+
parent_request_id: The `X-Actor-Request-ID` header of the request your Server Actor is handling, sent on
195+
as the same header so the called Actor charges at that request's pricing tier.
193196
compression: Compression algorithm for the request body, as a string literal or an `HttpCompressor`
194197
instance. The body is compressed under the same conditions as for API calls. Set it only if the
195198
Actor's server decodes the resulting `Content-Encoding`.
@@ -208,6 +211,8 @@ def request(
208211
"""
209212
server_url = self.get_url()
210213
server_path = path.partition('#')[0].lstrip('/')
214+
if parent_request_id is not None:
215+
headers = self._http_client._merge_headers(headers, {'X-Actor-Request-ID': parent_request_id}) # noqa: SLF001
211216
return self._http_client.call(
212217
url=f'{server_url.rstrip("/")}/{server_path}',
213218
method=method,
@@ -355,6 +360,7 @@ async def request(
355360
headers: dict[str, str] | None = None,
356361
data: str | bytes | None = None,
357362
json: JsonSerializable | None = None,
363+
parent_request_id: str | None = None,
358364
compression: HttpCompressionAlgorithm | HttpCompressor | None = None,
359365
stream: bool = False,
360366
timeout: Timeout = 'long',
@@ -386,6 +392,8 @@ async def request(
386392
headers: Additional headers of the request.
387393
data: Raw request body. Cannot be used together with `json`.
388394
json: JSON-serializable request body. Cannot be used together with `data`.
395+
parent_request_id: The `X-Actor-Request-ID` header of the request your Server Actor is handling, sent on
396+
as the same header so the called Actor charges at that request's pricing tier.
389397
compression: Compression algorithm for the request body, as a string literal or an `HttpCompressor`
390398
instance. The body is compressed under the same conditions as for API calls. Set it only if the
391399
Actor's server decodes the resulting `Content-Encoding`.
@@ -404,6 +412,8 @@ async def request(
404412
"""
405413
server_url = await self.get_url()
406414
server_path = path.partition('#')[0].lstrip('/')
415+
if parent_request_id is not None:
416+
headers = self._http_client._merge_headers(headers, {'X-Actor-Request-ID': parent_request_id}) # noqa: SLF001
407417
return await self._http_client.call(
408418
url=f'{server_url.rstrip("/")}/{server_path}',
409419
method=method,

‎tests/unit/test_actor_server.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ def test_request_sends_caller_authorization_header(httpserver: HTTPServer, sync_
112112
assert captured[0].headers['X-Apify-Authorization'] == 'Bearer test'
113113

114114

115+
def test_request_sends_parent_request_id_header(httpserver: HTTPServer, sync_client: ApifyClient) -> None:
116+
"""`parent_request_id` goes out as the `X-Actor-Request-ID` header, over one passed in `headers`."""
117+
expect_server_actor(httpserver)
118+
httpserver.expect_request(f'{_SERVER_PREFIX}/', method='GET').respond_with_json({'ok': True})
119+
120+
sync_client.actor(_MOCKED_ACTOR_ID).server().request(
121+
headers={'x-actor-request-id': 'stale'}, parent_request_id='parent-id'
122+
)
123+
124+
captured = server_requests(httpserver)
125+
assert captured[0].headers.getlist('X-Actor-Request-ID') == ['parent-id']
126+
127+
115128
def test_request_fetches_server_url_once(httpserver: HTTPServer, sync_client: ApifyClient) -> None:
116129
"""Later calls on the same client reuse the server URL fetched by the first one."""
117130
expect_server_actor(httpserver)

0 commit comments

Comments
 (0)