def handle_scheme_from(self, value): url = URLObject(value) self.url = self.url.with_scheme(url.scheme)
def list_callback(request): url = URLObject(request.url) offset = int(url.query_dict.get("offset") or 0) if offset: return (200, {}, json.dumps([])) return (200, {}, json.dumps(drafts))
def wrap_url(self, url): o_url = URLObject(url) return o_url.add_query_param(settings.URLKEY_NAME, self.id)
def download_from_url(self, url): url_object = URLObject(url) file_id, file_key = url_object.fragment[1:].split('!') self.download_file(file_id, file_key, public=True)
def __init__(self, blueprint=None, base_url=None, *args, **kwargs): super(OAuth2Session, self).__init__(*args, **kwargs) self.blueprint = blueprint self.base_url = URLObject(base_url) lazy.invalidate(self, "token")
def process_repository_hook(hook_data, via="webhook", fetched_at=None, commit=True, requestor_id=None, repo_id=None): hook_id = hook_data.get("id") if not hook_id: raise MissingData("no hook ID") if not repo_id: url = hook_data.get("url") if not url: raise MissingData("no hook url") # parse repo info from url path = URLObject(url).path assert path.segments[0] == "repos" repo_owner = path.segments[1] repo_name = path.segments[2] # fetch repo from database repo_query = (Repository.query .filter(Repository.owner_login == repo_owner) .filter(Repository.name == repo_name) ) try: repo = repo_query.one() except NoResultFound: msg = "Repo {owner}/{repo} not loaded in webhookdb".format( owner=repo_owner, repo=repo_name, ) raise NotFound(msg, { "type": "repo_hook", "owner": repo_owner, "repo": repo_name, }) except MultipleResultsFound: msg = "Repo {owner}/{repo} found multiple times!".format( owner=repo_owner, repo=repo_name, ) raise DatabaseError(msg, { "type": "repo_hook", "owner": repo_owner, "repo": repo_name, }) repo_id = repo.id # fetch the object from the database, # or create it if it doesn't exist in the DB hook = RepositoryHook.query.get(hook_id) if not hook: hook = RepositoryHook(id=hook_id, repo_id=repo_id) # should we update the object? fetched_at = fetched_at or datetime.now() if hook.last_replicated_at > fetched_at: raise StaleData() # update the object fields = ( "name", "config", "events", "active", "last_response", ) for field in fields: if field in hook_data: setattr(hook, field, hook_data[field]) dt_fields = ("created_at", "updated_at") for field in dt_fields: if hook_data.get(field): dt = parse_date(hook_data[field]).replace(tzinfo=None) setattr(hook, field, dt) # `url` is special -- it's the value in the `config` object, # NOT the top-level `url` property hook.url = hook_data.get("config", {}).get("url") # update replication timestamp replicated_dt_field = "last_replicated_via_{}_at".format(via) if hasattr(hook, replicated_dt_field): setattr(hook, replicated_dt_field, fetched_at) # add to DB session, so that it will be committed db.session.add(hook) if commit: db.session.commit() return hook
def get_comments(pull): url = URLObject(pull.comments_url).set_query_param( "sort", "created").set_query_param("direction", "desc") comments = Comment.from_json(paginated_get(url)) return comments
def handle_base(self, value): base = self.prepare_value(value) self.url = URLObject(base)
def handle_query_from(self, value): url = URLObject(value) self.url = self.url.with_query(url.query)
def handle_fragment_from(self, value): url = URLObject(value) self.url = self.url.with_fragment(url.fragment)
def handle_port_from(self, value): url = URLObject(value) self.url = self.url.with_port(url.port)
def handle_add_path_from(self, value): url = URLObject(value) path_to_add = url.path if path_to_add.startswith('/'): path_to_add = path_to_add[1:] self.url = self.url.add_path(path_to_add)
def handle_path_from(self, value): url = URLObject(value) self.url = self.url.with_path(url.path)
def handle_host_from(self, value): url = URLObject(value) self.url = self.url.with_hostname(url.hostname)
from urlobject import URLObject import os ROOT = URLObject(os.environ.get('HOST', 'https://shortesttrack.com')) UAA_SERVICE_ENDPOINT = ROOT.add_path('api/uaa') OAUTH_SERVICE_ENDPOINT = ROOT.add_path('oauth') JWTKEY_ENDPOINT = ROOT.add_path('oauth_jwtkey') METADATA_SERVICE_ENDPOINT = ROOT.add_path('api/metadata')
def handle_add_query_from(self, value): url = URLObject(value) self.url = self.url.add_query_params(**url.query.dict)
def test_init_invalid(self): with pytest.raises(ValueError): self._get_plantuml('') with pytest.raises(ValueError): self._get_plantuml(str(URLObject(host).with_scheme('socks5')))
def test_remote_plantuml_error(self): with pytest.raises(ValueError): self._get_remote_plantuml_by_try_plantuml( str(URLObject(OFFICIAL_PLANTUML_HOST).with_scheme('socks5')))
def _host_process(host: str) -> URLObject: return URLObject(host).without_fragment().without_query()
from urlobject import URLObject ROOT = URLObject('https://shortesttrack.com') METADATA_ENDPOINT = ROOT.add_path('api/metadata/') EXECUTION_METADATA_ENDPOINT = ROOT.add_path('api/execution-metadata/') LOGGING_ENDPOINT = ROOT.add_path('api/logging/') DATA_ENDPOINT = ROOT.add_path('api/data') PERFORMANCES_LIST_GET_PATH = EXECUTION_METADATA_ENDPOINT.add_path( 'v2/performances/') def sec_get_detail(uuid): return METADATA_ENDPOINT.add_path( 'script-execution-configurations/{uuid}'.format(uuid=uuid)) def performance_get_detail(uuid): return EXECUTION_METADATA_ENDPOINT.add_path( 'v2/performances/{uuid}/'.format(uuid=uuid)) def performance_output_parameter_post(parameter_id, performance_id): return EXECUTION_METADATA_ENDPOINT.\ add_path('v2/performances/{performance_id}/output-parameters/{parameter_id}/value/'. format(performance_id=performance_id, parameter_id=parameter_id)) def logging_post(performance_id): return LOGGING_ENDPOINT.add_path(
def test_filter_threads_ts(mocked_responses, api_client): api_client.threads.where(started_before=1275350400).all() assert len(mocked_responses.calls) == 1 request = mocked_responses.calls[0].request url = URLObject(request.url) assert url.query_dict["started_before"] == "1275350400"
def __init__(self, blueprint=None, base_url=None, *args, **kwargs): token = {"access_token": blueprint.admin_access_token} super().__init__(token=token, *args, **kwargs) self.blueprint = blueprint self.base_url = URLObject(base_url)
def __init__(self, blueprint=None, base_url=None, *args, **kwargs): super(OAuth1Session, self).__init__(*args, **kwargs) self.blueprint = blueprint self.base_url = URLObject(base_url)
def __init__(self, blueprint=None, base_url=None, *args, **kwargs): super().__init__(*args, **kwargs) self.blueprint = blueprint self.base_url = URLObject(base_url) invalidate_cached_property(self, "token")
def request(self, http_method, path, params=None, data=None, json=None, base=resources.METADATA_SERVICE_ENDPOINT, basic_auth_tuple=None, sec_id_for_special_token=None, extra_headers=None, raw_content=False): assert not (basic_auth_tuple and sec_id_for_special_token ), 'Only one of these args can be specified' if path.startswith('/'): path = path[1:] retry = False auth = None if basic_auth_tuple: # The basic auth can be used instead of normal Bearer auth auth = HTTPBasicAuth(*basic_auth_tuple) headers = {} else: # Normal Bearer auth if sec_id_for_special_token: assert hasattr(self, 'get_sec_access_token' ), 'Method does not exist. Forgot Mixin?' # This param is overriding auth with SEC token auth headers = { 'Authorization': 'Bearer {}'.format( self.get_sec_access_token(sec_id_for_special_token)) } retry = True else: # Use user auth headers = {'Authorization': self._get_auth_string()} retry = False url = URLObject(base).add_path(path) headers['Content-Type'] = 'application/json' if extra_headers: headers.update(extra_headers) def _request(): try: self.logger.info('Calling metadata service (%s) : %s', http_method, url) resp_ = requests.request(http_method, url, params=params, data=data, json=json, headers=headers, auth=auth) except requests.RequestException as e_: self.logger.info('Outer service request error: {}'.format(e_)) self._raise_performing_request_error( 'Error of request performing.: {}'.format(e_)) return resp_ resp = _request() try: resp.raise_for_status() except requests.HTTPError as e: self.logger.info(resp.content) if retry and resp.status_code in [401, 403]: self._retry_occurred() # Probably sec_access_token is expired, let's get a new one headers = { 'Authorization': 'Bearer {}'.format( self.get_sec_access_token(sec_id_for_special_token)) } # Retry request resp = _request() try: resp.raise_for_status() except requests.HTTPError as e: # This is not access_token expiration, raise exception self._raise_http_error(resp.status_code, resp.reason) else: self._raise_http_error(resp.status_code, resp.reason) except Exception as e: # This error should never be happened self.logger.info('Unexpected error: {}'.format(e)) raise if not raw_content: return resp.json() return resp.content
def test_filter_messages_ts(mocked_responses, api_client): api_client.messages.where(received_before=1275350400).all() assert len(mocked_responses.calls) == 1 request = mocked_responses.calls[0].request url = URLObject(request.url) assert url.query_dict["received_before"] == "1275350400"
def make_jira_blueprint(base_url, consumer_key=None, rsa_key=None, redirect_url=None, redirect_to=None, login_url=None, authorized_url=None, session_class=None, backend=None): """ Make a blueprint for authenticating with JIRA using OAuth 1. This requires a consumer key and RSA key for the JIRA appication link. You should either pass them to this constructor, or make sure that your Flask application config defines them, using the variables JIRA_OAUTH_CONSUMER_KEY and JIRA_OAUTH_RSA_KEY. Args: base_url (str): The base URL of your JIRA installation. For example, for Atlassian's hosted Cloud JIRA, the base_url would be ``https://jira.atlassian.com`` consumer_key (str): The consumer key for your Application Link on JIRA rsa_key (str or path): The RSA private key for your Application Link on JIRA. This can be the contents of the key as a string, or a path to the key file on disk. redirect_url (str): the URL to redirect to after the authentication dance is complete redirect_to (str): if ``redirect_url`` is not defined, the name of the view to redirect to after the authentication dance is complete. The actual URL will be determined by :func:`flask.url_for` login_url (str, optional): the URL path for the ``login`` view. Defaults to ``/jira`` authorized_url (str, optional): the URL path for the ``authorized`` view. Defaults to ``/jira/authorized``. session_class (class, optional): The class to use for creating a Requests session. Defaults to :class:`~flask_dance.consumer.requests.OAuth1Session`. backend: A storage backend class, or an instance of a storage backend class, to use for this blueprint. Defaults to :class:`~flask_dance.consumer.backend.session.SessionBackend`. :rtype: :class:`~flask_dance.consumer.OAuth1ConsumerBlueprint` :returns: A :ref:`blueprint <flask:blueprints>` to attach to your Flask app. """ if rsa_key and os.path.isfile(rsa_key): with open(rsa_key) as f: rsa_key = f.read() base_url = URLObject(base_url) jira_bp = OAuth1ConsumerBlueprint( "jira", __name__, client_key=consumer_key, rsa_key=rsa_key, signature_method=SIGNATURE_RSA, base_url=base_url, request_token_url=base_url.relative( "plugins/servlet/oauth/request-token"), access_token_url=base_url.relative( "plugins/servlet/oauth/access-token"), authorization_url=base_url.relative("plugins/servlet/oauth/authorize"), redirect_url=redirect_url, redirect_to=redirect_to, login_url=login_url, authorized_url=authorized_url, session_class=session_class, backend=backend, ) jira_bp.session.headers["Content-Type"] = "application/json" jira_bp.from_config["client_key"] = "JIRA_OAUTH_CONSUMER_KEY" jira_bp.from_config["rsa_key"] = "JIRA_OAUTH_RSA_KEY" @jira_bp.before_app_request def set_applocal_session(): ctx = stack.top ctx.jira_oauth = jira_bp.session return jira_bp
def test_message_delete_by_id(mocked_responses, api_client): api_client.messages.delete(1234, forceful=True) assert len(mocked_responses.calls) == 1 request = mocked_responses.calls[0].request url = URLObject(request.url) assert url.query_dict["forceful"] == "True"
def process_milestone(milestone_data, via="webhook", fetched_at=None, commit=True, repo_id=None): number = milestone_data.get("number") if not number: raise MissingData("no milestone number") if not repo_id: url = milestone_data.get("url") if not url: raise MissingData("no milestone url") # parse repo info from url path = URLObject(url).path assert path.segments[0] == "repos" repo_owner = path.segments[1] repo_name = path.segments[2] # fetch repo from database try: repo = Repository.get(repo_owner, repo_name) except MultipleResultsFound: msg = "Repo {owner}/{repo} found multiple times!".format( owner=repo_owner, repo=repo_name, ) raise DatabaseError(msg, { "type": "milestone", "owner": repo_owner, "repo": repo_name, }) if not repo: msg = "Repo {owner}/{repo} not loaded in webhookdb".format( owner=repo_owner, repo=repo_name, ) raise NotFound(msg, { "type": "milestone", "owner": repo_owner, "repo": repo_name, }) repo_id = repo.id # fetch the object from the database, # or create it if it doesn't exist in the DB milestone = Milestone.query.get((repo_id, number)) if not milestone: milestone = Milestone(repo_id=repo_id, number=number) # should we update the object? fetched_at = fetched_at or datetime.now() if milestone.last_replicated_at > fetched_at: raise StaleData() # Most fields have the same name in our model as they do in Github's API. # However, some are different. This mapping contains just the differences. field_to_model = { "open_issues": "open_issues_count", "closed_issues": "closed_issues_count", "due_on": "due_at", } # update the object fields = ( "state", "title", "description", "open_issues", "closed_issues", ) for field in fields: if field in milestone_data: mfield = field_to_model.get(field, field) setattr(milestone, mfield, milestone_data[field]) dt_fields = ("created_at", "updated_at", "closed_at", "due_on") for field in dt_fields: if milestone_data.get(field): dt = parse_date(milestone_data[field]).replace(tzinfo=None) mfield = field_to_model.get(field, field) setattr(milestone, mfield, dt) # user references user_fields = ("creator", ) for user_field in user_fields: if user_field not in milestone_data: continue user_data = milestone_data[user_field] id_field = "{}_id".format(user_field) login_field = "{}_login".format(user_field) if user_data: setattr(milestone, id_field, user_data["id"]) if hasattr(milestone, login_field): setattr(milestone, login_field, user_data["login"]) try: process_user(user_data, via=via, fetched_at=fetched_at) except StaleData: pass else: setattr(milestone, id_field, None) if hasattr(milestone, login_field): setattr(milestone, login_field, None) # update replication timestamp replicated_dt_field = "last_replicated_via_{}_at".format(via) if hasattr(milestone, replicated_dt_field): setattr(milestone, replicated_dt_field, fetched_at) # add to DB session, so that it will be committed db.session.add(milestone) if commit: db.session.commit() return milestone
def clean_url(self, url): """ Remove querystrings. """ return URLObject(url).without_query()