-
Notifications
You must be signed in to change notification settings - Fork 183
Add session.fetch for HTTP requests from the page #943
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
9b45fe6
677102d
307a063
a614c68
418f3a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,65 @@ | ||||||||
| --- | ||||||||
| title: "Response" | ||||||||
| description: "The :class:`Response <Response>` object, which contains a" | ||||||||
| --- | ||||||||
|
|
||||||||
|
|
||||||||
| server's response to an HTTP request | ||||||||
|
|
||||||||
| ## Methods | ||||||||
|
|
||||||||
| ### close | ||||||||
|
|
||||||||
| ```python | ||||||||
| close() | ||||||||
| ``` | ||||||||
|
|
||||||||
| Releases the connection back to the pool | ||||||||
|
|
||||||||
| --- | ||||||||
|
|
||||||||
| ### iter_content | ||||||||
|
|
||||||||
| ```python | ||||||||
| iter_content(chunk_size = 1, decode_unicode = False) | ||||||||
| ``` | ||||||||
|
|
||||||||
| Iterates over the response data | ||||||||
|
|
||||||||
| --- | ||||||||
|
|
||||||||
| ### iter_lines | ||||||||
|
|
||||||||
| ```python | ||||||||
| iter_lines(chunk_size = 512, decode_unicode = False, delimiter = None) | ||||||||
| ``` | ||||||||
|
|
||||||||
| Iterates over the response data, one line at a time | ||||||||
|
|
||||||||
| --- | ||||||||
|
|
||||||||
| ### json | ||||||||
|
|
||||||||
| ```python | ||||||||
| json(kwargs) | ||||||||
| ``` | ||||||||
|
Comment on lines
+44
to
+45
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: #!/bin/bash
set -euo pipefail
fd -HI -t f 'pyproject\.toml|setup\.py|requirements.*' . \
--exec rg -n 'requests' {} \; || true
python - <<'PY'
import inspect
import requests
print(requests.__version__)
print(inspect.signature(requests.Response.json))
PYRepository: nottelabs/notte Length of output: 220 Pass decoder options as keyword arguments to
Proposed fix-json(kwargs)
+json(**kwargs)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||
|
|
||||||||
| Decodes the JSON response body (if any) as a Python object | ||||||||
|
|
||||||||
| --- | ||||||||
|
|
||||||||
| ### raise_for_status | ||||||||
|
|
||||||||
| ```python | ||||||||
| raise_for_status() | ||||||||
| ``` | ||||||||
|
|
||||||||
| Raises :class:`HTTPError`, if one occurred | ||||||||
|
|
||||||||
| --- | ||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
| ## Module | ||||||||
|
|
||||||||
| `requests.models` | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| --- | ||
| title: "fetch" | ||
| description: "Issue an HTTP request from the page the session is on and return the response" | ||
| --- | ||
| import AgentMdNotice from '/partials/agent-md-notice.mdx'; | ||
|
|
||
| <AgentMdNotice /> | ||
|
|
||
| The request runs inside the browser through `fetch()`, so it carries the | ||
| page's cookies, the session's proxy and the browser's own network | ||
| fingerprint. A relative `url` resolves against the current page, which | ||
| also makes it same-origin; a cross-origin URL is subject to CORS exactly | ||
| as in a browser tab, so `goto` the target origin first. Redirects are | ||
| followed and the final URL is on `response.url`. The result is a standard | ||
| `requests.Response`: a non-2xx status is returned, not raised, and | ||
| `response.raise_for_status()` raises `requests.HTTPError`. A network | ||
| failure surfaces as the JavaScript error. | ||
|
|
||
| `json` is serialised as the body with an `application/json` content type, | ||
| `data` as a form body when it is a mapping or verbatim when it is a string. | ||
|
|
||
| ```python | ||
| session.execute(type="goto", url="https://en.wikipedia.org/wiki/Main_Page") | ||
| summary = session.fetch("/api/rest_v1/page/summary/Main_Page").json() | ||
| ``` | ||
|
|
||
|
|
||
| ## Parameters | ||
|
|
||
| <ParamField path="url" type="str" required> | ||
| </ParamField> | ||
|
|
||
| <ParamField path="method" type="str" default="GET"> | ||
| </ParamField> | ||
|
|
||
| <ParamField path="headers" type="UnionType[Mapping[str, str], None]" default="None"> | ||
| </ParamField> | ||
|
|
||
| <ParamField path="params" type="UnionType[Mapping[str, Any], None]" default="None"> | ||
| </ParamField> | ||
|
|
||
| <ParamField path="json" type="Any" default="None"> | ||
| </ParamField> | ||
|
|
||
| <ParamField path="data" type="UnionType[str, Mapping[str, Any], None]" default="None"> | ||
| </ParamField> | ||
|
|
||
| <ParamField path="timeout" type="UnionType[float, None]" default="None"> | ||
| </ParamField> | ||
|
|
||
| ## Returns | ||
|
|
||
| <Visibility for="humans">[`Response`](/sdk-reference/misc/response)</Visibility><Visibility for="agents">[`Response`](/sdk-reference/misc/response.md)</Visibility> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| {/* Auto-generated mdx file. Do not edit! */} | ||
| {/* @sniptest testers/browser-controls/fetch.py */} | ||
|
|
||
| ```python fetch.py | ||
| from notte_sdk import NotteClient | ||
|
|
||
| client = NotteClient() | ||
|
|
||
| with client.Session() as session: | ||
| session.execute(type="goto", url="https://en.wikipedia.org/wiki/Main_Page") | ||
| # fetch runs inside the page: same cookies, proxy and fingerprint as the browser | ||
| response = session.fetch("/api/rest_v1/page/summary/Main_Page") | ||
| response.raise_for_status() | ||
| summary = response.json() | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # @sniptest filename=fetch.py | ||
| from notte_sdk import NotteClient | ||
|
|
||
| client = NotteClient() | ||
|
|
||
| with client.Session() as session: | ||
| session.execute(type="goto", url="https://en.wikipedia.org/wiki/Main_Page") | ||
| # fetch runs inside the page: same cookies, proxy and fingerprint as the browser | ||
| response = session.fetch("/api/rest_v1/page/summary/Main_Page") | ||
| response.raise_for_status() | ||
| summary = response.json() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Complete the page description.
The frontmatter description ends with
"which contains a". The following text is outside the frontmatter, so generated metadata remains incomplete. Put the full sentence indescriptionand remove or relocate the body fragment.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents