@@ -35,22 +35,30 @@ def __init__(self, get_response):
3535
3636 response = requests .get (url = root_url + "/api/client_metadata" ,
3737 headers = {"Authorization" : f"Bearer { api_key } " })
38- response .raise_for_status ()
39- data = response .json ()
40- credentials = service_account .Credentials .from_service_account_info (
41- data ["pubsub_push_service_account" ])
42-
43- self .publisher = pubsub_v1 .PublisherClient (credentials = credentials )
44- self .topic_name = 'projects/{project_id}/topics/{topic}' .format (
45- project_id = data ['pubsub_project_id' ],
46- topic = data ['topic_id' ],
47- )
48- self .meta = data
38+ if response .status_code == 401 :
39+ raise Exception (f"APIToolkit Error: Invalid API key" )
40+ elif response .status_code >= 400 :
41+ print (f"APIToolkit: Error getting client metadata { response .status_code } " )
42+ else :
43+ data = response .json ()
44+ credentials = service_account .Credentials .from_service_account_info (
45+ data ["pubsub_push_service_account" ])
46+
47+ self .publisher = pubsub_v1 .PublisherClient (credentials = credentials )
48+ self .topic_name = 'projects/{project_id}/topics/{topic}' .format (
49+ project_id = data ['pubsub_project_id' ],
50+ topic = data ['topic_id' ],
51+ )
52+ self .meta = data
4953
5054 def getInfo (self ):
5155 return {"project_id" : self .meta ["project_id" ], "service_version" : self .service_version , "tags" : self .tags }
5256
5357 def publish_message (self , payload ):
58+ if self .topic_name is None or self .publisher is None :
59+ if self .debug :
60+ print ("APIToolkit: No topic or publisher (restart your server to fix)" )
61+ return
5462 data = json .dumps (payload ).encode ('utf-8' )
5563 if self .debug :
5664 print ("APIToolkit: publish message" )
@@ -104,6 +112,12 @@ def __call__(self, request):
104112 request .apitoolkit_errors = []
105113 request .apitoolkit_client = self
106114 response = self .get_response (request )
115+
116+ if self .meta is None :
117+ if self .debug :
118+ print ("APIToolkit: Project ID not set (restart your server to fix)" )
119+ return response
120+
107121 if self .debug :
108122 print ("APIToolkit: after request" )
109123 try :
0 commit comments