Repository navigation
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds a new driver,
Stevebauman\Location\Drivers\IpGeolocation, for the IPGeolocation.io IP geolocation API. It follows the same pattern as the existingIpData,IpInfoandIp2locationiodrivers: it extendsHttpDriver, builds a URL, and maps the JSON response ontoPosition.Nothing existing changes. The driver is opt-in, it is not added to the default
driverorfallbacks, and no dependencies are added.Files changed
src/Drivers/IpGeolocation.phptests/Drivers/IpGeolocationTest.phpconfig/location.phpipgeolocation.tokenentry, read fromIPGEOLOCATION_TOKENreadme.mdHow the driver calls the API
The driver sends one GET request per lookup through the package's existing HTTP client, so the
location.httptimeout options andHttpDriver::resolveHttpBy()apply as usual:apiKey: the value oflocation.ipgeolocation.token.ip: the IP being looked up (IPv4 or IPv6). The query string is built withhttp_build_query(), so IPv6 addresses are encoded correctly.fields: limits the response to the objects the driver maps. It keeps the payload small and doesn't change the price: the base lookup costs 1 credit either way.fieldsis available on every plan, including the free one.The driver doesn't use the API's optional paid modules (
include=security,abuseand so on), so it works the same on free and paid keys.Field mapping
PositionpropertycountryNamelocation.country_nameUnited StatescountryCodelocation.country_code2USregionNamelocation.state_provCaliforniaregionCodelocation.state_codeUS-CAcityNamelocation.cityMountain ViewzipCodelocation.zipcode94043-1351postalCodelocation.zipcode94043-1351latitudelocation.latitude37.42240longitudelocation.longitude-122.08421timezonetime_zone.nameAmerica/Los_AngelescurrencyCodecurrency.codeUSDNotes on the mapping:
regionCodeis passed through exactly as the API returns it, an ISO 3166-2 subdivision code that includes the country prefix (US-CA,SE-AB). Some other drivers return only the subdivision part (CA). I kept the API's value as-is so it isn't lossy, and I'm happy to change it if you prefer the short form for consistency.zipCodeandpostalCodeboth getlocation.zipcode, the same way theIpDatadriver fills both from one field.latitudeandlongitudecome back from the API as strings, so they are assigned directly to the string-typedPositionproperties without casting.isoCode,metroCodeandareaCodeare leftnull: the API has no matching field.null(every read uses?? null).Error handling
There is no special error handling: the driver relies on
HttpDriver::process(). Any non-2xx response makes the driver returnfalse, so the next fallback driver is tried. For this API that covers:10.0.0.1Configuration and usage
Get a free API key at https://app.ipgeolocation.io/login.
Add it to
.env:Use the driver as the default or as a fallback in
config/location.php:The new config entry follows the existing convention (
token, the same key name theipdata,ipinfoandip2locationioentries use):Tests
tests/Drivers/IpGeolocationTest.phpadds 3 tests:process()returns a v3 response, and the test asserts the fullPosition::toArray()output, in the same style as the other driver tests.fieldsparameter.Http::fake()returns HTTP 423 (bogon IP), and the test asserts thatLocation::get()returnsfalse.Results:
vendor/bin/pest: 38 passed (35 existing + 3 new).pint --teston the changed files: passes.8.8.8.8. It returned the values in the "Example" column of the mapping table above. That check isn't part of the test suite, because it needs a key.Disclosure
I work at IPGeolocation.io. We will keep this driver in step with the API and respond to issues about it.