1414
1515from six .moves .urllib .parse import urlencode , urljoin
1616from six import string_types
17- import requests
18- from oauth2client . client import GoogleCredentials
17+ import google . auth
18+ from google . auth . transport . requests import AuthorizedSession
1919
2020from firecloud .errors import FireCloudServerError
2121from firecloud .fccore import __fcconfig as fcconfig
2222from firecloud .__about__ import __version__
2323
2424FISS_USER_AGENT = "FISS/" + __version__
2525
26- # Set Global credentials
27- __CREDENTIALS = None
26+ # Set Global Authorized Session
27+ __SESSION = AuthorizedSession (google .auth .default (['https://www.googleapis.com/auth/userinfo.profile' ,
28+ 'https://www.googleapis.com/auth/userinfo.email' ])[0 ])
2829
2930#################################################
3031# Utilities
3132#################################################
32- def _fiss_access_headers (headers = None ):
33+ def _fiss_agent_header (headers = None ):
3334 """ Return request headers for fiss.
34- Retrieves an access token with the user's google crededentials, and
35- inserts FISS as the User-Agent.
35+ Inserts FISS as the User-Agent.
3636
3737 Args:
3838 headers (dict): Include additional headers as key-value pairs
3939
4040 """
41- # By only having a single instance of GoogleCredentials, access token
42- # caching is enabled
43- global __CREDENTIALS
44- if __CREDENTIALS is None :
45- __CREDENTIALS = GoogleCredentials .get_application_default ()
46- # We need to request userinfo.profile and userinfo.email if we are using a service account to run FISS
47- __CREDENTIALS = __CREDENTIALS .create_scoped (['https://www.googleapis.com/auth/userinfo.profile' , 'https://www.googleapis.com/auth/userinfo.email' ])
48- fcconfig .set_access_token (__CREDENTIALS .get_access_token ().access_token )
49- fiss_headers = {"Authorization" : "bearer " + fcconfig .access_token }
50- fiss_headers ["User-Agent" ] = FISS_USER_AGENT
51- if headers :
41+ fiss_headers = {"User-Agent" : FISS_USER_AGENT }
42+ if headers is not None :
5243 fiss_headers .update (headers )
5344 return fiss_headers
5445
5546def __get (methcall , headers = None , root_url = fcconfig .root_url , ** kwargs ):
5647 if not headers :
57- headers = _fiss_access_headers ()
58- r = requests .get (urljoin (root_url , methcall ), headers = headers , ** kwargs )
48+ headers = _fiss_agent_header ()
49+ r = __SESSION .get (urljoin (root_url , methcall ), headers = headers , ** kwargs )
5950 if fcconfig .verbosity > 1 :
6051 print ('FISSFC call: %s' % r .url , file = sys .stderr )
6152 return r
6253
6354def __post (methcall , headers = None , root_url = fcconfig .root_url , ** kwargs ):
6455 if not headers :
65- headers = _fiss_access_headers ({"Content-type" : "application/json" })
66- r = requests .post (urljoin (root_url , methcall ), headers = headers , ** kwargs )
56+ headers = _fiss_agent_header ({"Content-type" : "application/json" })
57+ r = __SESSION .post (urljoin (root_url , methcall ), headers = headers , ** kwargs )
6758 if fcconfig .verbosity > 1 :
6859 info = r .url
6960 json = kwargs .get ("json" , None )
@@ -74,8 +65,8 @@ def __post(methcall, headers=None, root_url=fcconfig.root_url, **kwargs):
7465
7566def __put (methcall , headers = None , root_url = fcconfig .root_url , ** kwargs ):
7667 if not headers :
77- headers = _fiss_access_headers ()
78- r = requests .put (urljoin (root_url , methcall ), headers = headers , ** kwargs )
68+ headers = _fiss_agent_header ()
69+ r = __SESSION .put (urljoin (root_url , methcall ), headers = headers , ** kwargs )
7970 if fcconfig .verbosity > 1 :
8071 info = r .url
8172 json = kwargs .get ("json" , None )
@@ -86,8 +77,8 @@ def __put(methcall, headers=None, root_url=fcconfig.root_url, **kwargs):
8677
8778def __delete (methcall , headers = None , root_url = fcconfig .root_url ):
8879 if not headers :
89- headers = _fiss_access_headers ()
90- r = requests .delete (urljoin (root_url , methcall ), headers = headers )
80+ headers = _fiss_agent_header ()
81+ r = __SESSION .delete (urljoin (root_url , methcall ), headers = headers )
9182 if fcconfig .verbosity > 1 :
9283 print ('FISSFC call: DELETE %s' % r .url , file = sys .stderr )
9384 return r
@@ -138,7 +129,7 @@ def list_entity_types(namespace, workspace):
138129 Swagger:
139130 https://api.firecloud.org/#!/Entities/getEntityTypes
140131 """
141- headers = _fiss_access_headers ({"Content-type" : "application/json" })
132+ headers = _fiss_agent_header ({"Content-type" : "application/json" })
142133 uri = "workspaces/{0}/{1}/entities" .format (namespace , workspace )
143134 return __get (uri , headers = headers )
144135
@@ -154,7 +145,7 @@ def upload_entities(namespace, workspace, entity_data):
154145 https://api.firecloud.org/#!/Entities/importEntities
155146 """
156147 body = urlencode ({"entities" : entity_data })
157- headers = _fiss_access_headers ({
148+ headers = _fiss_agent_header ({
158149 'Content-type' : "application/x-www-form-urlencoded"
159150 })
160151 uri = "workspaces/{0}/{1}/importEntities" .format (namespace , workspace )
@@ -429,12 +420,12 @@ def update_entity(namespace, workspace, etype, ename, updates):
429420 Swagger:
430421 https://api.firecloud.org/#!/Entities/update_entity
431422 """
432- headers = _fiss_access_headers ({"Content-type" : "application/json" })
423+ headers = _fiss_agent_header ({"Content-type" : "application/json" })
433424 uri = "{0}workspaces/{1}/{2}/entities/{3}/{4}" .format (fcconfig .root_url ,
434425 namespace , workspace , etype , ename )
435426
436427 # FIXME: create __patch method, akin to __get, __delete etc
437- return requests .patch (uri , headers = headers , json = updates )
428+ return __SESSION .patch (uri , headers = headers , json = updates )
438429
439430###############################
440431### 1.2 Method Configurations
@@ -525,7 +516,7 @@ def update_workspace_config(namespace, workspace, cnamespace, configname, body):
525516 Swagger:
526517 https://api.firecloud.org/#!/Method_Configurations/updateWorkspaceMethodConfig
527518 """
528- headers = _fiss_access_headers ({"Content-type" : "application/json" })
519+ headers = _fiss_agent_header ({"Content-type" : "application/json" })
529520 body = json .dumps (body )
530521 uri = "workspaces/{0}/{1}/method_configs/{2}/{3}" .format (namespace ,
531522 workspace , cnamespace , configname )
@@ -1121,9 +1112,9 @@ def update_workspace_acl(namespace, workspace, acl_updates):
11211112 """
11221113 uri = "{0}workspaces/{1}/{2}/acl" .format (fcconfig .root_url ,
11231114 namespace , workspace )
1124- headers = _fiss_access_headers ({"Content-type" : "application/json" })
1115+ headers = _fiss_agent_header ({"Content-type" : "application/json" })
11251116 # FIXME: create __patch method, akin to __get, __delete etc
1126- return requests .patch (uri , headers = headers , data = json .dumps (acl_updates ))
1117+ return __SESSION .patch (uri , headers = headers , data = json .dumps (acl_updates ))
11271118
11281119def clone_workspace (from_namespace , from_workspace , to_namespace , to_workspace ,
11291120 authorizationDomain = "" ):
@@ -1209,13 +1200,13 @@ def update_workspace_attributes(namespace, workspace, attrs):
12091200 Swagger:
12101201 https://api.firecloud.org/#!/Workspaces/updateAttributes
12111202 """
1212- headers = _fiss_access_headers ({"Content-type" : "application/json" })
1203+ headers = _fiss_agent_header ({"Content-type" : "application/json" })
12131204 uri = "{0}workspaces/{1}/{2}/updateAttributes" .format (fcconfig .root_url ,
12141205 namespace , workspace )
12151206 body = json .dumps (attrs )
12161207
12171208 # FIXME: create __patch method, akin to __get, __delete etc
1218- return requests .patch (uri , headers = headers , data = body )
1209+ return __SESSION .patch (uri , headers = headers , data = body )
12191210
12201211# Helper functions to create attribute update dictionaries
12211212
0 commit comments