Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# to be able to unittest outside of any context.
uri_vars = {}
# If passed in from Robot, it's a series of strings that need to be
# parsed by the "=" char., otherwise it's a python dictionary, which is
# the only argument.
if isinstance(args[0], basestring):
for arg in args:
split_arg = arg.split("=")
uri_vars[split_arg[0]] = split_arg[1]
else:
uri_vars = args[0]
# Check that variables are correct and match template.
for uri_var in uri_vars:
if uri_var not in uritemplate.variables(self.uri_template):
raise exceptions.InvalidUriTemplateVariable("The variable passed in, \"%s\" does not match "
"template \"%s\" for page object \"%s\"" % (uri_var,
self
.uri_template,
pageobj_name))
return uritemplate.expand(self.baseurl + self.uri_template, uri_vars)
# URI template not being passed in, so the page object might have a "url" attribute
# set which means the page object has a unique URL. Eg, Pubmed Home Page would have a
# "url" attribute set to "/pubmed" given a baseurl of "http://domain".
try:
self.url
except AttributeError:
raise exceptions.NoUrlAttributeException(
"Page object \"%s\" must have a \"url\" attribute set." % pageobj_name)
if sub_content:
content[key] = sub_content
if links:
for link in get_dicts(links):
rel = _get_string(link, 'rel')
if rel:
href = _get_string(link, 'href')
method = _get_string(link, 'method')
schema = _get_dict(link, 'schema')
schema_type = _get_list(schema, 'type')
schema_properties = _get_dict(schema, 'properties')
schema_required = _get_list(schema, 'required')
fields = []
url = urlparse.urljoin(base_url, href)
templated = uritemplate.variables(url)
for item in templated:
orig = item
if item.startswith('(') and item.endswith(')'):
item = urllib.unquote(item.strip('(').rstrip(')'))
if item.startswith('#/'):
components = [
component for component in item.strip('#/').split('/')
if component != 'definitions'
]
item = '_'.join(components).replace('-', '_')
url = url.replace(orig, item)
fields.append(Field(name=item, location='path', required=True))
if schema_type == ['object'] and schema_properties:
fields += [
Field(name=key, required=(key in schema_required))
if sub_content:
content[key] = sub_content
if links:
for link in get_dicts(links):
rel = _get_string(link, 'rel')
if rel:
href = _get_string(link, 'href')
method = _get_string(link, 'method')
schema = _get_dict(link, 'schema')
schema_type = _get_list(schema, 'type')
schema_properties = _get_dict(schema, 'properties')
schema_required = _get_list(schema, 'required')
fields = []
url = urlparse.urljoin(base_url, href)
templated = uritemplate.variables(url)
for item in templated:
orig = item
if item.startswith('(') and item.endswith(')'):
item = unquote(item.strip('(').rstrip(')'))
if item.startswith('#/'):
components = [
component for component in item.strip('#/').split('/')
if component != 'definitions'
]
item = '_'.join(components).replace('-', '_')
url = url.replace(orig, item)
fields.append(Field(name=item, location='path', required=True))
if schema_type == ['object'] and schema_properties:
fields += [
Field(name=key, required=(key in schema_required))
def get_example_uri(path):
"""Returns an example URI for a path template
Args:
path (openapi.Path): path object for a specific resource
Returns:
str: The path with concrete path parameters.
"""
params = {}
for variable in uritemplate.variables(path):
params[variable] = "1"
return uritemplate.expand(path, **params)
def variables(self):
"""Returns the variables the URI takes"""
return uritemplate.variables(self.url)
def rel(self, name, **kwargs):
link = self.schema.get_link(name)
method = link.get('method', 'get').lower()
href = link.get('href', '')
params = kwargs.get('params', {})
variables = uritemplate.variables(href)
uri = self.expand_uri(name, **params)
if not urlparse(uri).netloc:
uri = urljoin(self.url, uri)
if 'params' in kwargs:
unused_params = {
k: v for k, v in list(params.items()) if k not in variables}
kwargs['params'] = unused_params
if "data" in kwargs:
resource = kwargs.get("data")
headers = kwargs.get('headers', {})
if isinstance(resource, Resource):
kwargs["data"] = json.dumps(resource.data)