Exemplo n.º 1
0
    def __init__(self, endpoint_class=None, state=None, **kwargs):
        self.state = state or State(is_using_cli=False)

        if not self.__class__.is_logged_in(self.state):
            raise ArcsecondNotLoggedInError()

        if 'debug' in kwargs.keys():
            self.state.debug = kwargs.get('debug')
        if 'verbose' in kwargs.keys():
            self.state.verbose = kwargs.get('verbose')

        prefix = kwargs.get('prefix', '')
        possible_prefixes = set(kwargs.keys()).intersection(
            VALID_PREFIXES.keys())
        if len(possible_prefixes) > 1:
            raise ArcsecondTooManyPrefixesError(possible_prefixes)
        elif len(possible_prefixes) == 1 and prefix:
            raise ArcsecondTooManyPrefixesError(
                [possible_prefixes.pop(), prefix])
        elif len(possible_prefixes) == 1 and not prefix:
            prefix_key = possible_prefixes.pop()
            prefix = VALID_PREFIXES[prefix_key] + kwargs[prefix_key]

        endpoint_class = self._check_endpoint_class(endpoint_class)
        self.endpoint = endpoint_class(
            self.state, prefix=prefix) if endpoint_class else None
Exemplo n.º 2
0
 def _check_organisation_membership(cls, state, username, subdomain):
     if state.verbose:
         click.echo(
             'Checking Membership of Organisation with subdomain "{}"...'.
             format(subdomain))
     profile, error = PersonalProfileAPIEndPoint(
         State(verbose=False, debug=state.debug)).read(username)
     if error:
         ArcsecondAPI._echo_error(state, error)
     else:
         memberships = {
             m['organisation']['subdomain']: m['role']
             for m in profile['memberships']
         }
         if subdomain in memberships.keys():
             if state.verbose:
                 click.echo(
                     'Membership confirmed. Role is "{}", stored in {}.'.
                     format(memberships[subdomain], config_file_path()))
             config_file_save_membership_role(subdomain,
                                              memberships[subdomain],
                                              state.debug)
         else:
             if state.verbose:
                 click.echo('Membership denied.')
Exemplo n.º 3
0
 def register(cls, username, email, password1, password2, state=None):
     state = state or State()
     result, error = AuthAPIEndPoint(state).register(
         username, email, password1, password2)
     if error:
         return ArcsecondAPI._echo_request_error(state, error)
     if result:
         return ArcsecondAPI._get_and_save_api_key(state, username,
                                                   result['key'])
Exemplo n.º 4
0
 def login(cls, username, password, subdomain, state=None):
     state = state or State()
     result, error = AuthAPIEndPoint(state).login(username, password)
     if error:
         return ArcsecondAPI._echo_error(state, error)
     elif result:
         result = ArcsecondAPI._get_and_save_api_key(
             state, username, result['key'])
         if subdomain:
             ArcsecondAPI._check_organisation_membership(
                 state, username, subdomain)
         return result
Exemplo n.º 5
0
def get_api_state(state=None, **kwargs):
    state = state or State(is_using_cli=False)

    if 'debug' in kwargs.keys():
        state.debug = kwargs.get('debug')
    if 'test' in kwargs.keys():
        state.test = kwargs.get('test')
    if 'verbose' in kwargs.keys():
        state.verbose = kwargs.get('verbose')
    if 'organisation' in kwargs.keys():
        state.organisation = kwargs.get('organisation')
    if 'api_key' in kwargs.keys():
        state.api_key = kwargs.get('api_key')
    if 'upload_key' in kwargs.keys():
        state.upload_key = kwargs.get('upload_key')

    if state.verbose and state.debug and state.is_using_cli:
        click.echo(f'{ECHO_PREFIX}debug mode{ECHO_PREFIX}')

    return state
Exemplo n.º 6
0
 def __init__(self, state=None, prefix=''):
     # Provided state may contain overriding api_key or upload_key to be used.
     self.state = state or State()
     self.prefix = prefix
     self.organisation = getattr(state, 'organisation', '')
     self.headers = {}
Exemplo n.º 7
0
 def __init__(self, state=None, prefix=''):
     self.state = state or State()
     self.prefix = prefix
     self.organisation = state.organisation or ''