async def labels_url(self, label: str = None) -> str: """Construct the URL to the label.""" if not hasattr(self, '_labels_url'): issue_url = self.request['pull_request']['issue_url'] issue_data = await self._gh.getitem(issue_url) self._labels_url = uritemplate.URITemplate(issue_data['labels_url']) return self._labels_url.expand(name=label)
def upload_file(self, rel, f): fname = os.path.basename(f) flname = fname.lower() # https://www.iana.org/assignments/media-types/media-types.xhtml if flname.endswith('.deb'): content_type = 'application/vnd.debian.binary-package' elif flname.endswith('.rpm'): content_type = 'application/x-rpm' elif flname.endswith('.tar.gz'): content_type = 'application/gzip' elif flname.endswith('.exe'): content_type = 'application/vnd.microsoft.portable-executable' else: content_type = 'application/octet-stream' url = uritemplate.URITemplate(rel['upload_url']).expand(name=fname) with open(f, 'rb') as fp: fdata = fp.read() result = self._post_url_raw(url, fdata, content_type) if result[0] != 201: raise Exception('Error {0} when uploading {1}'.format( result[0], f)) return result[1]
def get_path_variable_names(url_path: str) -> List[str]: uri_template = uritemplate.URITemplate(url_path) path_variables = list( itertools.chain.from_iterable( variable.variable_names for variable in uri_template.variables if variable.operator != '?')) return path_variables
def __init__(self, routes: RouteConfig) -> None: rules = [] # type: typing.List[Rule] views = {} # type: typing.Dict[str, typing.Callable] for path, method, view, name in flatten_routes(routes): if name in views: msg = ('Route wtih name "%s" exists more than once. Use an ' 'explicit name="..." on the Route to avoid a conflict.' ) % name raise exceptions.ConfigurationError(msg) template = uritemplate.URITemplate(path) werkzeug_path = str(path) parameters = inspect.signature(view).parameters for arg in template.variable_names: converter = self._get_converter(parameters, arg, view) template_format = '{%s}' % arg werkzeug_format = '<%s:%s>' % (converter, arg) werkzeug_path = werkzeug_path.replace(template_format, werkzeug_format) rule = Rule(werkzeug_path, methods=[method], endpoint=name) rules.append(rule) views[name] = view self._routes = routes self._adapter = Map(rules).bind('') self._views = views # Use an MRU cache for router lookups. self._lookup_cache = collections.OrderedDict( ) # type: collections.OrderedDict self._lookup_cache_size = 10000
def __init__(self, baseURI, uri, variables=None, hints=None): try: self.template = uritemplate.URITemplate(urljoin(baseURI, uri)) if (variables): self.variables = { variable: urljoin(baseURI, variables[variable]) for variable in variables } else: self.variables = { variable: '' for variable in self.template.variables } self.hints = hints except Exception as e: self.template = uritemplate.URITemplate('') self.variables = {} self.hints = None
def upload_release_file(self, release, board, tag, version, name, fmt, content): upload_url = release['upload_url'] ut = uritemplate.URITemplate(upload_url) path = ut.expand(name=name) content_type = mimetypes.types_map.get(fmt, 'application/octet-stream') try: yield self._api_request( path, method='POST', body=content, extra_headers={'Content-Type': content_type}, timeout=settings.UPLOAD_REQUEST_TIMEOUT) except httpclient.HTTPError as e: logger.error('failed to upload file %s: %s', name, self._api_error_message(e)) return
def get_link(route: Route) -> coreapi.Link: """ Given a single route, return a Link instance containing all the information needed to expose that route in an API Schema. """ path, method, view, name = route fields = [] # type: typing.List[coreapi.Field] path_names = set(uritemplate.URITemplate(path).variable_names) for param in inspect.signature(view).parameters.values(): fields += get_fields(param, method, path_names) if view.__doc__: description = textwrap.dedent(view.__doc__).strip() else: description = None return coreapi.Link(url=path, action=method, description=description, fields=fields)
def __init__(self, uri): self._uri = uritemplate.URITemplate(uri or "")
def variables(uri): try: return uritemplate.URITemplate(uri).variable_names except TypeError: return set()
def __init__(self, uri, base=None): self.uri_template = _uritemplate.URITemplate(uri=uri) self.base = base
tag = os.environ.get('CI_BRANCH') github_api_user = os.environ.get('GITHUB_API_USER') github_api_token = os.environ.get('GITHUB_API_TOKEN') artifacts = glob.glob('/artifacts/conductor-%s*' % tag) release_url = 'https://api.github.com/repos/AtomicConductor/conductor_client/releases/tags/%s' % tag if not artifacts: print 'No artifacts found' exit(2) release = requests.get(release_url) if 200 != release.status_code: print 'No release found' exit(2) responses = [] for artifact in artifacts: uri_template = uritemplate.URITemplate(release.json()['upload_url']) upload_url = uri_template.expand(name=os.path.basename(artifact)) with open(artifact, 'rb') as fh: response = requests.post(upload_url, data=fh.read(), auth=(github_api_user, github_api_token), headers={'content-type': 'application/octet-stream'}) responses.append(response) print response.json() for response in responses: response.raise_for_status()
def apply(self, *args, **kwargs): import uritemplate return L.Uri(uritemplate.URITemplate(self).expand(**kwargs))