1
+ import inspect
1
2
import json
2
3
import logging
3
4
import os
5
+ import re
4
6
import sys
5
7
from datetime import datetime , timezone
6
8
from types import MappingProxyType
7
- from typing import Callable , Dict , Optional
9
+ from typing import Callable , Dict , Optional , TypedDict
8
10
9
11
import requests
10
12
import requests .exceptions
@@ -22,6 +24,51 @@ def python_version_info():
22
24
return f"{ version_info .major } .{ version_info .minor } .{ version_info .micro } -{ version_info .releaselevel } "
23
25
24
26
27
+ LABELBOX_CALL_PATTERN = re .compile (r"/labelbox/" )
28
+ TEST_FILE_PATTERN = re .compile (r".*test.*\.py$" )
29
+
30
+
31
+ class _RequestInfo (TypedDict ):
32
+ prefix : str
33
+ class_name : str
34
+ method_name : str
35
+
36
+
37
+ def call_info ():
38
+ method_name = "Unknown"
39
+ prefix = ""
40
+ class_name = ""
41
+ skip_methods = ["wrapper" , "__init__" ]
42
+ skip_classes = ["PaginatedCollection" , "_CursorPagination" , "_OffsetPagination" ]
43
+
44
+ try :
45
+ call_info = None
46
+ for stack in reversed (inspect .stack ()):
47
+ if LABELBOX_CALL_PATTERN .search (stack .filename ):
48
+ call_info = stack
49
+ method_name = call_info .function
50
+ class_name = call_info .frame .f_locals .get (
51
+ "self" , None
52
+ ).__class__ .__name__
53
+
54
+ if method_name not in skip_methods and class_name not in skip_classes :
55
+ if TEST_FILE_PATTERN .search (call_info .filename ):
56
+ prefix = "test:"
57
+ else :
58
+ if class_name == "NoneType" :
59
+ class_name = ""
60
+ break
61
+
62
+ except Exception :
63
+ pass
64
+ return _RequestInfo (prefix = prefix , class_name = class_name , method_name = method_name )
65
+
66
+
67
+ def call_info_as_str ():
68
+ info = call_info ()
69
+ return f"{ info ['prefix' ]} { info ['class_name' ]} :{ info ['method_name' ]} "
70
+
71
+
25
72
class RequestClient :
26
73
"""A Labelbox request client.
27
74
@@ -186,6 +233,8 @@ def convert_value(value):
186
233
if files :
187
234
del headers ["Content-Type" ]
188
235
del headers ["Accept" ]
236
+ headers ["X-SDK-Method" ] = call_info_as_str ()
237
+
189
238
request = requests .Request (
190
239
"POST" ,
191
240
endpoint ,
0 commit comments