You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+61
Original file line number
Diff line number
Diff line change
@@ -81,6 +81,67 @@ Nested request parameters are [TypedDicts](https://docs.python.org/3/library/typ
81
81
82
82
Typed requests and responses provide autocomplete and documentation within your editor. If you would like to see type errors in VS Code to help catch bugs earlier, set `python.analysis.typeCheckingMode` to `basic`.
83
83
84
+
## Pagination
85
+
86
+
List methods in the Cloudflare API are paginated.
87
+
88
+
This library provides auto-paginating iterators with each list response, so you do not have to request successive pages manually:
89
+
90
+
```python
91
+
import cloudflare
92
+
93
+
client = Cloudflare()
94
+
95
+
all_accounts = []
96
+
# Automatically fetches more pages as needed.
97
+
for account in client.accounts.list():
98
+
# Do something with account here
99
+
all_accounts.append(account)
100
+
print(all_accounts)
101
+
```
102
+
103
+
Or, asynchronously:
104
+
105
+
```python
106
+
import asyncio
107
+
import cloudflare
108
+
109
+
client = AsyncCloudflare()
110
+
111
+
112
+
asyncdefmain() -> None:
113
+
all_accounts = []
114
+
# Iterate through items across all pages, issuing requests as needed.
115
+
asyncfor account in client.accounts.list():
116
+
all_accounts.append(account)
117
+
print(all_accounts)
118
+
119
+
120
+
asyncio.run(main())
121
+
```
122
+
123
+
Alternatively, you can use the `.has_next_page()`, `.next_page_info()`, or `.get_next_page()` methods for more granular control working with pages:
124
+
125
+
```python
126
+
first_page =await client.accounts.list()
127
+
if first_page.has_next_page():
128
+
print(f"will fetch next page using these details: {first_page.next_page_info()}")
129
+
next_page =await first_page.get_next_page()
130
+
print(f"number of items we just fetched: {len(next_page.result)}")
131
+
132
+
# Remove `await` for non-async usage.
133
+
```
134
+
135
+
Or just work directly with the returned data:
136
+
137
+
```python
138
+
first_page =await client.accounts.list()
139
+
for account in first_page.result:
140
+
print(account)
141
+
142
+
# Remove `await` for non-async usage.
143
+
```
144
+
84
145
## Handling errors
85
146
86
147
When the library is unable to connect to the API (for example, due to network connection problems or a timeout), a subclass of `cloudflare.APIConnectionError` is raised.
0 commit comments