-
Notifications
You must be signed in to change notification settings - Fork 34
/
myapp_hex.erl
89 lines (78 loc) · 2.43 KB
/
myapp_hex.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
-module(myapp_hex).
-export([
get_api_package/1,
get_repo_tarball/2,
get_repo_versions/0
]).
%%====================================================================
%% API functions
%%====================================================================
get_api_package(Name) ->
Result = with_http_cache({api_package, Name}, fun(Config) ->
hex_api_package:get(maps:merge(Config, config()), Name)
end),
case Result of
{ok, {200, _Headers, Payload}} ->
{ok, Payload};
Other ->
Other
end.
get_repo_versions() ->
Result = with_http_cache(repo_versions, fun(Config) ->
hex_repo:get_versions(maps:merge(Config, config()))
end),
case Result of
{ok, {200, _Headers, Payload}} ->
{ok, maps:get(packages, Payload)};
Other ->
Other
end.
get_repo_tarball(Name, Version) ->
Result = with_http_cache({repo_tarball, Name, Version}, fun(Config) ->
hex_repo:get_tarball(maps:merge(Config, config()), Name, Version)
end),
case Result of
{ok, {200, _Headers, Tarball}} ->
{ok, Tarball};
Other ->
Other
end.
%%====================================================================
%% Internal functions
%%====================================================================
config() ->
Config1 = hex_core:default_config(),
Config2 = put_http_config(Config1),
Config3 = maybe_put_api_key(Config2),
Config3.
put_http_config(Config) ->
maps:put(http_user_agent_fragment, <<"(myapp/1.0.0) (httpc)">>, Config).
maybe_put_api_key(Config) ->
case os:getenv("HEX_API_KEY") of
false -> Config;
Key -> maps:put(api_key, Config, Key)
end.
with_http_cache(Key, Fun) ->
ETag = get_cache({etag, Key}, <<"">>),
Config = #{http_etag => ETag},
case Fun(Config) of
{ok, {200, Headers, Body}} ->
ok = put_cache({etag, Key}, ETag),
ok = put_cache({body, Key}, Body),
{200, Headers, Body};
{ok, {304, Headers, _Body}} ->
{200, Headers, get_cache({body, Key}, undefined)};
Other ->
Other
end.
%% naive, process dictionary based cache. Don't use in production!
get_cache(Key, Default) ->
case erlang:get({cache, Key}) of
undefined ->
Default;
Other ->
Other
end.
put_cache(Key, Value) ->
erlang:put({cache, Key}, Value),
ok.