Packages
launchdarkly_server_sdk
1.2.0
3.11.0
3.10.1
3.9.0
3.8.1
3.8.0
3.7.2
3.7.1
3.7.0
3.6.0
3.5.0
3.4.0
3.3.1
3.3.0
3.2.0
3.1.0
3.0.4
3.0.3
3.0.2
3.0.1
3.0.0
2.1.2
2.1.1
2.1.0
2.0.5
2.0.4
2.0.3
2.0.2
2.0.1
2.0.0
1.6.0
1.5.0
1.4.0
1.3.2
1.3.1
1.3.0
1.2.0
1.1.3
1.1.2
1.1.1
retired
1.1.0
retired
1.0.1
retired
1.0.0
retired
1.0.0-beta4
retired
1.0.0-beta3
retired
1.0.0-beta2
retired
LaunchDarkly SDK for Erlang
Current section
Files
Jump to
Current section
Files
src/ldclient_event_dispatch_httpc.erl
%%-------------------------------------------------------------------
%% @doc Event dispatcher
%% @private
%% @end
%%-------------------------------------------------------------------
-module(ldclient_event_dispatch_httpc).
-behaviour(ldclient_event_dispatch).
%% Behavior callbacks
-export([send/4]).
%%===================================================================
%% Behavior callbacks
%%===================================================================
%% @doc Send events to LaunchDarkly event server
%%
%% @end
-spec send(JsonEvents :: binary(), PayloadId :: uuid:uuid(), Uri :: string(), SdkKey :: string()) ->
ok | {error, temporary, string()} | {error, permanent, string()}.
send(JsonEvents, PayloadId, Uri, SdkKey) ->
Headers = [
{"Authorization", SdkKey},
{"X-LaunchDarkly-Event-Schema", ldclient_config:get_event_schema()},
{"User-Agent", ldclient_config:get_user_agent()},
{"X-LaunchDarkly-Payload-ID", uuid:uuid_to_string(PayloadId)}
],
Request = httpc:request(post, {Uri, Headers, "application/json", JsonEvents}, [], []),
process_request(Request).
%%===================================================================
%% Internal functions
%%===================================================================
-type http_request() :: {ok, {{string(), integer(), string()}, [{string(), string()}], string() | binary()}}.
-spec process_request({error, term()} | http_request())
-> ok | {error, temporary, string()} | {error, permanent, string()}.
process_request({error, Reason}) ->
{error, temporary, Reason};
process_request({ok, {{_Version, StatusCode, _ReasonPhrase}, _Headers, _Body}}) when StatusCode < 400 ->
ok;
process_request({ok, {{Version, StatusCode, ReasonPhrase}, _Headers, _Body}}) ->
Reason = format_response(Version, StatusCode, ReasonPhrase),
HttpErrorType = is_http_error_code_recoverable(StatusCode),
{error, HttpErrorType, Reason}.
-spec format_response(Version :: string(), StatusCode :: integer(), ReasonPhrase :: string()) ->
string().
format_response(Version, StatusCode, ReasonPhrase) ->
io_lib:format("~s ~b ~s", [Version, StatusCode, ReasonPhrase]).
-spec is_http_error_code_recoverable(StatusCode :: integer()) -> temporary | permanent.
is_http_error_code_recoverable(400) -> temporary;
is_http_error_code_recoverable(408) -> temporary;
is_http_error_code_recoverable(429) -> temporary;
is_http_error_code_recoverable(StatusCode) when StatusCode >= 500 -> temporary;
is_http_error_code_recoverable(_) -> permanent.