Skip to content

Commit a52c4a4

Browse files
authored
Merge pull request #1521 from maxmind/greg/eng-3706
Make code samples more idiomatic, modernize, and fix various issues
2 parents bf029e9 + 1f2429b commit a52c4a4

File tree

4 files changed

+501
-440
lines changed

4 files changed

+501
-440
lines changed

content/geoip/geolocate-an-ip/databases.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ Install-Package MaxMind.GeoIP2
2929
<dependency>
3030
<groupId>com.maxmind.geoip2</groupId>
3131
<artifactId>geoip2</artifactId>
32-
<version>2.15.0</version>
32+
<version>5.0.2</version>
3333
</dependency>
3434

3535
// Or install via Gradle
3636
repositories {
3737
mavenCentral()
3838
}
3939
dependencies {
40-
compile 'com.maxmind.geoip2:geoip2:2.15.0'
40+
implementation 'com.maxmind.geoip2:geoip2:5.0.2'
4141
}
4242
```
4343

@@ -51,7 +51,7 @@ yarn add @maxmind/geoip2-node
5151

5252
```php
5353
# Install via Composer
54-
composer require geoip2/geoip2:~2.0
54+
composer require geoip2/geoip2:~3.0
5555
```
5656

5757
```python
@@ -111,8 +111,8 @@ InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
111111

112112
CityResponse response = reader.city(ipAddress);
113113

114-
Country country = response.getCountry();
115-
System.out.println(country.getIsoCode());
114+
Country country = response.country();
115+
System.out.println(country.isoCode());
116116

117117
```
118118

@@ -136,7 +136,7 @@ const dbBuffer = fs.readFileSync('/path/to/maxmind-database.mmdb');
136136
// expensive.
137137
const reader = Reader.openBuffer(dbBuffer);
138138

139-
response = reader.city('128.101.101.101');
139+
const response = reader.city('128.101.101.101');
140140

141141
console.log(response.country.isoCode);
142142
```
@@ -161,7 +161,7 @@ import geoip2.database
161161
# This reader object should be reused across lookups as creation of it is
162162
# expensive.
163163
with geoip2.database.Reader('/path/to/maxmind-database.mmdb') as reader:
164-
response = reader.city('128.101.101.101');
164+
response = reader.city('128.101.101.101')
165165
print(response.country.iso_code)
166166
```
167167

content/geoip/geolocate-an-ip/web-services.md

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ Install-Package MaxMind.GeoIP2
3232
<dependency>
3333
<groupId>com.maxmind.geoip2</groupId>
3434
<artifactId>geoip2</artifactId>
35-
<version>2.15.0</version>
35+
<version>5.0.2</version>
3636
</dependency>
3737

3838
// Or install via Gradle
3939
repositories {
4040
mavenCentral()
4141
}
4242
dependencies {
43-
compile 'com.maxmind.geoip2:geoip2:2.15.0'
43+
implementation 'com.maxmind.geoip2:geoip2:5.0.2'
4444
}
4545
```
4646

@@ -54,7 +54,7 @@ yarn add @maxmind/geoip2-node
5454

5555
```php
5656
# Install via Composer
57-
composer require geoip2/geoip2:~2.0
57+
composer require geoip2/geoip2:~3.0
5858
```
5959

6060
```python
@@ -122,23 +122,23 @@ const client = new WebServiceClient(accountId, licenseKey, {
122122
```
123123

124124
```php
125-
<?php require_once 'vendor/autoload.php'
125+
<?php
126+
require_once 'vendor/autoload.php';
126127
use GeoIp2\WebService\Client;
127128

128-
$account_id = 10;
129-
$license_key = 'LICENSEKEY';
129+
$accountId = 10;
130+
$licenseKey = 'LICENSEKEY';
130131

131-
$client = new Client($account_id, $license_key);
132+
$client = new Client($accountId, $licenseKey);
132133

133134
// To query the GeoLite web service, you must set the optional `host` argument.
134135
// The third argument specifies the language preferences when using the `->name`
135136
// method on the model classes that this client creates.
136-
$client = new Client($account_id, $license_key, ['en'], ['host' => 'geolite.info']);
137+
$client = new Client($accountId, $licenseKey, ['en'], ['host' => 'geolite.info']);
137138
```
138139

