1
- """Output and/or upload a TestRun or MfgEvent proto for mfg-inspector.com.
2
- """
1
+ """Output and/or upload a TestRun or MfgEvent proto for mfg-inspector.com."""
3
2
4
- import json
5
3
import logging
6
- import threading
7
4
import time
8
5
import zlib
9
6
10
- import httplib2
11
- import oauth2client . client
7
+ from google . auth . transport import requests
8
+ from google . oauth2 import service_account
12
9
13
10
from openhtf .output import callbacks
14
11
from openhtf .output .proto import guzzle_pb2
@@ -26,26 +23,24 @@ class InvalidTestRunError(Exception):
26
23
def _send_mfg_inspector_request (envelope_data , credentials , destination_url ):
27
24
"""Send upload http request. Intended to be run in retry loop."""
28
25
logging .info ('Uploading result...' )
29
- http = httplib2 .Http ()
30
26
31
- if credentials .access_token_expired :
32
- credentials .refresh (http )
33
- credentials .authorize (http )
34
-
35
- resp , content = http .request (destination_url , 'POST' , envelope_data )
27
+ with requests .AuthorizedSession (credentials ) as authed_session :
28
+ response = authed_session .request (
29
+ 'POST' , destination_url , data = envelope_data )
36
30
37
31
try :
38
- result = json . loads ( content )
32
+ result = response . json ( )
39
33
except Exception :
40
- logging .warning ('Upload failed with response %s: %s' , resp , content )
41
- raise UploadFailedError (resp , content )
34
+ logging .warning ('Upload failed with response %s: %s' , response ,
35
+ response .text )
36
+ raise UploadFailedError (response , response .text )
42
37
43
- if resp . status == 200 :
38
+ if response . status_code == 200 :
44
39
return result
45
40
46
41
message = '%s: %s' % (result .get ('error' ,
47
42
'UNKNOWN_ERROR' ), result .get ('message' ))
48
- if resp . status == 400 :
43
+ if response . status_code == 400 :
49
44
raise InvalidTestRunError (message )
50
45
else :
51
46
raise UploadFailedError (message )
@@ -73,26 +68,6 @@ def send_mfg_inspector_data(inspector_proto, credentials, destination_url,
73
68
return {}
74
69
75
70
76
- class _MemStorage (oauth2client .client .Storage ):
77
- """Helper Storage class that keeps credentials in memory."""
78
-
79
- def __init__ (self ):
80
- self ._lock = threading .Lock ()
81
- self ._credentials = None
82
-
83
- def acquire_lock (self ):
84
- self ._lock .acquire (True )
85
-
86
- def release_lock (self ):
87
- self ._lock .release ()
88
-
89
- def locked_get (self ):
90
- return self ._credentials
91
-
92
- def locked_put (self , credentials ):
93
- self ._credentials = credentials
94
-
95
-
96
71
class MfgInspector (object ):
97
72
"""Interface to convert a TestRun to a mfg-inspector compatible proto.
98
73
@@ -146,14 +121,14 @@ def __init__(self,
146
121
self .destination_url = destination_url
147
122
148
123
if user and keydata :
149
- self .credentials = oauth2client . client . SignedJwtAssertionCredentials (
150
- service_account_name = self . user ,
151
- private_key = ( self .keydata . encode ()
152
- if isinstance ( self .keydata , str ) else self . keydata ) ,
153
- scope = self .SCOPE_CODE_URI ,
154
- user_agent = 'OpenHTF Guzzle Upload Client' ,
155
- token_uri = self . token_uri )
156
- self .credentials . set_store ( _MemStorage () )
124
+ self .credentials = service_account . Credentials . from_service_account_info (
125
+ {
126
+ 'client_email' : self .user ,
127
+ 'token_uri' : self .token_uri ,
128
+ 'private_key' : self .keydata ,
129
+ ' user_agent' : 'OpenHTF Guzzle Upload Client' ,
130
+ },
131
+ scopes = [ self .SCOPE_CODE_URI ] )
157
132
else :
158
133
self .credentials = None
159
134
0 commit comments