@@ -9,75 +9,43 @@ defmodule SdkComplianceAdapter.TrackedClient do
99
1010 @ impl true
1111 def client ( api_key , api_host ) do
12- # Create the underlying Req client
13- client =
14- Req . new ( base_url: api_host )
15- |> Req.Request . put_private ( :api_key , api_key )
16-
17- # Return the standard PostHog.API.Client struct with our module
18- % PostHog.API.Client { client: client , module: __MODULE__ }
12+ client = PostHog.API.Client . client ( api_key , api_host )
13+ instrumented_client =
14+ client . client
15+ |> Req.Request . append_response_steps ( track: & track / 1 )
16+ |> Req.Request . append_error_steps ( track_error: & track_error / 1 )
17+
18+ % { client | client: instrumented_client }
1919 end
20-
20+
2121 @ impl true
22- def request ( client , method , url , opts ) do
23- # Build the request
24- req =
25- client
26- |> Req . merge ( method: method , url: url )
27- |> Req . merge ( opts )
28- |> then ( fn req ->
29- req
30- |> Req.Request . fetch_option ( :json )
31- |> case do
32- { :ok , json } ->
33- api_key = Req.Request . get_private ( req , :api_key )
34- Req . merge ( req , json: Map . put_new ( json , :api_key , api_key ) )
35-
36- :error ->
37- req
38- end
39- end )
40-
41- # Extract UUIDs from the batch before sending
42- uuid_list = extract_uuids ( opts )
43- event_count = count_events ( opts )
44-
45- # Make the request
46- result = Req . request ( req )
47-
48- # Track the request
49- case result do
50- { :ok , % { status: status } } ->
51- SdkComplianceAdapter.State . record_request ( status , event_count , uuid_list )
52-
53- { :error , reason } ->
54- SdkComplianceAdapter.State . set_last_error ( inspect ( reason ) )
55- end
56-
57- result
22+ defdelegate request ( client , method , url , opts ) , to: PostHog.API.Client
23+
24+ def track ( { request , response } ) do
25+ req_body =
26+ request . body
27+ |> to_string ( )
28+ |> JSON . decode! ( )
29+
30+ uuid_list = extract_uuids ( req_body )
31+ event_count = count_events ( req_body )
32+
33+ SdkComplianceAdapter.State . record_request ( response . status , event_count , uuid_list )
34+
35+ { request , response }
5836 end
59-
60- defp extract_uuids ( opts ) do
61- case Keyword . get ( opts , :json ) do
62- % { batch: batch } when is_list ( batch ) ->
63- Enum . map ( batch , fn event ->
64- # Check event level first, then properties
65- Map . get ( event , :uuid ) ||
66- Map . get ( event , "uuid" ) ||
67- get_in ( event , [ :properties , :uuid ] ) ||
68- get_in ( event , [ "properties" , "uuid" ] )
69- end )
70- |> Enum . reject ( & is_nil / 1 )
71-
72- _ ->
73- [ ]
74- end
37+
38+ def track_error ( { request , exception } ) do
39+ SdkComplianceAdapter.State . set_last_error ( inspect ( exception ) )
40+ { request , exception }
7541 end
7642
77- defp count_events ( opts ) do
78- case Keyword . get ( opts , :json ) do
79- % { batch: batch } when is_list ( batch ) -> length ( batch )
80- _ -> 0
81- end
43+ defp extract_uuids ( request ) do
44+ request
45+ |> get_in ( [ Access . key ( "batch" , [ ] ) , Access . all ( ) , "uuid" ] )
46+ |> Enum . reject ( & is_nil / 1 )
8247 end
48+
49+ defp count_events ( % { "batch" => events } ) when is_list ( events ) , do: length ( events )
50+ defp count_events ( _ ) , do: 0
8351end
0 commit comments