def __new__(mcs, attrs, record_type_name, endpoint_route): """Object creation.""" # TODO better template building ? url_template = f"/api{endpoint_route}" + "/{?params*}" mcs.name = f"{record_type_name}SearchLinksSchema" attrs.update( { "self": Link( template=URITemplate(url_template), permission="search", params=search_link_params(0), ), "prev": Link( template=URITemplate(url_template), permission="search", params=search_link_params(-1), when=search_link_when(-1), ), "next": Link( template=URITemplate(url_template), permission="search", params=search_link_params(+1), when=search_link_when(+1), ), } ) return super().__new__(mcs, mcs.name, (), attrs)
class FileLinksSchema(Schema): """Schema for a record file's links.""" self_ = Link( template=URITemplate("/api/records/{pid_value}/files/{key}"), permission="read", params=lambda record_file: { 'pid_value': record_file.record.pid.pid_value, 'key': record_file.key, }, data_key="self" # To avoid using self since is python reserved key ) # TODO: Explore how to expose also the HTTP method # commit = { # "href": URITemplate("/api/records/{pid_value}/files/{key}/commit"), # "method": "POST", # } content = Link( template=URITemplate("/api/records/{pid_value}/files/{key}/content"), permission="read", params=lambda record_file: { 'pid_value': record_file.record.pid.pid_value, 'key': record_file.key, }, ) commit = Link( template=URITemplate("/api/records/{pid_value}/files/{key}/commit"), permission="read", params=lambda record_file: { 'pid_value': record_file.record.pid.pid_value, 'key': record_file.key, }, )
def test_resource_class_create(): rec_type = RecordTypeFactory("ResourceTest", RecordSchema) assert rec_type.resource_cls assert rec_type.resource_config_cls assert rec_type.resource_cls.__name__ == "ResourceTestResource" assert ( rec_type.resource_config_cls.__name__ == "ResourceTestResourceConfig") assert rec_type.resource_config_cls.list_route == "/resourcetests" assert (rec_type.resource_config_cls.item_route == "/resourcetests/<pid_value>") links_schema_class = rec_type.resource_config_cls.links_config["record"] search_links_schema_class = rec_type.resource_config_cls.links_config[ "search"] assert links_schema_class.__name__ == "ResourceTestLinksSchema" assert ( search_links_schema_class.__name__ == "ResourceTestSearchLinksSchema") assert hasattr(links_schema_class, "self") assert hasattr(search_links_schema_class, "self") assert hasattr(search_links_schema_class, "prev") assert hasattr(search_links_schema_class, "next") assert (links_schema_class.self.template.uri == URITemplate( "/api/resourcetests/{pid_value}").uri) assert (search_links_schema_class.self.template.uri == URITemplate( "/api/resourcetests/{?params*}").uri) assert rec_type.resource_cls.default_config == rec_type.resource_config_cls
class FileLinksSchema(Schema): """Schema for a record's links.""" self_ = Link( template=URITemplate("/api/mocks/{pid_value}/files/{key}"), permission="read", params=lambda record_file: { 'pid_value': record_file.record.pid.pid_value, 'key': record_file.key, }, data_key="self" # To avoid using self since is python reserved key ) content = Link( template=URITemplate("/api/mocks/{pid_value}/files/{key}/content"), permission="read", params=lambda record_file: { 'pid_value': record_file.record.pid.pid_value, 'key': record_file.key, }, ) commit = Link( template=URITemplate("/api/mocks/{pid_value}/files/{key}/commit"), permission="read", params=lambda record_file: { 'pid_value': record_file.record.pid.pid_value, 'key': record_file.key, }, )
def __init__(self, user, session=None): super(User, self).__init__(user, session) if not self.type: self.type = 'User' #: ID of the user's image on Gravatar self.gravatar_id = user.get('gravatar_id', '') #: True -- for hire, False -- not for hire self.hireable = user.get('hireable', False) ## The number of public_gists #: Number of public gists self.public_gists = user.get('public_gists', 0) # Private information #: How much disk consumed by the user self.disk_usage = user.get('disk_usage', 0) #: Number of private repos owned by this user self.owned_private_repos = user.get('owned_private_repos', 0) #: Number of private gists owned by this user self.total_private_gists = user.get('total_private_gists', 0) #: Total number of private repos self.total_private_repos = user.get('total_private_repos', 0) #: Which plan this user is on self.plan = Plan(user.get('plan', {})) events_url = user.get('events_url', '') #: Events URL Template. Expands with ``privacy`` self.events_urlt = URITemplate(events_url) if events_url else None #: Followers URL (not a template) self.followers_url = user.get('followers_url', '') furl = user.get('following_url', '') #: Following URL Template. Expands with ``other_user`` self.following_urlt = URITemplate(furl) if furl else None gists_url = user.get('gists_url', '') #: Gists URL Template. Expands with ``gist_id`` self.gists_urlt = URITemplate(gists_url) if gists_url else None #: Organizations URL (not a template) self.organizations_url = user.get('organizations_url', '') #: Received Events URL (not a template) self.received_events_url = user.get('received_events_url', '') #: Repostories URL (not a template) self.repos_url = user.get('repos_url', '') starred_url = user.get('starred_url', '') #: Starred URL Template. Expands with ``owner`` and ``repo`` self.starred_urlt = URITemplate(starred_url) if starred_url else None #: Subscriptions URL (not a template) self.subscriptions_url = user.get('subscriptions_url', '') self._uniq = user.get('id', None)
def test_no_slash_leading_fail(self): with self.assertRaises(ValueError): check_template_leading_slash(URITemplate('{}')) with self.assertRaises(ValueError): check_template_leading_slash(URITemplate('{count}')) check_template_leading_slash(URITemplate('/{count}'))
def test_unacceptable_route(self): # TODO decide whether failing all routes because one route has # failed is intended. with self.assertRaises(ValueError): URITemplateRouter([ URITemplate('{target}'), URITemplate('/{target}'), ])
def test_basic_creation_from_template(self): m = Map([ URITemplateRule(URITemplate('/'), endpoint='root'), URITemplateRule(URITemplate('/somewhere'), endpoint='elsewhere'), ]).bind('example.com') self.assertEqual(('root', {}), m.match('/')) self.assertEqual(('elsewhere', {}), m.match('/somewhere')) with self.assertRaises(NotFound): m.match('/nowhere')
class DraftLinksSchema(Schema): """Schema for a draft's links.""" self = Link(template=URITemplate("/api/mocks/{pid_value}/draft"), permission="read", params=lambda draft: {'pid_value': draft.pid.pid_value}) publish = Link( template=URITemplate("/api/mocks/{pid_value}/draft/actions/publish"), permission="publish", params=lambda draft: {'pid_value': draft.pid.pid_value})
def _update_attributes(self, user): #: URL of the avatar at gravatar self.avatar_url = user['avatar_url'] #: Events URL Template. Expands with ``privacy`` self.events_urlt = URITemplate(user['events_url']) #: Followers URL (not a template) self.followers_url = user['followers_url'] #: Following URL Template. Expands with ``other_user`` self.following_urlt = URITemplate(user['following_url']) #: Gists URL Template. Expands with ``gist_id`` self.gists_urlt = URITemplate(user['gists_url']) #: ID of the user's image on Gravatar self.gravatar_id = user['gravatar_id'] # e.g. https://github.com/self._login #: URL of the user/org's profile self.html_url = user['html_url'] #: Unique ID of the account self.id = user['id'] #: User name of the user self.login = user['login'] #: Organizations URL (not a template) self.organizations_url = user['organizations_url'] #: Received Events URL (not a template) self.received_events_url = user['received_events_url'] #: Repostories URL (not a template) self.repos_url = user['repos_url'] self.site_admin = user.get('site_admin') #: Starred URL Template. Expands with ``owner`` and ``repo`` self.starred_urlt = URITemplate(user['starred_url']) #: Subscriptions URL (not a template) self.subscriptions_url = user['subscriptions_url'] self.type = user['type'] self.url = self._api = user['url'] self._uniq = self.id
class BibliographicUserRecordsSearchLinksSchemaV1(SearchLinksSchema): """User Record Links schema.""" self = Link(template=URITemplate("/api/user/records{?params*}"), permission="search", params=search_link_params(0)) prev = Link(template=URITemplate("/api/user/records{?params*}"), permission="search", params=search_link_params(-1), when=search_link_when(-1)) next = Link(template=URITemplate("/api/user/records{?params*}"), permission="search", params=search_link_params(+1), when=search_link_when(+1))
class BibliographicRecordLinksSchemaV1(RecordLinksSchema): """Record Links schema.""" # WARNING: It was intentionally decided that if # config.py::RECORDS_UI_ENDPOINTS is changed by the instance, # the instance also needs to overwrite this Schema (in the # links_config) self_html = Link(template=URITemplate("/records/{pid_value}"), permission="read", params=lambda record: {'pid_value': record.pid.pid_value}) files = Link(template=URITemplate("/api/records/{pid_value}/files"), permission="read", params=lambda record: {'pid_value': record.pid.pid_value})
def status(self, upload, account=None, username=None): """Check status of upload Parameters ---------- upload: str The id of the upload or a dict with key 'id'. username : str Account username, defaults to the service's username. account : str, **deprecated** Alias for username. Will be removed in version 1.0. Returns ------- requests.Response """ username = self._resolve_username(account, username) if isinstance(upload, dict): upload_id = upload['id'] else: upload_id = upload uri = URITemplate(self.baseuri + '/{username}/{upload_id}').expand( username=username, upload_id=upload_id) resp = self.session.get(uri) self.handle_http_error(resp) return resp
class RecordLinksSchema(Schema): """Schema for a record's links.""" # NOTE: # - /api prefix is needed here because above are mounted on /api self_ = Link( template=URITemplate("/api/mocks/{pid_value}"), permission="read", params=lambda record: {'pid_value': record.pid.pid_value}, data_key="self" # To avoid using self since is python reserved key ) files = Link( template=URITemplate("/api/mocks/{pid_value}/files"), permission="read", params=lambda record: {'pid_value': record.pid.pid_value}, )
def _request_uri(self, searchtext, country=None, apiKey=None): baseuri = REQUEST_BASEURI if country: baseuri += '&countrySet={}'.format(country) baseuri = baseuri + '&key={apiKey}' if apiKey else baseuri return URITemplate(baseuri).expand(apiKey=apiKey, searchtext=searchtext.encode('utf-8'))
def __init__(self, local_search_paths=None, url_search_prefixes=None, auth=None, uri_search_template=None): """Instantiates a ComponentStore including the specified locations. Args: local_search_paths: A list of local directories to include in the search. url_seach_prefixes: A list of URL prefixes to include in the search. auth: Auth object for the requests library. See https://requests.readthedocs.io/en/master/user/authentication/ uri_search_template: A URI template for components, which may include {name}, {digest} and {tag} variables. """ self.local_search_paths = local_search_paths or ['.'] if uri_search_template: self.uri_search_template = URITemplate(uri_search_template) self.url_search_prefixes = url_search_prefixes or [] self._auth = auth self._component_file_name = 'component.yaml' self._digests_subpath = 'versions/sha256' self._tags_subpath = 'versions/tags' cache_base_dir = Path(tempfile.gettempdir()) / '.kfp_components' self._git_blob_hash_to_data_db = KeyValueStore( cache_dir=cache_base_dir / 'git_blob_hash_to_data') self._url_to_info_db = KeyValueStore(cache_dir=cache_base_dir / 'url_to_info')
def upload_asset(upload_uri_tmpl, filepath, label): filename = os.path.basename(filepath) upload_url = URITemplate(upload_uri_tmpl).expand(name=filename, label=label) headers = GITHUB_API_HEADERS headers['Content-Type'] = 'application/octet-stream' with open(filepath, 'rb') as payload: requests.post(upload_url, data=payload, auth=GITHUB_API_AUTH, headers=headers)
def test_multi_path_matcher(self): template = URITemplate('{/path*}') url = '/one%2Ctwo%2Cthree/some/path' result = match(template, url) self.assertEqual({ 'path': ['one%2Ctwo%2Cthree', 'some', 'path'], }, result)
def update_feature(self, dataset, fid, feature): """Inserts or updates a feature in a dataset. Parameters ---------- dataset : str The dataset id. fid : str The feature id. If the dataset has no feature with the given feature id, then a new feature will be created. feature : dict The GeoJSON feature object. This should be one individual GeoJSON feature and not a GeoJSON FeatureCollection. Returns ------- request.Response The response contains a GeoJSON representation of the new or updated feature. """ uri = URITemplate( self.baseuri + '/{owner}/{did}/features/{fid}').expand( owner=self.username, did=dataset, fid=fid) return self.session.put(uri, json=feature)
def test_str_repr(self): uri = 'https://api.github.com{/endpoint}' t = URITemplate(uri) self.assertEqual(str(t), uri) self.assertEqual(str(t.variables[0]), '/endpoint') self.assertEqual(repr(t), 'URITemplate("%s")' % uri) self.assertEqual(repr(t.variables[0]), 'URIVariable(/endpoint)')
def __init__(self, release, session=None): super(Release, self).__init__(release, session) self._api = release.get('url') #: List of :class:`Asset <Asset>` objects for this release self.assets = [Asset(i, self) for i in release.get('assets', [])] #: URL for uploaded assets self.assets_url = release.get('assets_url') #: Body of the release (the description) self.body = release.get('body') #: Date the release was created self.created_at = self._strptime(release.get('created_at')) #: Boolean whether value is True or False self.draft = release.get('draft') #: HTML URL of the release self.html_url = release.get('html_url') #: GitHub id self.id = release.get('id') #: Name given to the release self.name = release.get('name') #; Boolean whether release is a prerelease self.prerelease = release.get('prerelease') #: Date the release was published self.published_at = self._strptime(release.get('published_at')) #: Name of the tag self.tag_name = release.get('tag_name') #: "Commit" that this release targets self.target_commitish = release.get('target_commitish') upload_url = release.get('upload_url') #: URITemplate to upload an asset with self.upload_urlt = URITemplate(upload_url) if upload_url else None
def _update_attributes(self, issue): self._api = issue["url"] self.assignee = issue["assignee"] if self.assignee: self.assignee = users.ShortUser(self.assignee, self) self.assignees = issue["assignees"] if self.assignees: self.assignees = [ users.ShortUser(assignee, self) for assignee in self.assignees ] self.body = issue["body"] self.closed_at = self._strptime(issue["closed_at"]) self.comments_count = issue["comments"] self.comments_url = issue["comments_url"] self.created_at = self._strptime(issue["created_at"]) self.events_url = issue["events_url"] self.html_url = issue["html_url"] self.id = issue["id"] self.labels_urlt = URITemplate(issue["labels_url"]) self.locked = issue["locked"] self.milestone = issue["milestone"] if self.milestone: self.milestone = milestone.Milestone(self.milestone, self) self.number = issue["number"] self.original_labels = issue["labels"] if self.original_labels: self.original_labels = [ label.ShortLabel(lbl, self) for lbl in self.original_labels ] self.pull_request_urls = issue.get("pull_request") self.state = issue["state"] self.title = issue["title"] self.updated_at = self._strptime(issue["updated_at"]) self.user = users.ShortUser(issue["user"], self)
def get_sentences(self, request): expansions = { 'count': request.get('count', None), 'max-words': request.get('max_words', None) } if request.get('feature', None) is not None: template = '/sentence{/feature}{?count,max-words}' expansions.update(feature=request['feature']) elif request.get('word', None) is not None: if request.get('pos', None) is not None: template = '/sentence/word{/word}/pos{/pos}{?count,max-words}' expansions.update(word=request['word'], pos=request['pos']) else: template = '/sentence/word{/word}{?count,max-words}' expansions.update(word=request['word']) else: raise NotImplementedError() endpoint = URITemplate(self._api_path + template) url = endpoint.expand(expansions) return self.client.get(url, headers=self.headers)
def create(self, stage_url, tileset, name=None, patch=False): """Initiates the creation process from the staging S3 bucket into the user's tileset. Note: this step is refered to as "upload" in the API docs; This classes upload() method is a high-level function which acts like the web-based upload form Parameters stage_url: URL to resource on S3, does not work on arbitrary URLs (TODO) tileset: the map/tileset name to create. Username will be prefixed if not done already (e.g. 'test1' becomes 'username.test1') Returns a response object where the json() contents are an upload dict """ if not tileset.startswith(self.username + "."): tileset = "{0}.{1}".format(self.username, tileset) msg = {'tileset': tileset, 'url': stage_url} if patch: msg['patch'] = patch if name is not None: msg['name'] = name uri = URITemplate(self.baseuri + '/{username}').expand(username=self.username) resp = self.session.post(uri, json=msg) self.handle_http_error(resp) return resp
def reverse(self, lon=None, lat=None, types=None): """Returns a Requests response object that contains a GeoJSON collection of places near the given longitude and latitude. `response.geojson()` returns the geocoding result as GeoJSON. `response.status_code` returns the HTTP API status code. See: https://www.mapbox.com/api-documentation/#retrieve-places-near-a-location.""" uri = URITemplate(self.baseuri + '/{dataset}/{lon},{lat}.json').expand( dataset=self.name, lon=str(round(float(lon), self.precision.get('reverse', 5))), lat=str(round(float(lat), self.precision.get('reverse', 5)))) params = {} if types: params.update(self._validate_place_types(types)) resp = self.session.get(uri, params=params) self.handle_http_error(resp) # for consistency with other services def geojson(): return resp.json() resp.geojson = geojson return resp
def from_strings(cls, uritemplate_strs): """ Construct a router from a list of strings that can be formed into a valid URITemplate. """ return cls(URITemplate(s) for s in uritemplate_strs)
def test_expand(self): """ This test ensures that expansion works as expected. """ # Single t = URITemplate('https://api.github.com/users{/user}') expanded = 'https://api.github.com/users/sigmavirus24' self.assertEqual(t.expand(user='******'), expanded) v = t.variables[0] self.assertEqual(v.expand({'user': None}), {'/user': ''}) # Multiple t = URITemplate('https://api.github.com/users{/user}{/repo}') expanded = 'https://api.github.com/users/sigmavirus24/github3.py' self.assertEqual(t.expand({'repo': 'github3.py'}, user='******'), expanded)
def test_simple_variable_matcher(self): template = URITemplate('/{count}') url = '/123' result = match(template, url) self.assertEqual({ 'count': '123', }, result)
def test_default_value(self): uri = 'https://api.github.com/user{/user=sigmavirus24}' t = URITemplate(uri) self.assertEqual(t.expand(), 'https://api.github.com/user/sigmavirus24') self.assertEqual(t.expand(user='******'), 'https://api.github.com/user/lukasa')
def test_single_path_matcher(self): template = URITemplate('{/count}') url = '/root/foo/bar' result = match(template, url) # nothing because additional path segment separator which does # not match. self.assertIsNone(result)