示例#1
0
def get_search_client():
    def search_refresh_callback(token_response):
        tkn = token_response.by_resource_server
        token = tkn["search.api.globus.org"]["access_token"]
        expires_at = tkn["search.api.globus.org"]["expires_at_seconds"]
        write_option(SEARCH_AT_OPTNAME, token)
        write_option(SEARCH_AT_EXPIRES_OPTNAME, expires_at)

    refresh_token = lookup_option(SEARCH_RT_OPTNAME)
    access_token = lookup_option(SEARCH_AT_OPTNAME)
    access_token_expires = lookup_option(SEARCH_AT_EXPIRES_OPTNAME)

    authorizer = None
    if access_token_expires is not None:
        authorizer = globus_sdk.RefreshTokenAuthorizer(
            refresh_token,
            internal_auth_client(),
            access_token,
            int(access_token_expires),
            on_refresh=search_refresh_callback,
        )

    return globus_sdk.SearchClient(
        authorizer=authorizer,
        app_name="search-client-cli v{}".format(version.__version__),
    )
示例#2
0
def ingest_search():
    r = request.json

    #dataset_mdata = None
    if 'dataset_mdata' in r:
        dataset_mdata = r['dataset_mdata']
    else:
        dataset_mdata = None
    search_index_id = r['search_index_id']
    mdata_dir = r['mdata_dir']
    tokens = r['tokens']

    search_auth = globus_sdk.authorizers.AccessTokenAuthorizer(
        access_token=tokens['Search'])
    sc = globus_sdk.SearchClient(authorizer=search_auth)
    print("Showing indexes")
    # indexes = [si for si in sc.get("/").data]
    # print(indexes)
    # time.sleep(20)

    from scheddy.scheduler import get_fx_client

    fxc = get_fx_client(tokens)

    search_ingest_fx_eid = "e246aad8-7838-46a3-b260-956eed859c7c"

    event = {
        'dataset_mdata': dataset_mdata,
        'search_index_id': search_index_id,
        'mdata_dir': mdata_dir,
        'search_token': tokens['Search']
    }

    proc = funcx_func(event)
示例#3
0
def tasks():

    client = NativeClient(client_id=CLIENT_ID, app_name=APP_NAME)
    client.login(requested_scopes=SCOPES)

    tokens = client.load_tokens(requested_scopes=SCOPES)
    auther = globus_sdk.AccessTokenAuthorizer(
        tokens['search.api.globus.org']['access_token'])
    sc = globus_sdk.SearchClient(authorizer=auther)
    print(sc.get_task_list(INDEX))
    print('Finished')
def main():
    search_index = '3e117028-2513-4f5b-b53c-90fda3cd328b'
    service = 'search.api.globus.org'

    authorizer = get_native_app_authorizer(client_id=CLIENT_ID,
                                           service=service)

    sc = globus_sdk.SearchClient(authorizer=authorizer)
    r = sc.search(search_index, '*')
    #r = sc.delete_subject(index_id=search_index, subject='globus://e56c36e4-1063-11e6-a747-22000bf2d559/data/papers/clipping_10428503.pdf')
    print(json.dumps(r.data, indent=4))
示例#5
0
def list_command(test, output_json, limit):
    # Should require login if there are publicly visible records
    pc = PilotClient()
    if not pc.is_logged_in():
        click.echo('You are not logged in.')
        return

    search_authorizer = pc.get_authorizers()['search.api.globus.org']
    sc = globus_sdk.SearchClient(authorizer=search_authorizer)
    # TO DO: iterate instead of upping limit
    search_results = sc.search(index_id=pc.get_index(test), q='*', limit=limit)

    if output_json:
        click.echo(json.dumps(search_results.data, indent=4))
        return

    fmt = '{:21.20}{:11.10}{:10.9}{:7.6}{:7.6}{:7.6}{}'
    columns = [
        ('Title', lambda r: r['dc']['titles'][0]['title']),
        ('Data', lambda r: r['ncipilot']['data_type']),
        ('Dataframe', lambda r: r['ncipilot']['dataframe_type']),
        ('Rows', lambda r: str(r['field_metadata']['numrows'])),
        ('Cols', lambda r: str(r['field_metadata']['numcols'])),
        ('Size', get_size),
        ('Filename', get_identifier),
    ]

    # Build row data
    rows = []
    for result in search_results['gmeta']:
        content = result['content'][0]
        if content.get('testing'):
            content = content['testing']
        row = []
        for _, function in columns:
            try:
                row.append(function(content))
            except Exception:
                row.append('')
                # raise
        rows.append(row)

    formatted_rows = [fmt.format(*r) for r in rows]
    header = fmt.format(*[c[0] for c in columns])
    output = '{}\n{}'.format(header, '\n'.join(formatted_rows))
    click.echo(output)
