Ejemplo n.º 1
0
 def get_status(cls, api_root, status_id):
     log_debug(f'Request to Get the status of {status_id} from {api_root}')
     try:
         result = cls.es_client.get_doc(index=f'{api_root}-status',
                                        doc_id=status_id)
         return result['data']
     except Exception as e:
         log_error(e)
         return EXCEPTIONS.get('StatusNotFoundException')
Ejemplo n.º 2
0
 def get_api_root_information(cls, api_root):
     log_debug(f'Request to Get {api_root} Root Information')
     api_root_list = cls.es_client.get_doc(
         index='discovery', doc_id='discovery').get('data')['api_roots']
     if api_root in str(api_root_list):
         result = cls.es_client.get_doc(index='feeds', doc_id=api_root)
         return result['data']['information']
     else:
         return EXCEPTIONS.get('APIRootNotFoundException')
Ejemplo n.º 3
0
 def get_collection(cls, api_root, collection_id):
     log_debug(
         f'Request to Get Collection {collection_id} from Feed: {api_root}')
     try:
         result = cls.es_client.get_doc(index=f'{api_root}-collections',
                                        doc_id=collection_id).get('data')
         return result
     except Exception as e:
         log_error(e)
         return EXCEPTIONS.get('CollectionNotFoundException', {})
Ejemplo n.º 4
0
    def roots_discovery(cls):
        log_debug('Request to Get Discovery Info')

        try:
            result = cls.es_client.get_doc(index='discovery',
                                           doc_id='discovery')
            return result['data']
        except Exception as e:
            log_error(e)
            return EXCEPTIONS.get('DiscoveryException', {})
Ejemplo n.º 5
0
    def get_collection_objects(cls, api_root, collection_id):
        log_debug(f'Request to Get The objects of Collection: {collection_id} in the Feed Root: {api_root}')

        try:
            result = cls.es_client.get_doc(index=f'{api_root}-collections', doc_id=collection_id)['data']['objects']
            return {
                'objects': result
            }
        except Exception as e:
            log_error(e)
            return EXCEPTIONS.get('CollectionNotFoundException', {})
Ejemplo n.º 6
0
 def get_collections(cls, api_root):
     log_debug(f'Request to Get all Collections under {api_root} Root')
     try:
         result = cls.es_client.get_docs(
             index=f'{api_root}-collections').get('data')
         return {'collections': result}
     except NotFoundError as e:
         log_error(e)
         return EXCEPTIONS.get('APIRootNotFoundException', {})
     except Exception as e:
         log_error(e)
         return EXCEPTIONS.get('CollectionsNotFoundException', {})
Ejemplo n.º 7
0
 def get_default_root_information(cls):
     log_debug('Request to Get Default API Root Information')
     try:
         default_api_root_url = cls.es_client.get_doc(
             index='discovery', doc_id='discovery').get('data')['default']
         default_api_root = urlparse(default_api_root_url)[2].partition(
             '/')[2].partition('/')[0]
         result = cls.es_client.get_doc(index='feeds',
                                        doc_id=default_api_root)
         return result['data']['information']
     except Exception as e:
         log_error(e)
         return EXCEPTIONS.get('DefaultAPIRootNotFoundException')
Ejemplo n.º 8
0
    def get_collection_manifest(cls, api_root, **query_parameters):
        objects_query = None
        manifest_query = None
        version_range = None
        added_after_range = None
        size = int(query_parameters.get('limit'))
        max_page_size = PAGE_SIZE
        added_after = query_parameters.get('added_after')
        sort_by = {'date_added': {'order': 'asc'}}
        types = query_parameters.get('types')
        ids = query_parameters.get('ids')
        versions = query_parameters.get('versions')
        spec_versions = query_parameters.get('spec_versions')
        base_page = 0
        next_id = 0
        objects_query = None
        manifest_query = None
        version_range = None
        added_after_range = None

        log_debug(
            f"Request to Get The objects Manifest of Collection: {query_parameters.get('collection_id')} "
            f"in the Feed Root: {api_root}")

        if query_parameters is None:
            query_parameters = {}

        try:
            # Create a Query to filter Objects by collection id, types and spec_versions
            objects_query = f"collection : {query_parameters.get('collection_id')}"
            if types:
                types = types.replace(",", " OR ")
                objects_query = objects_query + f" AND type : ('{types}')"
            if spec_versions:
                spec_versions = spec_versions.replace(",", " OR ")
                objects_query = objects_query + f" AND spec_version : ('{spec_versions}')"
            objects_query_string = QueryString(query=objects_query,
                                               default_operator="and")

            # Create a Query to filter Manifest by collection id, object id's, versions and added after dates
            manifest_query = f"collection : {query_parameters.get('collection_id')}"
            if ids:
                ids = ids.replace(",", " OR ")
                manifest_query = manifest_query + f" AND id : ('{ids}')"
            if added_after:
                added_after_range = Range(
                    **{'date_added': {
                        'gt': f'{added_after}'
                    }})
            manifests_query_string = QueryString(query=manifest_query,
                                                 default_operator="and")

            # Get the intersect of both Objects and Manifest Queries
            intersected_results = cls.es_client.manifest_intersect(
                intersect_by='id',
                objects_index=f'{api_root}-objects',
                objects_query_string=objects_query_string,
                manifests_index=f'{api_root}-manifest',
                manifests_query_string=manifests_query_string,
                added_after_range=added_after_range)

            # Version and Paginate The Results
            if intersected_results:
                manifest_ids = ",".join(intersected_results).replace(
                    ',', ' OR ')
                query_string = QueryString(query=f"id:('{manifest_ids}')",
                                           default_operator="AND")
                pre_versioning_results = cls.es_client.scan(
                    index=f'{api_root}-manifest', query_string=query_string)
                pre_pagination_results = Helper.fetch_objects_by_versions(
                    stix_objects=pre_versioning_results, versions=versions)
                if -1 < size < max_page_size:
                    results = cls.es_client.search(
                        index=f'{api_root}-manifest',
                        query_string=query_string,
                        search_from=base_page,
                        size=size,
                        sort_by=sort_by)
                else:
                    results = cls.es_client.search(
                        index=f'{api_root}-manifest',
                        query_string=query_string,
                        search_from=base_page,
                        size=max_page_size,
                        sort_by=sort_by)
                results = {'objects': pre_pagination_results}
            else:
                results = {"objects": []}
            return results

        except Exception as e:
            log_error(e)
            if query_parameters.get('next'):
                return EXCEPTIONS.get('NextNotFoundException', {})
            else:
                return EXCEPTIONS.get('CollectionNotFoundException', {})