Ejemplo n.º 1
0
    def search_one(self,
                   stac_request: stac_pb2.StacRequest,
                   timeout=15,
                   nsl_id: str = None,
                   profile_name: str = None) -> stac_pb2.StacItem:
        """
        search for one item from the db that matches the stac request
        :param timeout: timeout for request
        :param stac_request: StacRequest of query parameters to filter by
        :param nsl_id: ADVANCED ONLY. Only necessary if more than one nsl_id and nsl_secret have been defined with
        set_credentials method.  Specify nsl_id to use. if NSL_ID and NSL_SECRET environment variables not set must use
        NSLClient object's set_credentials to set credentials
        :param profile_name: if a ~/.nsl/credentials file exists, you can override the [default] credential usage, by
        using a different profile name
        :return: StacItem
        """
        # limit to only search Near Space Labs SWIFT data
        if self._nsl_only:
            stac_request.mission_enum = stac_pb2.SWIFT

        metadata = (('authorization',
                     bearer_auth.auth_header(nsl_id=nsl_id,
                                             profile_name=profile_name)), )
        return self._stac_service.stub.SearchOneItem(stac_request,
                                                     timeout=timeout,
                                                     metadata=metadata)
Ejemplo n.º 2
0
    def count(self,
              stac_request: stac_pb2.StacRequest,
              timeout=15,
              nsl_id: str = None,
              profile_name: str = None) -> int:
        """
        count all the items in the database that match the stac request
        :param timeout: timeout for request
        :param stac_request: StacRequest query parameters to apply to count method (limit ignored)
        :param nsl_id: ADVANCED ONLY. Only necessary if more than one nsl_id and nsl_secret have been defined with
        set_credentials method.  Specify nsl_id to use. if NSL_ID and NSL_SECRET environment variables not set must use
        NSLClient object's set_credentials to set credentials
        :param profile_name: if a ~/.nsl/credentials file exists, you can override the [default] credential usage, by
        using a different profile name
        :return: int
        """
        # limit to only search Near Space Labs SWIFT data
        if self._nsl_only:
            stac_request.mission_enum = stac_pb2.SWIFT

        metadata = (('authorization',
                     bearer_auth.auth_header(nsl_id=nsl_id,
                                             profile_name=profile_name)), )
        db_result = self._stac_service.stub.CountItems(stac_request,
                                                       timeout=timeout,
                                                       metadata=metadata)
        return db_result.count
Ejemplo n.º 3
0
def download_href_object(asset: Asset,
                         file_obj: IO = None,
                         save_filename: str = "",
                         nsl_id: str = None):
    """
    download the href of an asset
    :param asset: The asset to download
    :param file_obj: BinaryIO file object to download data into. If file_obj and save_filename and/or save_directory
    are set, then only file_obj is used
    :param save_filename: absolute or relative path filename to save asset to (must have write permissions)
    :param nsl_id: ADVANCED ONLY. Only necessary if more than one nsl_id and nsl_secret have been defined with
    set_credentials method.  Specify nsl_id to use. if NSL_ID and NSL_SECRET environment variables not set must use
        NSLClient object's set_credentials to set credentials
    :return: returns the save_filename. if BinaryIO is not a FileIO object type, save_filename returned is an
    empty string
    """
    if not asset.href:
        raise ValueError("no href on asset")

    host = urlparse(asset.href)
    conn = http.client.HTTPConnection(host.netloc)

    headers = {}
    asset_url = host.path
    if asset.bucket_manager == "Near Space Labs":
        headers = {"authorization": bearer_auth.auth_header(nsl_id=nsl_id)}
        asset_url = "/download/{object}".format(object=asset.object_path)

    if len(asset.type) > 0:
        headers["content-type"] = asset.type
    conn.request(method="GET", url=asset_url, headers=headers)

    res = conn.getresponse()
    if res.status == 404:
        raise ValueError("not found error for {path}".format(path=asset.href))
    elif res.status == 403:
        raise ValueError(
            "auth error for asset {asset}".format(asset=asset.href))
    elif res.status == 402:
        raise ValueError(
            "not enough credits for downloading asset {asset}".format(
                asset=asset.href))
    elif res.status != 200:
        raise ValueError("error code {code} for asset: {asset}".format(
            code=res.status, asset=asset.href))

    if len(save_filename) > 0:
        with open(save_filename, mode='wb') as f:
            f.write(res.read())
    elif file_obj is not None:
        file_obj.write(res.read())
        if "name" in file_obj.__dict__:
            save_filename = file_obj.name
        else:
            save_filename = ""
        file_obj.seek(0)
    else:
        raise ValueError("must provide filename or file_obj")

    return save_filename
Ejemplo n.º 4
0
 def insert_one(self,
                stac_item: stac_pb2.StacItem,
                timeout=15,
                nsl_id: str = None,
                profile_name: str = None) -> stac_pb2.StacDbResponse:
     """
     Insert on item into the stac service
     :param nsl_id: ADVANCED ONLY. Only necessary if more than one nsl_id and nsl_secret have been defined with
     set_credentials method.  Specify nsl_id to use. if NSL_ID and NSL_SECRET environment variables not set must use
     NSLClient object's set_credentials to set credentials
     :param timeout: timeout for request
     :param stac_item: item to insert
     :param profile_name: if a ~/.nsl/credentials file exists, you can override the [default] credential usage, by
     using a different profile name
     :return: StacDbResponse, the response of the success of the insert
     """
     metadata = (('authorization',
                  bearer_auth.auth_header(nsl_id=nsl_id,
                                          profile_name=profile_name)), )
     return self._stac_service.stub.InsertOneItem(stac_item,
                                                  timeout=timeout,
                                                  metadata=metadata)
Ejemplo n.º 5
0
def download_href_object(asset: Asset,
                         file_obj: BinaryIO = None,
                         save_filename: str = ""):
    """
    download the href of an asset
    :param asset: The asset to download
    :param file_obj: BinaryIO file object to download data into. If file_obj and save_filename and/or save_directory
    are set, then only file_obj is used
    :param save_filename: absolute or relative path filename to save asset to (must have write permissions)
    :return:
    """

    headers = {"authorization": bearer_auth.auth_header()}
    if len(asset.type) > 0:
        headers["content-type"] = asset.type

    host = urlparse(asset.href)
    asset_url = "/download/{object}".format(object=asset.object_path)
    conn = http.client.HTTPConnection(host.netloc)
    conn.request(method="GET", url=asset_url, headers=headers)

    res = conn.getresponse()
    if res.status is not 200:
        raise ValueError("{path} does not exist".format(path=asset_url))

    if len(save_filename) > 0:
        with open(save_filename, mode='wb') as f:
            f.write(res.read())
    elif file_obj is not None:
        file_obj.write(res.read())
        save_filename = file_obj.name
        file_obj.seek(0)
    else:
        raise ValueError("must provide filename or file_obj")

    return save_filename