示例#6
0
def funcx_func(event):
    from globus_sdk import SearchClient
    import globus_sdk

    search_token = event['search_token']
    mdata_dir = event['mdata_dir']
    dataset_mdata = event['dataset_mdata']

    base_gmeta = {"ingest_type": "GMetaList", "ingest_data": {"gmeta": []}}

    # Auth with search
    print(search_token)
    sc = globus_sdk.SearchClient(
        authorizer=globus_sdk.authorizers.AccessTokenAuthorizer(
            access_token=search_token))
    #
    # TODO: hardcode.
    files_to_ingest = '/home/tskluzac/mdata'

    from random import randint
    cur_subject = randint(0, 100000)

    metadata = dict()
    metadata['file_information'] = {'a': 1, 'b': 2, 'c': 3}
    metadata["keywords"] = {'a': 30, 'b': 20, 'c': 10}

    file_obj = {
        "subject": str(cur_subject),
        "visible_to": ["public"],
        "content": metadata
    }

    base_gmeta['ingest_data']['gmeta'].append(file_obj)

    print(base_gmeta)

    index_id = "cba4cfc9-435e-4e88-90ab-cc17ef488d6f"
    x = sc.ingest(index_id, base_gmeta)
    print(x)
    print(x.content)

    #print(x)

    # # TODO: ingest.
    return "HELLO WORLD"
示例#7
0
def ingest():
    with open(SEARCH_DATA) as f:
        ingest_doc = json.loads(f.read())

    tokens = load_tokens()
    auther = globus_sdk.AccessTokenAuthorizer(
        tokens['search.api.globus.org']['access_token'])
    sc = globus_sdk.SearchClient(authorizer=auther)

    preview = [ent['subject'] for ent in ingest_doc['ingest_data']['gmeta']]
    print('\n'.join(preview))
    print('Ingest these to "{}"?'.format(
        sc.get_index(INDEX).data['display_name']))
    user_input = input('Y/N> ')
    if user_input in ['yes', 'Y', 'y', 'yarr']:
        sc.ingest(INDEX, ingest_doc)
        print('Finished')
    else:
        print('Aborting')
示例#8
0
def anonymous_login(services):
    """Initialize services without authenticating to Globus Auth.

    Arguments:
    services (str or list of str): The services to initialize clients for.
                                   Note that not all services support unauthenticated clients.

    Returns:
    dict: The clients requested, indexed by service name.
          For example, if anonymous_login() is told to auth with 'search'
            then the search client will be in the 'search' field.
    """
    if isinstance(services, str):
        services = [services]

    clients = {}
    # Initialize valid services
    if "transfer" in services:
        clients["transfer"] = globus_sdk.TransferClient()
        services.remove("transfer")

    if "search" in services:
        clients["search"] = globus_sdk.SearchClient()
        services.remove("search")

    if "publish" in services:
        clients["publish"] = DataPublicationClient()
        services.remove("publish")

    # Notify user of auth-only services
    if "search_ingest" in services:
        print_("Error: Service 'search_ingest' requires authentication.")
        services.remove("search_ingest")

    if "mdf" in services:
        print_("Error: Service 'mdf' requires authentication.")
        services.remove("mdf")

    # Warn of invalid services
    if services:
        print_("\n".join(["Unknown or invalid service: '" + sv + "'." for sv in services]))

    return clients
