11"""PassiveTotal API Interface."""
22
3- __author__ = 'Brandon Dixon (PassiveTotal)'
4- __version__ = '1.0.0'
53
64import json
75import logging
86import requests
97import sys
8+ from urllib .parse import quote as urlquote
109from passivetotal .config import Config
10+ from passivetotal ._version import VERSION
11+
12+ __author__ = 'Brandon Dixon (PassiveTotal)'
13+ __version__ = VERSION
14+
1115
1216
1317class Client (object ):
@@ -58,6 +62,7 @@ def __init__(self, username, api_key, server=DEFAULT_SERVER,
5862 if '127.0.0.1' in server :
5963 self .verify = False
6064 self .exception_class = exception_class
65+ self .set_context ('python' ,'passivetotal' ,VERSION )
6166
6267 @classmethod
6368 def from_config (cls ):
@@ -78,6 +83,18 @@ def set_debug(self, status):
7883 self .logger .setLevel ('DEBUG' )
7984 else :
8085 self .logger .setLevel ('INFO' )
86+
87+ def set_context (self , provider , variant , version , feature = '' ):
88+ """Set the context for this request.
89+
90+ :param provider: The company, partner, provider or other top-level application context.
91+ :param variant: The specific app, libary subcomponent, or feature category.
92+ :param version: Version of the app, feature or code setting the context.
93+ :param feature: Optional sub-feature, dashboard or script name.
94+ """
95+ context = Context (provider , variant , version , feature )
96+ self .context = context
97+ self .headers .update (context .get_header ())
8198
8299 def _endpoint (self , endpoint , action , * url_args ):
83100 """Return the URL for the action.
@@ -181,3 +198,30 @@ def _send_data(self, method, endpoint, action,
181198 kwargs ['proxies' ] = self .proxies
182199 response = requests .request (method , api_url , ** kwargs )
183200 return self ._json (response )
201+
202+
203+
204+ class Context :
205+
206+ """Integration context for a set of API requests."""
207+
208+ HEADER_NAME = 'X-RISKIQ-CONTEXT'
209+
210+ def __init__ (self , provider , variant , version , feature = '' ):
211+ """Build a new context header.
212+
213+ :param provider: The company, partner, provider or other top-level application context.
214+ :param variant: The specific app, libary subcomponent, or feature category.
215+ :param version: Version of the app, feature or code setting the context.
216+ :param feature: Optional sub-feature, dashboard or script name.
217+ """
218+ self ._fields = (provider , variant , version , feature )
219+
220+ def get_header_name (self ):
221+ return self .HEADER_NAME
222+
223+ def get_header_value (self ):
224+ return '/' .join (map (lambda f : urlquote (f , safe = '' ), self ._fields ))
225+
226+ def get_header (self ):
227+ return { self .get_header_name () : self .get_header_value () }
0 commit comments