diff --git a/README.md b/README.md index 44627b1..9cf62fc 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ Returns a JSON object with the following schema: "desktop": 11 } }, - ... + ... ] ``` @@ -51,8 +51,8 @@ This endpoint can return a full list of categories names or a categories with al The following parameters can be used to filter the data: -- `category` (`required`): A comma-separated string representing the category name(s). -- `onlyname` (optional): A string 'true' or 'false'. +- `category` (optional): A comma-separated string representing the category name(s). +- `onlyname` (optional): No value required. If present, only the category names will be returned. #### Response @@ -63,45 +63,46 @@ curl --request GET \ ```json [ - { - "description": "Solutions that redirect domains to a different location or page", - "technologies": [ - "Arsys Domain Parking" - ], - "origins": { - "mobile": 14, - "desktop": 8 - }, - "category": "Domain parking" - }, - { - "description": "Systems that automate building, testing, and deploying code", - "technologies": [ - "Jenkins", - "TeamCity" - ], - "origins": { - "mobile": 22, - "desktop": 35 + { + "description": "Systems that automate building, testing, and deploying code", + "technologies": [ + "Jenkins", + "TeamCity" + ], + "origins": { + "mobile": 22, + "desktop": 35 + }, + "category": "CI" }, - "category": "CI" - } + { + "description": "Solutions that redirect domains to a different location or page", + "technologies": [ + "Cloudflare", + "Arsys Domain Parking" + ], + "origins": { + "mobile": 14, + "desktop": 8 + }, + "category": "Domain parking" + } ] ``` ```bash curl --request GET \ - --url 'https://{{HOST}}/v1/categories?onlyname=true' + --url 'https://{{HOST}}/v1/categories?onlyname' ``` ```json [ - "Blogs", - "LMS", - "CI", - "Cross border ecommerce", - "Cart abandonment", - "Domain parking", + "A/B Testing", + "Accessibility", + "Accounting", + "Advertising", + "Affiliate programs", + "Analytics", ... ] @@ -245,31 +246,50 @@ Returns a JSON object with the following schema: The following parameters can be used to filter the data: -- `technology` (`required`): A comma-separated string representing the technology name(s). -- `start` (optional): A string representing the start date in the format `YYYY-MM-DD`. -- `end` (optional): A string representing the end date in the format `YYYY-MM-DD`. -- `geo` (optional): A string representing the geographic location. -- `rank` (optional): An string representing the rank. +- `client` (optional): A string with the client: `mobile` or `desktop`. +- `technology` (optional): A comma-separated string representing the technology name(s) or `ALL`. - `category` (optional): A comma-separated string representing the category name(s). +- `onlyname` (optional): No value required. If present, only the technology names will be returned. #### Response ```bash curl --request GET \ - --url 'https://{{HOST}}/v1/technologies?start=2022-02-01&end=2022-04-01&category=Live%20chat%2C%20blog&technology=Smartsupp&client=mobile' + --url 'https://{{HOST}}/v1/technologies?category=Live%20chat%2C%20blog&technology=Smartsupp&client=mobile' ``` Returns a JSON object with the following schema: ```json [ - { - "client": "mobile", - "similar_technologies": null, - "description": "Smartsupp is a live chat tool that offers visitor recording feature.", - "origins": 25649, - "technology": "Smartsupp", - "category": "Live chat" - } + { + "client": "mobile", + "similar_technologies": null, + "description": "Smartsupp is a live chat tool that offers visitor recording feature.", + "origins": 25649, + "technology": "Smartsupp", + "category": "Live chat" + } ] ``` + +```bash +curl --request GET \ + --url 'https://{{HOST}}/v1/technologies?onlyname' +``` + +Returns a JSON object with the following schema: + +```json +[ + "1C-Bitrix", + "2B Advice", + "33Across", + "34SP.com", + "4-Tell", + "42stores", + "51.LA", + "5centsCDN", + ... +} +``` diff --git a/functions/categories/libs/queries.py b/functions/categories/libs/queries.py index 83ae07b..141b14d 100644 --- a/functions/categories/libs/queries.py +++ b/functions/categories/libs/queries.py @@ -1,41 +1,34 @@ import os from google.cloud import firestore -from google.cloud.firestore_v1.base_query import FieldFilter -from .result import Result +from google.cloud.firestore_v1.base_query import FieldFilter, Or +from .result import Result from .utils import convert_to_array -DB = firestore.Client(project=os.environ.get('PROJECT'), database=os.environ.get('DATABASE')) -TABLE = 'categories' +DB = firestore.Client( + project=os.environ.get("PROJECT"), database=os.environ.get("DATABASE") +) def list_data(params): - ref = DB.collection(TABLE) + ref = DB.collection("categories") - query = ref + query = ref.order_by("category", direction=firestore.Query.ASCENDING) - data = [] + if "category" in params: + category_array = convert_to_array(params["category"]) + filter_array = [] + for category in category_array: + filter_array.append(FieldFilter("category", "==", category)) + query = query.where(filter=Or(filters=filter_array)) - if 'onlyname' in params: documents = query.stream() + data = [] - for doc in documents: - item = doc.to_dict() - if 'category' in item: - data.append(item['category']) - - else: - - if 'category' in params: - category_array = convert_to_array(params['category']) - - for category in category_array: - results = query.where(filter=FieldFilter("category", "==", category)).stream() - for doc in results: - data.append(doc.to_dict()) + if "onlyname" in params: + for doc in documents: + data.append(doc.get("category")) else: - documents = query.stream() - - for doc in documents: - data.append(doc.to_dict()) + for doc in documents: + data.append(doc.to_dict()) - return Result(result=data) + return Result(result=data) diff --git a/functions/technologies/libs/queries.py b/functions/technologies/libs/queries.py index 9b9c2b3..f48c02c 100644 --- a/functions/technologies/libs/queries.py +++ b/functions/technologies/libs/queries.py @@ -3,53 +3,48 @@ from google.cloud import firestore from google.cloud.firestore_v1.base_query import FieldFilter, Or -from .result import Result +from .result import Result from .utils import convert_to_array from .presenters import Presenters -DB = firestore.Client(project=os.environ.get('PROJECT'), database=os.environ.get('DATABASE')) +DB = firestore.Client( + project=os.environ.get("PROJECT"), database=os.environ.get("DATABASE") +) def list_data(params): - onlyname = False - ref = DB.collection('technologies') - - query = ref - - if 'technology' in params: - arfilters = [] - params_array = convert_to_array(params['technology']) - for tech in params_array: - arfilters.append(FieldFilter('technology', '==', tech)) - - or_filter = Or(filters=arfilters) - - query = query.where(filter=or_filter) - - if 'category' in params: - params_array = convert_to_array(params['category']) - query = query.where(filter=FieldFilter('category_obj', 'array_contains_any', params_array)) - - if 'client' in params: - query = query.where(filter=FieldFilter('client', '==', params['client'])) - - if 'onlyname' in params: - onlyname = True - - if 'sort' not in params: - query = query.order_by('technology', direction=firestore.Query.ASCENDING) - else: - if params['sort'] == 'origins': - query = query.order_by('origins', direction=firestore.Query.DESCENDING) - - - documents = query.stream() - - data = [] - for doc in documents: - item = doc.to_dict() - if onlyname: - data.append(item['technology']) - else: - data.append(Presenters.technology(doc.to_dict())) - - return Result(result=data) + ref = DB.collection("technologies") + + query = ref.order_by("technology", direction=firestore.Query.ASCENDING) + + if "technology" in params: + arfilters = [] + params_array = convert_to_array(params["technology"]) + for tech in params_array: + arfilters.append(FieldFilter("technology", "==", tech)) + query = query.where(filter=Or(filters=arfilters)) + + if "category" in params: + params_array = convert_to_array(params["category"]) + query = query.where( + filter=FieldFilter("category_obj", "array_contains_any", params_array) + ) + + if "client" in params: + query = query.where(filter=FieldFilter("client", "==", params["client"])) + + documents = query.stream() + data = [] + + if "onlyname" in params and "client" not in params: + appended_tech = set() + for doc in documents: + tech = doc.get("technology") + if tech not in appended_tech: + appended_tech.add(tech) + data.append(tech) + + else: + for doc in documents: + data.append(Presenters.technology(doc.to_dict())) + + return Result(result=data)