Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 18 additions & 1 deletion src/ipinfo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
create/1,
create/2,
details/1,
details/2
details/2,
resproxy/2
]).

-define(DEFAULT_COUNTRY_FILE, "countries.json").
Expand Down Expand Up @@ -185,6 +186,22 @@ details(#{cache := Cache,
{error, Reason}
end.

-spec resproxy(t(), binary()) -> {ok, map()} | {error, term()}.
resproxy(#{cache := Cache} = IpInfo, IpAddress) ->
CacheKey = <<"resproxy:", IpAddress/binary>>,
case ipinfo_cache:get(Cache, CacheKey) of
{ok, Details} ->
{ok, Details};
error ->
case ipinfo_http:request_resproxy(IpInfo, IpAddress) of
{ok, Details} ->
ok = ipinfo_cache:add(Cache, CacheKey, Details),
{ok, Details};
{error, Reason} ->
{error, Reason}
end
end.

get_details(Cache, IpInfo, IpAddress) ->
case ipinfo_cache:get(Cache, IpAddress) of
{ok, Details} ->
Expand Down
21 changes: 17 additions & 4 deletions src/ipinfo_http.erl
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
-module(ipinfo_http).
-include_lib("kernel/include/logger.hrl").

-export([request_details/2]).
-export([request_details/2, request_resproxy/2]).

-define(RATE_LIMIT_MESSAGE, <<
"To increase your limits, please review our ",
"paid plans at https://ipinfo.io/pricing"
>>).

-spec request_details(ipinfo:t(), binary()) -> {ok, map()} | {error, term()}.
request_details(#{access_token := AccessToken, base_url := BaseUrl, timeout := Timeout}, Ip) ->
Url = binary_to_list(build_url(BaseUrl, Ip)),
request_details(#{base_url := BaseUrl} = Config, Ip) ->
Url = build_url(BaseUrl, Ip),
do_request(Config, Url).

-spec request_resproxy(ipinfo:t(), binary()) -> {ok, map()} | {error, term()}.
request_resproxy(#{base_url := BaseUrl} = Config, Ip) ->
Url = build_resproxy_url(BaseUrl, Ip),
do_request(Config, Url).

-spec do_request(ipinfo:t(), binary()) -> {ok, map()} | {error, term()}.
do_request(#{access_token := AccessToken, timeout := Timeout}, Url) ->
ReqHeaders = build_req_headers(AccessToken),
Request = {Url, ReqHeaders},
Request = {binary_to_list(Url), ReqHeaders},
HTTPOptions = [
{timeout, Timeout}
],
Expand Down Expand Up @@ -40,6 +49,10 @@ build_url(BaseUrl, Ip) when Ip =:= nil orelse Ip =:= undefined ->
build_url(BaseUrl, Ip) when is_binary(BaseUrl) andalso is_binary(Ip) ->
<<BaseUrl/binary, "/", Ip/binary>>.

-spec build_resproxy_url(binary(), binary()) -> binary().
build_resproxy_url(BaseUrl, Ip) when is_binary(BaseUrl) andalso is_binary(Ip) ->
<<BaseUrl/binary, "/resproxy/", Ip/binary>>.

build_req_headers(nil) ->
base_req_headers();
build_req_headers(AccessToken) when is_binary(AccessToken) ->
Expand Down
44 changes: 44 additions & 0 deletions test/ipinfo_resproxy_SUITE.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
-module(ipinfo_resproxy_SUITE).

-include_lib("eunit/include/eunit.hrl").
-include_lib("common_test/include/ct.hrl").

-export([
all/0,
init_per_suite/1,
end_per_suite/1
]).

-export([
resproxy_test/1,
resproxy_empty_test/1
]).

all() ->
[resproxy_test, resproxy_empty_test].

init_per_suite(Config) ->
{ok, _} = application:ensure_all_started(ipinfo),
Token = case os:getenv("IPINFO_TOKEN") of
false -> nil;
TokenStr -> list_to_binary(TokenStr)
end,
[{token, Token} | Config].

end_per_suite(_Config) ->
ok = application:stop(ipinfo).

resproxy_test(Config) ->
Token = proplists:get_value(token, Config),
{ok, IpInfo} = ipinfo:create(Token),
{ok, Resproxy} = ipinfo:resproxy(IpInfo, <<"175.107.211.204">>),
?assertEqual(<<"175.107.211.204">>, maps:get(ip, Resproxy)),
?assertNotEqual(undefined, maps:get(last_seen, Resproxy)),
?assertNotEqual(undefined, maps:get(percent_days_seen, Resproxy)),
?assertNotEqual(undefined, maps:get(service, Resproxy)).

resproxy_empty_test(Config) ->
Token = proplists:get_value(token, Config),
{ok, IpInfo} = ipinfo:create(Token),
{ok, Resproxy} = ipinfo:resproxy(IpInfo, <<"8.8.8.8">>),
?assertEqual(#{}, Resproxy).