示例#9
0
def search_client(authenticated=True):
    storage_adapter = token_storage_adapter()
    as_dict = storage_adapter.read_as_dict()

    authorizer = None
    if authenticated:
        authdata = as_dict[SEARCH_RESOURCE_SERVER]
        access_token = authdata["access_token"]
        refresh_token = authdata["refresh_token"]
        access_token_expires = authdata["expires_at_seconds"]
        authorizer = globus_sdk.RefreshTokenAuthorizer(
            refresh_token,
            internal_auth_client(),
            access_token,
            int(access_token_expires),
            on_refresh=storage_adapter.on_refresh,
        )

    return globus_sdk.SearchClient(authorizer=authorizer,
                                   app_name="searchable-files")
示例#10
0
def delete(filename):

    with open(filename) as f:
        ingest_doc = json.loads(f.read())

    client = NativeClient(client_id=CLIENT_ID, app_name=APP_NAME)
    client.login(requested_scopes=SCOPES)

    tokens = client.load_tokens(requested_scopes=SCOPES)
    auther = globus_sdk.AccessTokenAuthorizer(
        tokens['search.api.globus.org']['access_token'])
    sc = globus_sdk.SearchClient(authorizer=auther)

    subject = ingest_doc['ingest_data']['subject']
    print(subject)
    print('Deleting from "{}"?'.format(
        sc.get_index(INDEX).data['display_name']))
    #user_input = input('Y/N> ')
    #if user_input in ['yes', 'Y', 'y', 'yarr']:
    result = sc.delete_subject(INDEX, subject)
    print('Finished')
    print(result)
示例#11
0
def translate_index(index_name):
    """Translate a known Globus Search index into the index UUID.
    The UUID is the proper way to access indices, and will eventually be the only way.
    This method will return names it cannot disambiguate.

    Arguments:
        index_name (str): The name of the index.

    Returns:
        str: The UUID of the index. If the index is not known and is not unambiguous,
                this will be the ``index_name`` unchanged instead.
    """
    uuid = SEARCH_INDEX_UUIDS.get(index_name.strip().lower())
    if not uuid:
        try:
            index_info = globus_sdk.SearchClient().get_index(index_name).data
            if not isinstance(index_info, dict):
                raise ValueError("Multiple UUIDs possible")
            uuid = index_info.get("id", index_name)
        except Exception:
            uuid = index_name
    return uuid
示例#12
0
def listind():
    client = NativeClient(client_id=CLIENT_ID, app_name=APP_NAME)
    client.login(requested_scopes=SCOPES)

    tokens = client.load_tokens(requested_scopes=SCOPES)
    auther = globus_sdk.AccessTokenAuthorizer(
        tokens['search.api.globus.org']['access_token'])
    sc = globus_sdk.SearchClient(authorizer=auther)

    search_results = sc.search(index_id=INDEX, q='*')

    header = 'Title                Data       Dataframe Rows   Cols   Size   Filename'
    print(header)
    for i in search_results['gmeta']:
        j = i['content'][0]
        s, h = get_size(j['remote_file_manifest']['length'])
        size = str(int(s)) + ' ' + h
        print('{:21.20}'.format(j['dc']['titles'][0]['title']) +
              '{:11.10}'.format(j['ncipilot']['data_type']) +
              '{:10.9}'.format(j['ncipilot']['dataframe_type']) +
              '{:7.6}'.format(str(j['ncipilot']['numrows'])) +
              '{:7.6}'.format(str(j['ncipilot']['numcols'])) +
              '{:7.6}'.format(size) +
              '{:.16}'.format(j['remote_file_manifest']['filename']))