139140
```python
140-
import asyncio # for async requests with AsyncClient
141-
import geoip2.webservice
141+
from geoip2.webservice import Client, AsyncClient
142142

143143
account_id = 10
144144
license_key = 'LICENSEKEY'
@@ -150,7 +150,7 @@ client = Client(account_id, license_key)
150150
# to "geolite.info"
151151
client = Client(account_id, license_key, host='geolite.info')
152152

153-
# Or if you want to use asynchronous requests
153+
# Or if you want to use asynchronous requests (requires `import asyncio`)
154154
async_client = AsyncClient(account_id, license_key)
155155

156156
# To query the GeoLite web service, you must set the "host" keyword argument
@@ -227,10 +227,10 @@ try (WebServiceClient client = new WebServiceClient.Builder(accountId, licenseKe
227227
// `client.insights` is not available to GeoLite users
228228
CountryResponse response = client.country(ipAddress);
229229

230-
Country country = response.getCountry();
231-
System.out.println(country.getIsoCode()); // 'US'
232-
System.out.println(country.getName()); // 'United States'
233-
System.out.println(country.getNames().get("zh-CN")); // '美国'
230+
Country country = response.country();
231+
System.out.println(country.isoCode()); // 'US'
232+
System.out.println(country.name()); // 'United States'
233+
System.out.println(country.names().get("zh-CN")); // '美国'
234234
}
235235
```
236236

@@ -239,6 +239,11 @@ const WebServiceClient = require('@maxmind/geoip2-node').WebServiceClient;
239239
// Typescript:
240240
// import { WebServiceClient } from '@maxmind/geoip2-node';
241241

242+
const accountId = '10';
243+
const licenseKey = 'LICENSEKEY';
244+
245+
const client = new WebServiceClient(accountId, licenseKey);
246+
242247
// You can also use `client.city` or `client.insights`
243248
// `client.insights` is not available to GeoLite users
244249
client.country('142.1.1.1').then((response) => {
@@ -247,13 +252,14 @@ client.country('142.1.1.1').then((response) => {
247252
```
248253

249254
```php
250-
<?php require_once 'vendor/autoload.php'
255+
<?php
256+
require_once 'vendor/autoload.php';
251257
use GeoIp2\WebService\Client;
252258

253-
$account_id = 10;
254-
$license_key = 'LICENSEKEY';
259+
$accountId = 10;
260+
$licenseKey = 'LICENSEKEY';
255261

256-
$client = new Client($account_id, $license_key);
262+
$client = new Client($accountId, $licenseKey);
257263

258264
// You can also use `$client->city` or `$client->insights`
259265
// `$client->insights` is not available to GeoLite users
@@ -264,27 +270,27 @@ print($record->country->isoCode . "\n");
264270

265271
```python
266272
# Sync
267-
import geoip2.webservice
273+
from geoip2.webservice import Client
268274

269275
account_id = 10
270276
license_key = 'LICENSEKEY'
271277

272-
with geoip2.webservice.Client(account_id, license_key) as client:
278+
with Client(account_id, license_key) as client:
273279
# You can also use `client.city` or `client.insights`
274280
# `client.insights` is not available to GeoLite users
275-
response = client.country('128.101.101.101)
281+
response = client.country('128.101.101.101')
276282

277283
print(response.country.iso_code)
278284

279285
# Async
280286
import asyncio
281-
import geoip2.webservice
287+
from geoip2.webservice import AsyncClient
282288

283289
async def main():
284-
async with geoip2.webservice.AsyncClient(account_id, license_key) as client:
290+
async with AsyncClient(account_id, license_key) as client:
285291
# You can also use `client.city` or `client.insights`
286292
# `client.insights` is not available to GeoLite users
287-
response = await client.country('128.101.101.101)
293+
response = await client.country('128.101.101.101')
288294

289295
print(response.country.iso_code)
290296

0 commit comments

Comments
 (0)