Skip to content

Commit

Permalink
Correctly using the subdomain and headers
Browse files Browse the repository at this point in the history
Signed-off-by: Cédric Foellmi <[email protected]>
  • Loading branch information
onekiloparsec committed Mar 24, 2024
1 parent 6b637ec commit 63972e6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 45 deletions.
8 changes: 6 additions & 2 deletions arcsecond/api/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ def _detail_url(self, uuid_or_id):
def list(self, **filters):
return self._perform_request(self._list_url(**filters), 'get')

def read(self, id_name_uuid):
return self._perform_request(self._detail_url(id_name_uuid), 'get')
def read(self, id_name_uuid, headers=None):
return self._perform_request(self._detail_url(id_name_uuid),
'get',
json=None,
data=None,
headers=headers)

def create(self, json=None, data=None, headers=None):
print(json, data, headers)
Expand Down
55 changes: 12 additions & 43 deletions arcsecond/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,25 @@ def __init__(self, config: ArcsecondConfig, subdomain: str = ''):
self.datafiles = ArcsecondAPIEndpoint(self.config, 'datafiles', self.subdomain)

def register(self, username, email, password1, password2):
result, error = AuthAPIEndPoint('auth', self.config).register(username, email, password1, password2)
# never subdomain here
result, error = AuthAPIEndPoint(self.config, 'auth') \
.register(username, email, password1, password2)

if error and self.config.verbose:
click.echo(click.style(error, fg='red'))
return result, error

def login(self, username, password, **kwargs):
result, error = AuthAPIEndPoint('auth', self.config).login(username, password)
# never subdomain here
result, error = AuthAPIEndPoint(self.config, 'auth') \
.login(username, password)

if error:
click.echo(click.style(error, fg='red'))
return

auth_token = result['token']
self.profiles.use_headers({'Authorization': 'Token ' + auth_token})
profile, profile_error = self.profiles.read(username)
profile, profile_error = self.profiles.read(username, headers={'Authorization': 'Token ' + auth_token})
if profile_error:
click.echo(click.style(profile_error, fg='red'))
return
Expand All @@ -56,10 +61,9 @@ def login(self, username, password, **kwargs):
# Save memberships for future use (in Oort for instance).
self.config.save_memberships(profile.get('memberships'))

endpoint = APIEndPoint('profiles', self.config, 'uploadkey' if kwargs.get('upload_key', False) else 'apikey')
endpoint.use_headers({'Authorization': 'Token ' + auth_token})

key_data, key_error = endpoint.read(self.config.username)
subresource = 'uploadkey' if kwargs.get('upload_key', False) else 'apikey'
endpoint = ArcsecondAPIEndpoint(self.config, 'profiles', subresource=subresource) # never subdomain here
key_data, key_error = endpoint.read(self.config.username, headers={'Authorization': 'Token ' + auth_token})
if key_error:
click.echo(click.style(key_error, fg='red'))

Expand All @@ -69,38 +73,3 @@ def login(self, username, password, **kwargs):

if self.config.verbose:
click.echo(f'Successful {key_name} key retrieval.')

def fetch_full_profile(self):
return self.profiles.read(self.config.username)

@classmethod
def is_logged_in(cls, state: Optional[State] = None) -> bool:
return Config(state).is_logged_in

@classmethod
def get_username(cls, state: Optional[State] = None) -> str:
return Config(state).username

@classmethod
def get_api_name(cls, state: Optional[State] = None) -> str:
return Config(state).api_server or ''

@classmethod
def set_api_name(cls, address: str, state: Optional[State] = None) -> None:
Config(state).save(api_server=address)

@classmethod
def get_access_key(cls, state: Optional[State] = None) -> str:
return Config(state).access_key

@classmethod
def get_upload_key(cls, state: Optional[State] = None) -> str:
return Config(state).upload_key

@classmethod
def clear_access_key(cls, state: Optional[State] = None) -> None:
Config(state).clear_access_key()

@classmethod
def clear_upload_key(cls, state: Optional[State] = None) -> None:
Config(state).clear_upload_key()

0 comments on commit 63972e6

Please sign in to comment.