Changeset View
Changeset View
Standalone View
Standalone View
swh/lister/core/lister_transports.py
Show First 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | def request_headers(self): | ||||
return { | return { | ||||
'User-Agent': 'Software Heritage lister (%s)' % self.lister_version | 'User-Agent': 'Software Heritage lister (%s)' % self.lister_version | ||||
} | } | ||||
def request_instance_credentials(self): | def request_instance_credentials(self): | ||||
"""Returns dictionary of any credentials configuration needed by the | """Returns dictionary of any credentials configuration needed by the | ||||
forge instance to list. | forge instance to list. | ||||
Returns: | The 'credentials' configuration is expected to be a dict of multiple | ||||
dict of credentials per instance lister or {} if none. | levels. The first level is the lister's name, the second is the | ||||
lister's instance name, which value is expected to be a list of | |||||
""" | credential structures (typically a couple username/password). | ||||
all_creds = self.config.get('credentials') | |||||
if not all_creds: | |||||
return {} | |||||
lister_creds = all_creds.get(self.LISTER_NAME, {}) | |||||
creds = lister_creds.get(self.instance, {}) | |||||
return creds | |||||
def request_uri(self, identifier): | |||||
"""Get the full request URI given the transport_request identifier. | |||||
MAY BE OVERRIDDEN if something more complex than the PATH_TEMPLATE is | |||||
required. | |||||
""" | |||||
path = self.PATH_TEMPLATE % identifier | |||||
return self.api_baseurl + path | |||||
def request_params(self, identifier): | |||||
"""Get the full parameters passed to requests given the | |||||
transport_request identifier. | |||||
This uses credentials if any are provided. The 'credentials' | |||||
configuration is expected to be a dict of multiple levels. The first | |||||
level is the lister's name, the second is the lister's instance name. | |||||
For example: | For example: | ||||
credentials: | credentials: | ||||
github: # github lister | github: # github lister | ||||
github: # has only one instance (so far) | github: # has only one instance (so far) | ||||
- username: some | - username: some | ||||
password: somekey | password: somekey | ||||
- username: one | - username: one | ||||
password: onekey | password: onekey | ||||
- ... | - ... | ||||
gitlab: # gitlab lister | gitlab: # gitlab lister | ||||
riseup: # has many instances | riseup: # has many instances | ||||
- username: someone | - username: someone | ||||
password: ... | password: ... | ||||
- ... | - ... | ||||
gitlab: | gitlab: | ||||
- username: someone | - username: someone | ||||
password: ... | password: ... | ||||
- ... | - ... | ||||
Returns: | |||||
list of credential dicts for the current lister. | |||||
""" | |||||
all_creds = self.config.get('credentials') | |||||
if not all_creds: | |||||
return [] | |||||
lister_creds = all_creds.get(self.LISTER_NAME, {}) | |||||
creds = lister_creds.get(self.instance, []) | |||||
return creds | |||||
def request_uri(self, identifier): | |||||
"""Get the full request URI given the transport_request identifier. | |||||
MAY BE OVERRIDDEN if something more complex than the PATH_TEMPLATE is | |||||
required. | |||||
""" | |||||
path = self.PATH_TEMPLATE % identifier | |||||
return self.api_baseurl + path | |||||
def request_params(self, identifier): | |||||
"""Get the full parameters passed to requests given the | |||||
transport_request identifier. | |||||
This uses credentials if any are provided (see | |||||
request_instance_credentials). | |||||
MAY BE OVERRIDDEN if something more complex than the request headers | MAY BE OVERRIDDEN if something more complex than the request headers | ||||
is needed. | is needed. | ||||
""" | """ | ||||
params = {} | params = {} | ||||
params['headers'] = self.request_headers() or {} | params['headers'] = self.request_headers() or {} | ||||
creds = self.request_instance_credentials() | creds = self.request_instance_credentials() | ||||
▲ Show 20 Lines • Show All 111 Lines • Show Last 20 Lines |