示例#13
0
def login(credentials=None, clear_old_tokens=False, **kwargs):
    """Login to Globus services

    Arguments:
    credentials (str or dict): A string filename, string JSON, or dictionary
                                   with credential and config information.
                               By default, looks in ~/mdf/credentials/globus_login.json.
        Contains:
        app_name (str): Name of script/client. This will form the name of the token cache file.
        services (list of str): Services to authenticate with.
                                Services are listed in AUTH_SCOPES.
        client_id (str): The ID of the client, given when registered with Globus.
                         Default is the MDF Native Clients ID.
    clear_old_tokens (bool): If True, delete old token file if it exists, forcing user to re-login.
                             If False, use existing token file if there is one.
                             Default False.

    Returns:
    dict: The clients and authorizers requested, indexed by service name.
          For example, if login() is told to auth with 'search'
            then the search client will be in the 'search' field.
    """
    NATIVE_CLIENT_ID = "98bfc684-977f-4670-8669-71f8337688e4"
    DEFAULT_CRED_FILENAME = "globus_login.json"
    DEFAULT_CRED_PATH = os.path.expanduser("~/mdf/credentials")

    def _get_tokens(client, scopes, app_name, force_refresh=False):
        token_path = os.path.join(DEFAULT_CRED_PATH, app_name + "_tokens.json")
        if force_refresh:
            if os.path.exists(token_path):
                os.remove(token_path)
        if os.path.exists(token_path):
            with open(token_path, "r") as tf:
                try:
                    tokens = json.load(tf)
                    # Check that requested scopes are present
                    # :all scopes should override any scopes with lesser permissions
                    # Some scopes are returned in multiples and should be separated
                    existing_scopes = []
                    for sc in [val["scope"] for val in tokens.values()]:
                        if " " in sc:
                            existing_scopes += sc.split(" ")
                        else:
                            existing_scopes.append(sc)
                    permissive_scopes = [scope.replace(":all", "")
                                         for scope in existing_scopes
                                         if scope.endswith(":all")]
                    missing_scopes = [scope for scope in scopes.split(" ")
                                      if scope not in existing_scopes
                                      and not any([scope.startswith(per_sc)
                                                   for per_sc in permissive_scopes])
                                      and not scope.strip() == ""]
                    # If some scopes are missing, regenerate tokens
                    # Get tokens for existing scopes and new scopes
                    if len(missing_scopes) > 0:
                        scopes = " ".join(existing_scopes + missing_scopes)
                        os.remove(token_path)
                except ValueError:
                    # Tokens corrupted
                    os.remove(token_path)
        if not os.path.exists(token_path):
            os.makedirs(DEFAULT_CRED_PATH, exist_ok=True)
            client.oauth2_start_flow(requested_scopes=scopes, refresh_tokens=True)
            authorize_url = client.oauth2_get_authorize_url()

            print_("It looks like this is the first time you're accessing this service.",
                   "\nPlease log in to Globus at this link:\n", authorize_url)
            auth_code = input("Copy and paste the authorization code here: ").strip()

            # Handle 401s
            try:
                token_response = client.oauth2_exchange_code_for_tokens(auth_code)
            except globus_sdk.GlobusAPIError as e:
                if e.http_status == 401:
                    print_("\nSorry, that code isn't valid."
                           " You can try again, or contact support.")
                    sys.exit(1)
                else:
                    raise
            tokens = token_response.by_resource_server

            os.umask(0o077)
            with open(token_path, "w") as tf:
                json.dump(tokens, tf)
            print_("Thanks! You're now logged in.")

        return tokens

    if type(credentials) is str:
        try:
            with open(credentials) as cred_file:
                creds = json.load(cred_file)
        except IOError:
            try:
                creds = json.loads(credentials)
            except ValueError:
                raise ValueError("Credential string unreadable")
    elif type(credentials) is dict:
        creds = credentials
    else:
        try:
            with open(os.path.join(os.getcwd(), DEFAULT_CRED_FILENAME)) as cred_file:
                creds = json.load(cred_file)
        except IOError:
            try:
                with open(os.path.join(DEFAULT_CRED_PATH, DEFAULT_CRED_FILENAME)) as cred_file:
                    creds = json.load(cred_file)
            except IOError:
                raise ValueError("Credentials/configuration must be passed as a "
                                 + "filename string, JSON string, or dictionary, or provided in '"
                                 + DEFAULT_CRED_FILENAME
                                 + "' or '"
                                 + DEFAULT_CRED_PATH
                                 + "'.")

    native_client = globus_sdk.NativeAppAuthClient(creds.get("client_id", NATIVE_CLIENT_ID),
                                                   app_name=creds.get("app_name", "unknown"))

    servs = []
    for serv in creds.get("services", []):
        serv = serv.lower().strip()
        if type(serv) is str:
            servs += serv.split(" ")
        else:
            servs += list(serv)
    # Translate services into scopes, pass bad/unknown services
    scopes = " ".join([AUTH_SCOPES.get(sc, "") for sc in servs])

    all_tokens = _get_tokens(native_client, scopes, creds.get("app_name", "unknown"),
                             force_refresh=clear_old_tokens)

    clients = {}
    if "transfer" in servs:
        try:
            transfer_authorizer = globus_sdk.RefreshTokenAuthorizer(
                                        all_tokens["transfer.api.globus.org"]["refresh_token"],
                                        native_client)
            clients["transfer"] = globus_sdk.TransferClient(authorizer=transfer_authorizer)
        # Token not present
        except KeyError:
            print_("Error: Unable to retrieve Transfer tokens.\n"
                   "You may need to delete your old tokens and retry.")
            clients["transfer"] = None
        # Other issue
        except globus_sdk.GlobusAPIError as e:
            print_("Error: Unable to create Transfer client (" + e.message + ").")
            clients["transfer"] = None
        # Remove processed service
        servs.remove("transfer")

    if "search_ingest" in servs:
        try:
            ingest_authorizer = globus_sdk.RefreshTokenAuthorizer(
                                        all_tokens["search.api.globus.org"]["refresh_token"],
                                        native_client)
            clients["search_ingest"] = globus_sdk.SearchClient(authorizer=ingest_authorizer)
        # Token not present
        except KeyError:
            print_("Error: Unable to retrieve Search (ingest) tokens.\n"
                   "You may need to delete your old tokens and retry.")
            clients["search_ingest"] = None
        # Other issue
        except globus_sdk.GlobusAPIError as e:
            print_("Error: Unable to create Search (ingest) client (" + e.message + ").")
            clients["search_ingest"] = None
        # Remove processed service
        servs.remove("search_ingest")
        # And redundant service
        try:
            servs.remove("search")
        # No issue if it isn't there
        except Exception:
            pass
    elif "search" in servs:
        try:
            search_authorizer = globus_sdk.RefreshTokenAuthorizer(
                                        all_tokens["search.api.globus.org"]["refresh_token"],
                                        native_client)
            clients["search"] = globus_sdk.SearchClient(authorizer=search_authorizer)
        # Token not present
        except KeyError:
            print_("Error: Unable to retrieve Search tokens.\n"
                   "You may need to delete your old tokens and retry.")
            clients["search"] = None
        # Other issue
        except globus_sdk.GlobusAPIError as e:
            print_("Error: Unable to create Search client (" + e.message + ").")
            clients["search"] = None
        # Remove processed service
        servs.remove("search")

    if "mdf" in servs:
        try:
            mdf_authorizer = globus_sdk.RefreshTokenAuthorizer(
                                    all_tokens["data.materialsdatafacility.org"]["refresh_token"],
                                    native_client)
            clients["mdf"] = mdf_authorizer
        # Token not present
        except KeyError:
            print_("Error: Unable to retrieve MDF tokens.\n"
                   "You may need to delete your old tokens and retry.")
            clients["mdf"] = None
        # Other issue
        except globus_sdk.GlobusAPIError as e:
            print_("Error: Unable to create MDF Authorizer (" + e.message + ").")
            clients["mdf"] = None
        # Remove processed service
        servs.remove("mdf")

    if "publish" in servs:
        try:
            publish_authorizer = globus_sdk.RefreshTokenAuthorizer(
                                        all_tokens["publish.api.globus.org"]["refresh_token"],
                                        native_client)
            clients["publish"] = DataPublicationClient(authorizer=publish_authorizer)
        # Token not present
        except KeyError:
            print_("Error: Unable to retrieve Publish tokens.\n"
                   "You may need to delete your old tokens and retry.")
            clients["publish"] = None
        # Other issue
        except globus_sdk.GlobusAPIError as e:
            print_("Error: Unable to create Publish client (" + e.message + ").")
            clients["publish"] = None
        # Remove processed service
        servs.remove("publish")

    # Warn of invalid services
    if servs:
        print_("\n".join(["Unknown or invalid service: '" + sv + "'." for sv in servs]))

    return clients
示例#14
0
def confidential_login(credentials=None):
    """Login to Globus services as a confidential client (a client with its own login information).

    Arguments:
    credentials (str or dict): A string filename, string JSON, or dictionary
                                   with credential and config information.
                               By default, uses the DEFAULT_CRED_FILENAME and DEFAULT_CRED_PATH.
        Contains:
        client_id (str): The ID of the client.
        client_secret (str): The client's secret for authentication.
        services (list of str): Services to authenticate with.
                                Services are listed in AUTH_SCOPES.

    Returns:
    dict: The clients and authorizers requested, indexed by service name.
          For example, if confidential_login() is told to auth with 'search'
            then the search client will be in the 'search' field.
    """
    DEFAULT_CRED_FILENAME = "confidential_globus_login.json"
    DEFAULT_CRED_PATH = os.path.expanduser("~/mdf/credentials")
    # Read credentials
    if type(credentials) is str:
        try:
            with open(credentials) as cred_file:
                creds = json.load(cred_file)
        except IOError:
            try:
                creds = json.loads(credentials)
            except ValueError:
                raise ValueError("Credentials unreadable or missing")
    elif type(credentials) is dict:
        creds = credentials
    else:
        try:
            with open(os.path.join(os.getcwd(), DEFAULT_CRED_FILENAME)) as cred_file:
                creds = json.load(cred_file)
        except IOError:
            try:
                with open(os.path.join(DEFAULT_CRED_PATH, DEFAULT_CRED_FILENAME)) as cred_file:
                    creds = json.load(cred_file)
            except IOError:
                raise ValueError("Credentials/configuration must be passed as a "
                                 + "filename string, JSON string, or dictionary, or provided in '"
                                 + DEFAULT_CRED_FILENAME
                                 + "' or '"
                                 + DEFAULT_CRED_PATH
                                 + "'.")

    conf_client = globus_sdk.ConfidentialAppAuthClient(creds["client_id"], creds["client_secret"])
    servs = []
    for serv in creds["services"]:
        serv = serv.lower().strip()
        if type(serv) is str:
            servs += serv.split(" ")
        else:
            servs += list(serv)

    clients = {}
    if "transfer" in servs:
        clients["transfer"] = globus_sdk.TransferClient(
                                authorizer=globus_sdk.ClientCredentialsAuthorizer(
                                                conf_client, scopes=AUTH_SCOPES["transfer"]))
        # Remove processed service
        servs.remove("transfer")

    if "search_ingest" in servs:
        clients["search_ingest"] = globus_sdk.SearchClient(
                                        authorizer=globus_sdk.ClientCredentialsAuthorizer(
                                            conf_client, scopes=AUTH_SCOPES["search_ingest"]))
        # Remove processed service
        servs.remove("search_ingest")
        # And redundant service
        try:
            servs.remove("search")
        # No issue if it isn't there
        except Exception:
            pass
    elif "search" in servs:
        clients["search"] = globus_sdk.SearchClient(
                                authorizer=globus_sdk.ClientCredentialsAuthorizer(
                                    conf_client, scopes=AUTH_SCOPES["search"]))
        # Remove processed service
        servs.remove("search")

    if "mdf" in servs:
        clients["mdf"] = globus_sdk.ClientCredentialsAuthorizer(
                                conf_client, scopes=AUTH_SCOPES["mdf"])
        # Remove processed service
        servs.remove("mdf")

    if "publish" in servs:
        clients["publish"] = DataPublicationClient(
                                authorizer=globus_sdk.ClientCredentialsAuthorizer(
                                                conf_client, scopes=AUTH_SCOPES["publish"]))
        # Remove processed service
        servs.remove("publish")

    # Warn of invalid services
    if servs:
        print_("\n".join(["Unknown or invalid service: '" + sv + "'." for sv in servs]))

    return clients
