Skip to content
This repository was archived by the owner on Jun 29, 2022. It is now read-only.

Commit c72dd4b

Browse files
authored
Merge pull request #21 from Stewart86/beta-2
Beta 2 release
2 parents 10be86b + 0ed95da commit c72dd4b

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

README.rst

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ Feature Roadmap
5757

5858
* to support cert token
5959
* to support sync API client
60-
* to get to 100% test coverage
60+
61+
These are some alternative use cases that are not in the top of my priority now as I have not received
62+
any request for. If you are interested, you may want to submit a pull request to contribute some of these features.
6163

6264

6365
Requirements
@@ -85,20 +87,46 @@ Usage
8587
8688
async def get_zone():
8789
async with Cloudflare() as cf:
88-
result = await cf.zones.get()
90+
response = await cf.zones.get()
8991
90-
Full configuration can be done using `Config()` class.
92+
Unlike the offical ``python-cloudflare`` library, ``aiocloudflare`` does not parse and handle http responses.
93+
94+
So the awaited response object will have to be handled just as any http request, response pattern. the ``Response`` object is the same as ``httpx``'s ``Response``.
95+
96+
.. code:: Python
97+
98+
from aiocloudflare import Cloudflare
99+
100+
async def get_zone():
101+
async with Cloudflare() as cf:
102+
response = await cf.zones.get()
103+
104+
# check status code
105+
if response.status_code == 200:
106+
107+
# get json data
108+
resp_json = response.json()
109+
110+
# Cloudflare API typically store results in a ``result`` key.
111+
return resp_json["result"]
112+
113+
else:
114+
# to get texture data from response
115+
print(response.text)
116+
117+
118+
Full configuration can be done using ``Config()`` class.
91119

92120
.. code:: Python
93121
94122
from aioCloudflare import Cloudflare, Config
95123
96-
config = Config(email="[email protected]", token="<secret>")
124+
config = Config(email="[email protected]", token="<secret>") # for demo only, do not hardcode secrets
97125
async def get_zone():
98126
async with Cloudflare(config=config) as cf:
99127
result = await cf.zones.get()
100128
101-
Configuration can also be stored in a ``.env`` file for a "global configuration without needing to create a ``Config()`` class. Keys available are:
129+
Configuration can also be stored in a ``.env`` file for a "global" configuration without needing to create a ``Config()`` class. Keys available are:
102130

103131
.. code:: console
104132
@@ -110,6 +138,31 @@ Configuration can also be stored in a ``.env`` file for a "global configuration
110138
CF_PROFILE=""
111139
USER_AGENT=""
112140
141+
Advance Usage
142+
_____________
143+
144+
You may wish to wrap ``Cloudflare()`` into you own class for customised settings or requirements. To do that, just provide a ``__aenter__()`` and ``__aexit__()`` method to your class like so.
145+
146+
.. code:: Python
147+
148+
class MyCfClient:
149+
def __init__(self):
150+
self._config = Config(email="[email protected]", token="<secret>") # for demo only, do not hardcode secrets
151+
152+
async def __aenter__(self):
153+
self._client = Cloudflare(config=self._config)
154+
return self
155+
156+
async def __aexit__(self, exc_type, exc_value, traceback):
157+
await self._client.aclose()
158+
159+
Then you can call your own class with async context manager.
160+
161+
.. code:: Python
162+
163+
async with MyCfClient() as own_class:
164+
await own_class.zones.get()
165+
113166
114167
Contributing
115168
------------

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "aiocloudflare"
3-
version = "0.2.0-beta-1"
3+
version = "0.2.0-beta-2"
44
description = "A Cloudflare API wrapper for Python with asyncio support"
55
authors = ["Stewart Wong <[email protected]>"]
66
license = "MIT"

0 commit comments

Comments
 (0)