Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,11 @@ interface IpDataLookup {
* @return the set of properties this lookup will provide
*/
Set<Database.Property> getProperties();

/**
* A helper record that holds other records. Every ip data lookup will have an associated ip address that was looked up, as well
* as a network for which the record applies. Having a helper record prevents each individual response record from needing to
* track these bits of information.
*/
record Result<T>(T result, String ip, String network) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -264,15 +264,15 @@ static class Asn extends AbstractBase<AsnResult> {

@Override
protected Map<String, Object> transform(final Result<AsnResult> result) {
AsnResult response = result.result;
AsnResult response = result.result();
Long asn = response.asn;
String organizationName = response.name;
String network = result.network;
String network = result.network();

Map<String, Object> data = new HashMap<>();
for (Database.Property property : this.properties) {
switch (property) {
case IP -> data.put("ip", result.ip);
case IP -> data.put("ip", result.ip());
case ASN -> {
if (asn != null) {
data.put("asn", asn);
Expand Down Expand Up @@ -316,12 +316,12 @@ static class Country extends AbstractBase<CountryResult> {

@Override
protected Map<String, Object> transform(final Result<CountryResult> result) {
CountryResult response = result.result;
CountryResult response = result.result();

Map<String, Object> data = new HashMap<>();
for (Database.Property property : this.properties) {
switch (property) {
case IP -> data.put("ip", result.ip);
case IP -> data.put("ip", result.ip());
case COUNTRY_ISO_CODE -> {
String countryIsoCode = response.country;
if (countryIsoCode != null) {
Expand Down Expand Up @@ -359,12 +359,12 @@ static class Geolocation extends AbstractBase<GeolocationResult> {

@Override
protected Map<String, Object> transform(final Result<GeolocationResult> result) {
GeolocationResult response = result.result;
GeolocationResult response = result.result();

Map<String, Object> data = new HashMap<>();
for (Database.Property property : this.properties) {
switch (property) {
case IP -> data.put("ip", result.ip);
case IP -> data.put("ip", result.ip());
case COUNTRY_ISO_CODE -> {
String countryIsoCode = response.country;
if (countryIsoCode != null) {
Expand Down Expand Up @@ -418,12 +418,12 @@ static class PrivacyDetection extends AbstractBase<PrivacyDetectionResult> {

@Override
protected Map<String, Object> transform(final Result<PrivacyDetectionResult> result) {
PrivacyDetectionResult response = result.result;
PrivacyDetectionResult response = result.result();

Map<String, Object> data = new HashMap<>();
for (Database.Property property : this.properties) {
switch (property) {
case IP -> data.put("ip", result.ip);
case IP -> data.put("ip", result.ip());
case HOSTING -> {
if (response.hosting != null) {
data.put("hosting", response.hosting);
Expand Down Expand Up @@ -460,16 +460,9 @@ protected Map<String, Object> transform(final Result<PrivacyDetectionResult> res
}
}

/**
* Just a little record holder -- there's the data that we receive via the binding to our record objects from the Reader via the
* getRecord call, but then we also need to capture the passed-in ip address that came from the caller as well as the network for
* the returned DatabaseRecord from the Reader.
*/
public record Result<T>(T result, String ip, String network) {}

/**
* The {@link IpinfoIpDataLookups.AbstractBase} is an abstract base implementation of {@link IpDataLookup} that
* provides common functionality for getting a {@link IpinfoIpDataLookups.Result} that wraps a record from a {@link IpDatabase}.
* provides common functionality for getting a {@link IpDataLookup.Result} that wraps a record from a {@link IpDatabase}.
*
* @param <RESPONSE> the record type that will be wrapped and returned
*/
Expand All @@ -491,7 +484,7 @@ public Set<Database.Property> getProperties() {
@Override
public final Map<String, Object> getData(final IpDatabase ipDatabase, final String ipAddress) {
final Result<RESPONSE> response = ipDatabase.getResponse(ipAddress, this::lookup);
return (response == null || response.result == null) ? Map.of() : transform(response);
return (response == null || response.result() == null) ? Map.of() : transform(response);
}

@Nullable
Expand Down
Loading