1+ """
2+ GeoIP2 WebService Client API
3+ ============================
4+
5+ This class provides a client API for all the GeoIP Precision web service's end
6+ points. The end points are Country, City, City/ISP/Org, and Omni. Each end
7+ point returns a different set of data about an IP address, with Country
8+ returning the least data and Omni the most.
9+
10+ Each web service end point is represented by a different model class, and
11+ these model classes in turn contain multiple Record classes. The record
12+ classes have attributes which contain data about the IP address.
13+
14+ If the web service does not return a particular piece of data for an IP
15+ address, the associated attribute is not populated.
16+
17+ The web service may not return any information for an entire record, in which
18+ case all of the attributes for that record class will be empty.
19+
20+ SSL
21+ ---
22+
23+ Requests to the GeoIP Precision web service are always made with SSL.
24+
25+ Usage
26+ -----
27+
28+ The basic API for this class is the same for all of the web service end
29+ points. First you create a web service object with your MaxMind C<user_id> and
30+ C<license_key>, then you call the method corresponding to a specific end
31+ point, passing it the IP address you want to look up.
32+
33+ If the request succeeds, the method call will return a model class for the end
34+ point you called. This model in turn contains multiple record classes, each of
35+ which represents part of the data returned by the web service.
36+
37+ If the request fails, the client class throws an exception.
38+
39+ Requirements
40+ ------------
41+
42+ This library works on Python 2.6+ and Python 3. It requires that the requests
43+ HTTP library is installed. See <http://python-requests.org> for details.
44+
45+ Example
46+ -------
47+
48+ >>> import geoip2.webservices
49+ >>> client = geoip2.webservices.Client(42, 'abcdef123456')
50+ >>> omni = client.omni('24.24.24.24')
51+ >>> country = omni.country
52+ >>> print(country.iso_3166_alpha_2)
53+
54+ Exceptions
55+ ----------
56+
57+ For details on the possible errors returned by the web service itself, see
58+ http://dev.maxmind.com/geoip/precision for the GeoIP Precision web service
59+ docs.
60+
61+ If the web service returns an explicit error document, this is thrown as a
62+ GeoIP2WebServiceError exception. If some other sort of error occurs, this is
63+ thrown as a GeoIP2HTTPError. The difference is that the webservice error
64+ includes an error message and error code delivered by the web service. The
65+ latter is thrown when some sort of unanticipated error occurs, such as the
66+ web service returning a 500 or an invalid error document.
67+
68+ If the web service returns any status code besides 200, 4xx, or 5xx, this also
69+ becomes a GeoIP2HTTPError.
70+
71+ Finally, if the web service returns a 200 but the body is invalid, the client
72+ throws a GeoIP2Error object.
73+
74+ What data is returned?
75+ ----------------------
76+
77+ While many of the end points return the same basic records, the attributes
78+ which can be populated vary between end points. In addition, while an end
79+ point may offer a particular piece of data, MaxMind does not always have every
80+ piece of data for any given IP address.
81+
82+ Because of these factors, it is possible for any end point to return a record
83+ where some or all of the attributes are unpopulated.
84+
85+ See http://dev.maxmind.com/geoip/precision for details on what data each end
86+ point may return.
87+
88+ The only piece of data which is always returned is the ip_address key in
89+ the geoip.records.Traits record.
90+
91+ Every record class attribute has a corresponding predicate method so you can
92+ check to see if the attribute is set.
93+
94+ """
195import geoip2 .models
296import requests
397from .errors import GeoIP2Error , GeoIP2HTTPError , GeoIP2WebServiceError
498
599
6100class Client (object ):
101+ """This method creates a new client object.
102+
103+ It accepts the following required arguments:
104+
105+ :param user_id: Your MaxMind User ID.
106+ :param license_key: Your MaxMind license key.
107+
108+ Go to https://www.maxmind.com/en/my_license_key to see your MaxMind
109+ User ID and license key.
110+
111+ The following keyword arguments are also accepted:
112+
113+ :param host: The hostname to make a request against. This defaults to
114+ "geoip.maxmind.com". In most cases, you should not need to set this
115+ explicitly.
116+
117+ :param languages: This is list of language codes. This argument will be
118+ passed onto record classes to use when their name properties are
119+ called. The default value is ['en'].
120+
121+ Details on language handling:
122+
123+ The order of the languages is significant. When a record class has
124+ multiple names (country, city, etc.), its name property will return
125+ the name in the first language that has one.
126+
127+ Note that the only language which is always present in the GeoIP2
128+ Precision data in "en". If you do not include this language, the
129+ name property may end up returning None even when the record hass
130+ an English name.
131+
132+ Currently, the valid list of language codes is:
133+
134+ en -- English names may still include accented characters if that is
135+ the accepted spelling in English. In other words, English does not
136+ mean ASCII.
137+ ja -- Japanese
138+ ru -- Russian
139+ zh-CN -- Simplified Chinese.
140+
141+ Passing any other language code will result in an error.
142+
143+ """
144+
7145 def __init__ (self , user_id , license_key , host = 'geoip.maxmind.com' ,
8146 languages = None ):
9147 if languages is None :
@@ -14,15 +152,43 @@ def __init__(self, user_id, license_key, host='geoip.maxmind.com',
14152 self ._base_uri = 'https://%s/geoip' % (host )
15153
16154 def city (self , ip = 'me' ):
155+ """This method calls the GeoIP2 Precision City endpoint.
156+
157+ :param ip:IPv4 or IPv6 address as a string. If no address is provided,
158+ the address that the web service is called from will be used.
159+ :returns: geoip2.models.City object
160+
161+ """
17162 return self ._response_for ('city' , geoip2 .models .City , ip )
18163
19164 def city_isp_org (self , ip = 'me' ):
165+ """This method calls the GeoIP2 Precision City/ISP/Org endpoint.
166+
167+ :param ip: IPv4 or IPv6 address as a string. If no address is provided,
168+ the address that the web service is called from will be used.
169+ :returns: geoip2.models.CityISPOrg object
170+
171+ """
20172 return self ._response_for ('city_isp_org' , geoip2 .models .CityISPOrg , ip )
21173
22174 def country (self , ip = 'me' ):
175+ """This method calls the GeoIP2 Country endpoint.
176+
177+ :param ip: IPv4 or IPv6 address as a string. If no address is provided,
178+ the address that the web service is called from will be used.
179+ :returns: geoip2.models.Country object
180+
181+ """
23182 return self ._response_for ('country' , geoip2 .models .Country , ip )
24183
25184 def omni (self , ip = 'me' ):
185+ """This method calls the GeoIP2 Precision Omni endpoint.
186+
187+ :param ip: IPv4 or IPv6 address as a string. If no address is provided,
188+ the address that the web service is called from will be used.
189+ :returns: geoip2.models.Omni object
190+
191+ """
26192 return self ._response_for ('omni' , geoip2 .models .Omni , ip )
27193
28194 def _response_for (self , path , model_class , ip ):
@@ -84,3 +250,9 @@ def _handle_non_200_status(self, response, status, uri):
84250 raise GeoIP2HTTPError ('Received a very surprising HTTP status '
85251 '(%(status)i) for %(uri)s' % locals (), status ,
86252 uri )
253+ """
254+
255+ :copyright: (c) 2013 by MaxMind, Inc.
256+ :license: LGPL 2.1 or greater, see LICENSE for more details.
257+
258+ """
0 commit comments