From d8256d577a6319d612f119aa4cb929f86c79a64e Mon Sep 17 00:00:00 2001
From: Vuillaume <thomas.vuillaume@lapp.in2p3.fr>
Date: Mon, 4 Oct 2021 09:14:42 +0000
Subject: [PATCH] handle lists and lowercase of args in search zenodo / ossr
 records

---
 eossr/api/__init__.py        | 19 +++++++++++++++++--
 eossr/api/tests/test_api.py  |  2 +-
 eossr/api/zenodo/__init__.py | 28 +++++++++++++++++++++++++---
 3 files changed, 43 insertions(+), 6 deletions(-)

diff --git a/eossr/api/__init__.py b/eossr/api/__init__.py
index 954b1902..c45bcba1 100644
--- a/eossr/api/__init__.py
+++ b/eossr/api/__init__.py
@@ -13,6 +13,7 @@ escape_community = 'escape2020'
 def get_ossr_records(search='', sandbox=False, **kwargs):
     """
     Search the OSSR for records whose names or descriptions include the provided string `search`.
+    The default record type is 'software' or 'record'.
     Function rewritten from pyzenodo3 (https://github.com/space-physics/pyzenodo3)
 
     :param search: string
@@ -22,12 +23,19 @@ def get_ossr_records(search='', sandbox=False, **kwargs):
         Common arguments are:
             - size: int
                 Number of results to return
+                Default = 100
             - all_versions: int
                 Show (1) or hide (0) all versions of records
-            - type: string
+            - type: string or list[string]
+                Default: ['software', 'dataset']
                 Records of the specified type (Publication, Poster, Presentation, Software, ...)
-            - keywords: string
+                A logical OR is applied in case of a list
+            - keywords: string or list[string]
                 Records with the specified keywords
+                 A logical OR is applied in case of a list
+            - file_type: string or list[string]
+                Records from the specified keywords
+                A logical OR is applied in case of a list
 
     :return:
     list of `Record`
@@ -41,7 +49,14 @@ def get_ossr_records(search='', sandbox=False, **kwargs):
     # if another community is specified, a logical OR is apply be zenodo API,
     # thus potentially finding entries that are not part of escape2020
     # ruling out that possibility at the moment
+    if 'communities' in kwargs and kwargs['communities'] != escape_community:
+        raise NotImplementedError("Searching in another community will search outside of the OSSR"
+                                  "Use `eossr.api.zenodo.get_zenodo_records` to do so"
+                                  )
     kwargs['communities'] = escape_community
 
+    # OSSR is limited to software and datasets
+    kwargs.setdefault('type', ['software', 'dataset'])
+
     return get_zenodo_records(search, sandbox=sandbox, **kwargs)
 
diff --git a/eossr/api/tests/test_api.py b/eossr/api/tests/test_api.py
index 48d9fcc5..610f2d7e 100644
--- a/eossr/api/tests/test_api.py
+++ b/eossr/api/tests/test_api.py
@@ -3,7 +3,7 @@ from eossr import api
 
 def test_get_ossr_records():
     ossr_records = api.get_ossr_records()
-    assert len(ossr_records) >= 15  # number of records September 10, 2021
+    assert len(ossr_records) >= 12  # number of records October 01, 2021
     all_ids = [rec.data['id'] for rec in ossr_records]
     assert 4923992 in all_ids  # id of the ESCAPE template project
 
diff --git a/eossr/api/zenodo/__init__.py b/eossr/api/zenodo/__init__.py
index bcff1320..0758e274 100644
--- a/eossr/api/zenodo/__init__.py
+++ b/eossr/api/zenodo/__init__.py
@@ -502,12 +502,21 @@ def get_zenodo_records(search='', sandbox=False, **kwargs):
         Common arguments are:
             - size: int
                 Number of results to return
+                Default = 100
             - all_versions: int
                 Show (1) or hide (0) all versions of records
-            - type: string
+            - type: string or list[string]
                 Records of the specified type (Publication, Poster, Presentation, Software, ...)
-            - keywords: string
+                A logical OR is applied in case of a list
+            - keywords: string or list[string]
                 Records with the specified keywords
+                 A logical OR is applied in case of a list
+            - communities: string or list[string]
+                Records from the specified keywords
+                A logical OR is applied in case of a list
+            - file_type: string or list[string]
+                Records from the specified keywords
+                A logical OR is applied in case of a list
 
     :return:
     list of `Record`
@@ -519,8 +528,21 @@ def get_zenodo_records(search='', sandbox=False, **kwargs):
         **kwargs
     }
 
+    params.setdefault('size', 100)
+
+    def lowercase(param):
+        if isinstance(param, str):
+            param = param.lower()
+        if isinstance(param, list):
+            param = [char.lower() for char in param]
+        return param
+
+    for param_name in ['communities', 'type', 'file_type']:
+        if param_name in kwargs:
+            params[param_name] = lowercase(kwargs[param_name])
+
     api_url = zenodo_sandobx_api_url if sandbox else zenodo_api_url
-    url = api_url + "/records?" + urlencode(params)
+    url = api_url + "/records?" + urlencode(params, doseq=True)
 
     recs = [Record(hit) for hit in requests.get(url).json()["hits"]["hits"]]
 
-- 
GitLab