Skip to content

Make it possible to provide token file locations when using rdams_client from within python #15

@nicholas512

Description

@nicholas512

current behaviour

If I download the package using pip, I can't update the DEFAULT_AUTH_FILE location without directly modifying the code, and I can't pass a specific file location to functions like submit (i.e. the token_file parameter doesn't get passed to get_authentication)

This means I have to copy-paste the code locally into my repository and makes it difficult to

expected behaviour

After downloading the package using pip and importing it into python, I can specifiy a token_file location for use with submit and other functions

suggested solution 1

add a token_file parameter to functions that use get_authentication so that users can manually specify the token file location

e.g. for get_metadata:

def get_metadata(ds, token_file=DEFAULT_AUTH_FILE):
    """Return metadata of dataset.

    Args:
        ds (str): Datset id. e.g. 'ds083.2'

    Returns:
        dict: JSON decoded result of the query.
    """
    url = BASE_URL + 'metadata/'
    url += ds

    token = get_authentication(token_file)
    ret = requests.get(encode_url(url,token))

    check_status(ret)
    return ret.json()

suggested solution 2

Change the get_authentication file to dynamically fetch the token file from a class attribute if None is provided (see code below).

class Defaults:
    AUTH_FILE = "./rdams_token.txt"


def get_authentication(token_file=None):
    """Attempts to get authentication.

    Args:
        token_file (str): location of password file.

    Returns:
        (tuple): token
    """
    if token_file is None: 
        token_file = Defaults.AUTH_FILE
    if os.path.isfile(token_file) and os.path.getsize(token_file) > 0:
        return read_token_file(token_file)
    else:
        return get_userinfo()

Now, a user can monkey-patch the module as follows, because the default token file path doesn't get hard-coded into the function definition.

from rda_apps_clients import rdams_client
rdams_client.DEFAULT_AUTH_FILE = "/my/file/location.txt"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions