55It wraps the posthog-python SDK and exposes a REST API for the test harness to exercise.
66"""
77
8- import json
98import logging
109import os
1110import threading
1211import time
13- from datetime import datetime
1412from typing import Any , Dict , List , Optional
1513
1614from flask import Flask , jsonify , request
1715
18- from posthog import Client , Posthog
16+ from posthog import Client
1917from posthog .request import batch_post as original_batch_post
2018from posthog .version import VERSION
2119
3129class RequestInfo :
3230 """Information about an HTTP request made by the SDK"""
3331
34- def __init__ (self , timestamp_ms : int , status_code : int , retry_attempt : int , event_count : int , uuid_list : List [str ]):
32+ def __init__ (
33+ self ,
34+ timestamp_ms : int ,
35+ status_code : int ,
36+ retry_attempt : int ,
37+ event_count : int ,
38+ uuid_list : List [str ],
39+ ):
3540 self .timestamp_ms = timestamp_ms
3641 self .status_code = status_code
3742 self .retry_attempt = retry_attempt
@@ -145,7 +150,13 @@ def create_batch_id(batch: List[Dict]) -> str:
145150 return "-" .join (uuids [:3 ]) # Use first 3 UUIDs as batch ID
146151
147152
148- def patched_batch_post (api_key : str , host : Optional [str ] = None , gzip : bool = False , timeout : int = 15 , ** kwargs ):
153+ def patched_batch_post (
154+ api_key : str ,
155+ host : Optional [str ] = None ,
156+ gzip : bool = False ,
157+ timeout : int = 15 ,
158+ ** kwargs ,
159+ ):
149160 """Patched version of batch_post that tracks requests"""
150161 batch = kwargs .get ("batch" , [])
151162 batch_id = create_batch_id (batch )
@@ -158,27 +169,35 @@ def patched_batch_post(api_key: str, host: Optional[str] = None, gzip: bool = Fa
158169 return response
159170 except Exception as e :
160171 # Record failed request
161- status_code = getattr (e , "status_code" , 500 ) if hasattr (e , "status_code" ) else 500
172+ status_code = (
173+ getattr (e , "status_code" , 500 ) if hasattr (e , "status_code" ) else 500
174+ )
162175 state .record_request (status_code , batch , batch_id )
163176 state .record_error (str (e ))
164177 raise
165178
166179
167180# Monkey-patch the batch_post function
168- import posthog .request
181+ import posthog .request # noqa: E402
169182
170183posthog .request .batch_post = patched_batch_post
171184
172185# Also patch in consumer module
173- import posthog .consumer
186+ import posthog .consumer # noqa: E402
174187
175188posthog .consumer .batch_post = patched_batch_post
176189
177190
178191@app .route ("/health" , methods = ["GET" ])
179192def health ():
180193 """Health check endpoint"""
181- return jsonify ({"sdk_name" : "posthog-python" , "sdk_version" : VERSION , "adapter_version" : "1.0.0" })
194+ return jsonify (
195+ {
196+ "sdk_name" : "posthog-python" ,
197+ "sdk_version" : VERSION ,
198+ "adapter_version" : "1.0.0" ,
199+ }
200+ )
182201
183202
184203@app .route ("/init" , methods = ["POST" ])
@@ -294,7 +313,9 @@ def identify():
294313 state .increment_captured ()
295314
296315 if properties_set_once :
297- state .client .set_once (distinct_id = distinct_id , properties = properties_set_once )
316+ state .client .set_once (
317+ distinct_id = distinct_id , properties = properties_set_once
318+ )
298319 state .increment_captured ()
299320
300321 logger .info (f"Identified user: { distinct_id } " )
0 commit comments