示例#15
0
    def __init__(self, dlh_authorizer: Optional[GlobusAuthorizer] = None,
                 search_authorizer: Optional[GlobusAuthorizer] = None,
                 fx_authorizer: Optional[GlobusAuthorizer] = None,
                 openid_authorizer: Optional[GlobusAuthorizer] = None,
                 http_timeout: Optional[int] = None,
                 force_login: bool = False, **kwargs):
        """Initialize the client

        Args:
            http_timeout (int): Timeout for any call to service in seconds. (default is no timeout)
            force_login (bool): Whether to force a login to get new credentials.
                A login will always occur if ``dlh_authorizer`` or ``search_client``
                are not provided.
            no_local_server (bool): Disable spinning up a local server to automatically
                copy-paste the auth code. THIS IS REQUIRED if you are on a remote server.
                When used locally with no_local_server=False, the domain is localhost with
                a randomly chosen open port number.
                **Default**: ``True``.
            no_browser (bool): Do not automatically open the browser for the Globus Auth URL.
                Display the URL instead and let the user navigate to that location manually.
                **Default**: ``True``.
            dlh_authorizer (:class:`GlobusAuthorizer <globus_sdk.authorizers.base.GlobusAuthorizer>`):
                An authorizer instance used to communicate with DLHub.
                If ``None``, will be created from your account's credentials.
            search_authorizer (:class:`GlobusAuthorizer <globus_sdk.authorizers.base.GlobusAuthorizer>`):
                An authenticated SearchClient to communicate with Globus Search.
                If ``None``, will be created from your account's credentials.
            fx_authorizer (:class:`GlobusAuthorizer
                            <globus_sdk.authorizers.base.GlobusAuthorizer>`):
                An authorizer instance used to communicate with funcX.
                If ``None``, will be created from your account's credentials.
            openid_authorizer (:class:`GlobusAuthorizer
                            <globus_sdk.authorizers.base.GlobusAuthorizer>`):
                An authorizer instance used to communicate with OpenID.
                If ``None``, will be created from your account's credentials.
        Keyword arguments are the same as for :class:`BaseClient <globus_sdk.base.BaseClient>`.
        """

        authorizers = [dlh_authorizer, search_authorizer, openid_authorizer, fx_authorizer]
        # Get authorizers through Globus login if any are not provided
        if not all(a is not None for a in authorizers):
            # If some but not all were provided, warn the user they could be making a mistake
            if any(a is not None for a in authorizers):
                logger.warning('You have defined some of the authorizers but not all. DLHub is falling back to login. '
                               'You must provide authorizers for DLHub, Search, OpenID, FuncX.')

            fx_scope = "https://auth.globus.org/scopes/facd7ccc-c5f4-42aa-916b-a0e270e2c2a9/all"
            auth_res = login(services=["search", "dlhub",
                                       fx_scope, "openid"],
                             app_name="DLHub_Client",
                             make_clients=False,
                             client_id=CLIENT_ID,
                             clear_old_tokens=force_login,
                             token_dir=_token_dir,
                             no_local_server=kwargs.get("no_local_server", True),
                             no_browser=kwargs.get("no_browser", True))

            # Unpack the authorizers
            dlh_authorizer = auth_res["dlhub"]
            fx_authorizer = auth_res[fx_scope]
            openid_authorizer = auth_res['openid']
            search_authorizer = auth_res['search']

        # Define the subclients needed by the service
        self._fx_client = FuncXClient(fx_authorizer=fx_authorizer,
                                      search_authorizer=search_authorizer,
                                      openid_authorizer=openid_authorizer,
                                      no_local_server=kwargs.get("no_local_server", True),
                                      no_browser=kwargs.get("no_browser", True))
        self._search_client = globus_sdk.SearchClient(authorizer=search_authorizer,
                                                      http_timeout=5 * 60)

        # funcX endpoint to use
        self.fx_endpoint = '86a47061-f3d9-44f0-90dc-56ddc642c000'
        self.fx_cache = {}
        super(DLHubClient, self).__init__("DLHub", environment='dlhub',
                                          authorizer=dlh_authorizer,
                                          http_timeout=http_timeout,
                                          base_url=DLHUB_SERVICE_ADDRESS,
                                          **kwargs)
示例#16
0
def load_search_client():
    tokens = load_tokens()
    auth = globus_sdk.AccessTokenAuthorizer(
        tokens['search.api.globus.org']['access_token'])
    return globus_sdk.SearchClient(authorizer=auth)