Skip to content

Commit 6ece7a0

Browse files
committed
Add AsyncClient example
1 parent 0e35405 commit 6ece7a0

1 file changed

Lines changed: 61 additions & 16 deletions

File tree

README.rst

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ each of which represents part of the data returned by the web service.
5757

5858
If the request fails, the client class throws an exception.
5959

60-
Web Service Example
61-
-------------------
60+
Sync Web Service Example
61+
------------------------
6262

6363
.. code-block:: pycon
6464
@@ -67,36 +67,81 @@ Web Service Example
6767
>>> # This creates a Client object that can be reused across requests.
6868
>>> # Replace "42" with your account ID and "license_key" with your license
6969
>>> # key.
70-
>>> client = geoip2.webservice.Client(42, 'license_key')
70+
>>> with geoip2.webservice.Client(42, 'license_key') as client:
7171
>>>
72-
>>> # Replace "insights" with the method corresponding to the web service
73-
>>> # that you are using, e.g., "country", "city".
74-
>>> response = client.insights('128.101.101.101')
72+
>>> # Replace "insights" with the method corresponding to the web service
73+
>>> # that you are using, e.g., "country", "city".
74+
>>> response = client.insights('128.101.101.101')
7575
>>>
76-
>>> response.country.iso_code
76+
>>> response.country.iso_code
7777
'US'
78-
>>> response.country.name
78+
>>> response.country.name
7979
'United States'
80-
>>> response.country.names['zh-CN']
80+
>>> response.country.names['zh-CN']
8181
u'美国'
8282
>>>
83-
>>> response.subdivisions.most_specific.name
83+
>>> response.subdivisions.most_specific.name
8484
'Minnesota'
85-
>>> response.subdivisions.most_specific.iso_code
85+
>>> response.subdivisions.most_specific.iso_code
8686
'MN'
8787
>>>
88-
>>> response.city.name
88+
>>> response.city.name
8989
'Minneapolis'
9090
>>>
91-
>>> response.postal.code
91+
>>> response.postal.code
9292
'55455'
9393
>>>
94-
>>> response.location.latitude
94+
>>> response.location.latitude
9595
44.9733
96-
>>> response.location.longitude
96+
>>> response.location.longitude
9797
-93.2323
9898
>>>
99-
>>> response.traits.network
99+
>>> response.traits.network
100+
IPv4Network('128.101.101.101/32')
101+
102+
Async Web Service Example
103+
------------------------
104+
105+
.. code-block:: pycon
106+
107+
>>> import geoip2.webservice
108+
>>>
109+
>>> # This creates an AsyncClient object that can be reused across
110+
>>> # requests on the running event loop. If you are using multiple event
111+
>>> # loops, you must ensure the object is not used on another loop.
112+
>>> #
113+
>>> # Replace "42" with your account ID and "license_key" with your license
114+
>>> # key.
115+
>>> async with geoip2.webservice.AsyncClient(42, 'license_key') as client:
116+
>>>
117+
>>> # Replace "insights" with the method corresponding to the web service
118+
>>> # that you are using, e.g., "country", "city".
119+
>>> response = await client.insights('128.101.101.101')
120+
>>>
121+
>>> response.country.iso_code
122+
'US'
123+
>>> response.country.name
124+
'United States'
125+
>>> response.country.names['zh-CN']
126+
u'美国'
127+
>>>
128+
>>> response.subdivisions.most_specific.name
129+
'Minnesota'
130+
>>> response.subdivisions.most_specific.iso_code
131+
'MN'
132+
>>>
133+
>>> response.city.name
134+
'Minneapolis'
135+
>>>
136+
>>> response.postal.code
137+
'55455'
138+
>>>
139+
>>> response.location.latitude
140+
44.9733
141+
>>> response.location.longitude
142+
-93.2323
143+
>>>
144+
>>> response.traits.network
100145
IPv4Network('128.101.101.101/32')
101146
102147
Web Service Client Exceptions

0 commit comments

Comments
 (0)