|
| 1 | +-module(ipinfo_plus). |
| 2 | +-include_lib("kernel/include/logger.hrl"). |
| 3 | + |
| 4 | +-export([ |
| 5 | + '__struct__'/0, |
| 6 | + '__struct__'/1 |
| 7 | +]). |
| 8 | + |
| 9 | +-export([ |
| 10 | + create/0, |
| 11 | + create/1, |
| 12 | + create/2, |
| 13 | + details/1, |
| 14 | + details/2 |
| 15 | +]). |
| 16 | + |
| 17 | +-define(DEFAULT_COUNTRY_FILE, "countries.json"). |
| 18 | +-define(DEFAULT_EU_COUNTRY_FILE, "eu.json"). |
| 19 | +-define(DEFAULT_COUNTRY_FLAG_FILE, "flags.json"). |
| 20 | +-define(DEFAULT_COUNTRY_CURRENCY_FILE, "currency.json"). |
| 21 | +-define(DEFAULT_CONTINENT_FILE, "continent.json"). |
| 22 | +-define(DEFAULT_COUNTRY_FLAG_BASE_URL, |
| 23 | + <<"https://cdn.ipinfo.io/static/images/countries-flags/">>). |
| 24 | +-define(DEFAULT_BASE_URL, <<"https://api.ipinfo.io/lookup">>). |
| 25 | +-define(DEFAULT_TIMEOUT, timer:seconds(5)). |
| 26 | +-define(DEFAULT_CACHE_TTL_SECONDS, (24 * 60 * 60)). |
| 27 | + |
| 28 | +-export_type([t/0]). |
| 29 | + |
| 30 | +-type t() :: #{ |
| 31 | + '__struct__' := ?MODULE, |
| 32 | + access_token := nil | binary(), |
| 33 | + base_url := nil | binary(), |
| 34 | + timeout := nil | timeout(), |
| 35 | + cache := nil | pid(), |
| 36 | + countries := map(), |
| 37 | + countries_flags := map(), |
| 38 | + country_flag_base_url := nil | binary(), |
| 39 | + countries_currencies := map(), |
| 40 | + continents := map(), |
| 41 | + eu_countries := list() |
| 42 | +}. |
| 43 | + |
| 44 | +-spec new() -> t(). |
| 45 | +%% @private |
| 46 | +new() -> |
| 47 | + #{ |
| 48 | + '__struct__' => ?MODULE, |
| 49 | + access_token => nil, |
| 50 | + base_url => nil, |
| 51 | + timeout => nil, |
| 52 | + cache => nil, |
| 53 | + countries => #{}, |
| 54 | + countries_currencies => #{}, |
| 55 | + countries_flags => #{}, |
| 56 | + country_flag_base_url => nil, |
| 57 | + continents => #{}, |
| 58 | + eu_countries => [] |
| 59 | + }. |
| 60 | + |
| 61 | +-spec '__struct__'() -> t(). |
| 62 | +%% @private |
| 63 | +'__struct__'() -> |
| 64 | + new(). |
| 65 | + |
| 66 | +-spec '__struct__'(From :: list() | map()) -> t(). |
| 67 | +%% @private |
| 68 | +'__struct__'(From) -> |
| 69 | + new(From). |
| 70 | + |
| 71 | +-spec new(From :: list() | map()) -> t(). |
| 72 | +%% @private |
| 73 | +new(List) when is_list(List) -> |
| 74 | + new(maps:from_list(List)); |
| 75 | +new(Map) when is_map(Map) -> |
| 76 | + maps:fold(fun maps:update/3, new(), Map). |
| 77 | + |
| 78 | +create() -> |
| 79 | + create(application:get_env(ipinfo, access_token, nil)). |
| 80 | + |
| 81 | +create(AccessToken) when is_list(AccessToken) -> |
| 82 | + create(list_to_binary(AccessToken)); |
| 83 | +create(AccessToken) -> |
| 84 | + create(AccessToken, []). |
| 85 | + |
| 86 | +-spec create(AccessToken, Settings) -> Result when |
| 87 | + AccessToken :: binary() | nil, |
| 88 | + Settings :: proplists:proplist(), |
| 89 | + Result :: {ok, t()} | {error, term()}. |
| 90 | +create(AccessToken, Settings) -> |
| 91 | + CountriesFile = get_config(countries, Settings, |
| 92 | + filename:join(code:priv_dir(ipinfo), ?DEFAULT_COUNTRY_FILE)), |
| 93 | + EuCountriesFile = get_config(eu_countries, Settings, |
| 94 | + filename:join(code:priv_dir(ipinfo), ?DEFAULT_EU_COUNTRY_FILE)), |
| 95 | + CountriesFlagsFile = get_config(countries_flags, Settings, |
| 96 | + filename:join(code:priv_dir(ipinfo), ?DEFAULT_COUNTRY_FLAG_FILE)), |
| 97 | + CountriesCurrenciesFile = get_config(countries_currencies, Settings, |
| 98 | + filename:join(code:priv_dir(ipinfo), ?DEFAULT_COUNTRY_CURRENCY_FILE)), |
| 99 | + ContinentsFile = get_config(continents, Settings, |
| 100 | + filename:join(code:priv_dir(ipinfo), ?DEFAULT_CONTINENT_FILE)), |
| 101 | + CountryFlagBaseUrl = get_config(country_flag_base_url, Settings, |
| 102 | + ?DEFAULT_COUNTRY_FLAG_BASE_URL), |
| 103 | + BaseUrl = get_config(base_url, Settings, ?DEFAULT_BASE_URL), |
| 104 | + Timeout = get_config(timeout, Settings, ?DEFAULT_TIMEOUT), |
| 105 | + CacheTtl = get_config(cache_ttl, Settings, ?DEFAULT_CACHE_TTL_SECONDS), |
| 106 | + create_with_files(AccessToken, BaseUrl, Timeout, CacheTtl, CountriesFile, |
| 107 | + EuCountriesFile, CountriesFlagsFile, CountriesCurrenciesFile, |
| 108 | + ContinentsFile, CountryFlagBaseUrl). |
| 109 | + |
| 110 | +create_with_files(AccessToken, BaseUrl, Timeout, CacheTtl, CountriesFile, |
| 111 | + EuCountriesFile, CountriesFlagsFile, CountriesCurrenciesFile, |
| 112 | + ContinentsFile, CountryFlagBaseUrl) -> |
| 113 | + Files = [ |
| 114 | + CountriesFile, |
| 115 | + EuCountriesFile, |
| 116 | + CountriesFlagsFile, |
| 117 | + CountriesCurrenciesFile, |
| 118 | + ContinentsFile |
| 119 | + ], |
| 120 | + case read_json_files(Files) of |
| 121 | + {ok, [Countries, EuCountries, CountriesFlags, CountriesCurrencies, Continents]} -> |
| 122 | + create_ipinfo_plus_struct( |
| 123 | + AccessToken, |
| 124 | + BaseUrl, |
| 125 | + Timeout, |
| 126 | + CacheTtl, |
| 127 | + Countries, |
| 128 | + EuCountries, |
| 129 | + CountriesFlags, |
| 130 | + CountriesCurrencies, |
| 131 | + Continents, |
| 132 | + CountryFlagBaseUrl |
| 133 | + ); |
| 134 | + {error, Reason} -> |
| 135 | + {error, Reason} |
| 136 | + end. |
| 137 | + |
| 138 | +read_json_files(Files) -> |
| 139 | + read_json_files(Files, []). |
| 140 | + |
| 141 | +read_json_files([], Acc) -> |
| 142 | + {ok, lists:reverse(Acc)}; |
| 143 | +read_json_files([File | Rest], Acc) -> |
| 144 | + case read_json(File) of |
| 145 | + {ok, Data} -> |
| 146 | + read_json_files(Rest, [Data | Acc]); |
| 147 | + {error, Reason} -> |
| 148 | + {error, Reason} |
| 149 | + end. |
| 150 | + |
| 151 | +create_ipinfo_plus_struct(AccessToken, BaseUrl, Timeout, CacheTtl, Countries, |
| 152 | + EuCountries, CountriesFlags, CountriesCurrencies, Continents, |
| 153 | + CountryFlagBaseUrl) -> |
| 154 | + {ok, Cache} = ipinfo_cache:create(CacheTtl), |
| 155 | + {ok, new(#{ |
| 156 | + access_token => AccessToken, |
| 157 | + base_url => BaseUrl, |
| 158 | + timeout => Timeout, |
| 159 | + cache => Cache, |
| 160 | + countries => Countries, |
| 161 | + eu_countries => EuCountries, |
| 162 | + countries_flags => CountriesFlags, |
| 163 | + country_flag_base_url => CountryFlagBaseUrl, |
| 164 | + countries_currencies => CountriesCurrencies, |
| 165 | + continents => Continents |
| 166 | + })}. |
| 167 | + |
| 168 | +details(IpInfoPlus) -> |
| 169 | + details(IpInfoPlus, nil). |
| 170 | + |
| 171 | +details(#{cache := Cache, |
| 172 | + countries := Countries, |
| 173 | + eu_countries := EuCountries, |
| 174 | + countries_flags := CountriesFlags, |
| 175 | + country_flag_base_url:= CountryFlagBaseUrl, |
| 176 | + countries_currencies := CountriesCurrencies, |
| 177 | + continents := Continents |
| 178 | +} = IpInfo, IpAddress) -> |
| 179 | + ActualIpAddress = case IpAddress of |
| 180 | + nil -> <<"me">>; |
| 181 | + _ -> IpAddress |
| 182 | + end, |
| 183 | + case get_details(Cache, IpInfo, ActualIpAddress) of |
| 184 | + {ok, Details} -> |
| 185 | + {ok, enrich_details(Details, Countries, EuCountries, |
| 186 | + CountriesFlags, CountryFlagBaseUrl, CountriesCurrencies, |
| 187 | + Continents)}; |
| 188 | + {error, Reason} -> |
| 189 | + {error, Reason} |
| 190 | + end. |
| 191 | + |
| 192 | +get_details(Cache, IpInfo, IpAddress) -> |
| 193 | + case ipinfo_cache:get(Cache, IpAddress) of |
| 194 | + {ok, Details} -> |
| 195 | + {ok, Details}; |
| 196 | + error -> |
| 197 | + case ipinfo_http:request_details(IpInfo, IpAddress) of |
| 198 | + {ok, Details} -> |
| 199 | + ok = ipinfo_cache:add(Cache, IpAddress, Details), |
| 200 | + {ok, Details}; |
| 201 | + {error, Reason} -> |
| 202 | + {error, Reason} |
| 203 | + end |
| 204 | + end. |
| 205 | + |
| 206 | +enrich_details(Details, Countries, EuCountries, CountriesFlags, |
| 207 | + CountryFlagBaseUrl, CountriesCurrencies, Continents) -> |
| 208 | + Enrichers = [ |
| 209 | + fun enrich_geo/1, |
| 210 | + fun(D) -> put_geo_enrichments(D, Countries, EuCountries, CountriesFlags, |
| 211 | + CountryFlagBaseUrl, CountriesCurrencies, Continents) end |
| 212 | + ], |
| 213 | + lists:foldl(fun(F, Acc) -> F(Acc) end, Details, Enrichers). |
| 214 | + |
| 215 | +enrich_geo(#{geo := Geo} = Details) when is_map(Geo) -> |
| 216 | + Details; |
| 217 | +enrich_geo(Details) -> |
| 218 | + Details. |
| 219 | + |
| 220 | +put_geo_enrichments(#{geo := #{country_code := CountryCode} = Geo} = Details, |
| 221 | + Countries, EuCountries, CountriesFlags, CountryFlagBaseUrl, |
| 222 | + CountriesCurrencies, Continents) -> |
| 223 | + EnrichedGeo = Geo#{ |
| 224 | + country_name => maps:get(CountryCode, Countries, CountryCode), |
| 225 | + is_eu => lists:member(CountryCode, EuCountries), |
| 226 | + country_flag => maps:get(CountryCode, CountriesFlags, #{}), |
| 227 | + country_currency => maps:get(CountryCode, CountriesCurrencies, #{}), |
| 228 | + continent => maps:get(CountryCode, Continents, #{}), |
| 229 | + country_flag_url => <<CountryFlagBaseUrl/binary, CountryCode/binary, ".svg">> |
| 230 | + }, |
| 231 | + Details#{geo => EnrichedGeo}; |
| 232 | +put_geo_enrichments(Details, _Countries, _EuCountries, _CountriesFlags, |
| 233 | + _CountryFlagBaseUrl, _CountriesCurrencies, _Continents) -> |
| 234 | + Details. |
| 235 | + |
| 236 | +get_config(Key, Settings, Default) -> |
| 237 | + proplists:get_value(Key, Settings, |
| 238 | + application:get_env(ipinfo, Key, Default)). |
| 239 | + |
| 240 | +read_json(JsonFile) -> |
| 241 | + case file:read_file(JsonFile) of |
| 242 | + {ok, Binary} -> |
| 243 | + case jsx:is_json(Binary) of |
| 244 | + true -> |
| 245 | + {ok, jsx:decode(Binary, [return_maps])}; |
| 246 | + false -> |
| 247 | + {error, invalid_json} |
| 248 | + end; |
| 249 | + {error, Reason} -> |
| 250 | + {error, Reason} |
| 251 | + end. |
0 commit comments