|
1 | 1 | import json
|
2 | 2 | import os
|
3 |
| -from typing import NoReturn, Optional |
| 3 | +from typing import NoReturn, Optional, Literal |
4 | 4 |
|
5 | 5 | import pandas as pd
|
6 | 6 | import peppy
|
|
21 | 21 | PEPHUB_PUSH_URL,
|
22 | 22 | RegistryPath,
|
23 | 23 | ResponseStatusCodes,
|
| 24 | + PEPHUB_PEP_SEARCH_URL, |
24 | 25 | )
|
25 | 26 | from pephubclient.exceptions import (
|
26 | 27 | IncorrectQueryStringError,
|
|
29 | 30 | )
|
30 | 31 | from pephubclient.files_manager import FilesManager
|
31 | 32 | from pephubclient.helpers import MessageHandler, RequestManager
|
32 |
| -from pephubclient.models import ProjectDict, ProjectUploadData |
| 33 | +from pephubclient.models import ( |
| 34 | + ProjectDict, |
| 35 | + ProjectUploadData, |
| 36 | + SearchReturnModel, |
| 37 | + ProjectAnnotationModel, |
| 38 | +) |
33 | 39 | from pephubclient.pephub_oauth.pephub_oauth import PEPHubAuth
|
34 | 40 |
|
35 | 41 | urllib3.disable_warnings()
|
@@ -182,9 +188,65 @@ def upload(
|
182 | 188 | "User does not have permission to write to this namespace!"
|
183 | 189 | )
|
184 | 190 | else:
|
185 |
| - raise ResponseError("Unexpected Response Error.") |
| 191 | + raise ResponseError( |
| 192 | + f"Unexpected Response Error. {pephub_response.status_code}" |
| 193 | + ) |
186 | 194 | return None
|
187 | 195 |
|
| 196 | + def find_project( |
| 197 | + self, |
| 198 | + namespace: str, |
| 199 | + query_string: str = "", |
| 200 | + limit: int = 100, |
| 201 | + offset: int = 0, |
| 202 | + filter_by: Literal["submission_date", "last_update_date"] = None, |
| 203 | + start_date: str = None, |
| 204 | + end_date: str = None, |
| 205 | + ) -> SearchReturnModel: |
| 206 | + """ |
| 207 | + Find project in specific namespace and return list of PEP annotation |
| 208 | +
|
| 209 | + :param namespace: Namespace where to search for projects |
| 210 | + :param query_string: Search query |
| 211 | + :param limit: Return limit |
| 212 | + :param offset: Return offset |
| 213 | + :param filter_by: Use filter date. Option: [submission_date, last_update_date] |
| 214 | + :param start_date: filter beginning date |
| 215 | + :param end_date: filter end date (if none today's date is used) |
| 216 | + :return: |
| 217 | + """ |
| 218 | + jwt_data = FilesManager.load_jwt_data_from_file(self.PATH_TO_FILE_WITH_JWT) |
| 219 | + |
| 220 | + query_param = { |
| 221 | + "q": query_string, |
| 222 | + "limit": limit, |
| 223 | + "offset": offset, |
| 224 | + } |
| 225 | + if filter_by in ["submission_date", "last_update_date"]: |
| 226 | + query_param["filter_by"] = filter_by |
| 227 | + query_param["filter_start_date"] = start_date |
| 228 | + if end_date: |
| 229 | + query_param["filter_end_date"] = end_date |
| 230 | + |
| 231 | + url = self._build_project_search_url( |
| 232 | + namespace=namespace, |
| 233 | + query_param=query_param, |
| 234 | + ) |
| 235 | + |
| 236 | + pephub_response = self.send_request( |
| 237 | + method="GET", |
| 238 | + url=url, |
| 239 | + headers=self._get_header(jwt_data), |
| 240 | + json=None, |
| 241 | + cookies=None, |
| 242 | + ) |
| 243 | + if pephub_response.status_code == ResponseStatusCodes.OK: |
| 244 | + decoded_response = self._handle_pephub_response(pephub_response) |
| 245 | + project_list = [] |
| 246 | + for project_found in json.loads(decoded_response)["items"]: |
| 247 | + project_list.append(ProjectAnnotationModel(**project_found)) |
| 248 | + return SearchReturnModel(**json.loads(decoded_response)) |
| 249 | + |
188 | 250 | @staticmethod
|
189 | 251 | def _save_raw_pep(
|
190 | 252 | reg_path: str,
|
@@ -334,6 +396,21 @@ def _build_pull_request_url(self, query_param: dict = None) -> str:
|
334 | 396 |
|
335 | 397 | return PEPHUB_PEP_API_BASE_URL + endpoint
|
336 | 398 |
|
| 399 | + def _build_project_search_url( |
| 400 | + self, namespace: str, query_param: dict = None |
| 401 | + ) -> str: |
| 402 | + """ |
| 403 | + Build request for searching projects form pephub |
| 404 | +
|
| 405 | + :param query_param: dict of parameters used in query string |
| 406 | + :return: url string |
| 407 | + """ |
| 408 | + |
| 409 | + variables_string = PEPHubClient._parse_query_param(query_param) |
| 410 | + endpoint = variables_string |
| 411 | + |
| 412 | + return PEPHUB_PEP_SEARCH_URL.format(namespace=namespace) + endpoint |
| 413 | + |
337 | 414 | @staticmethod
|
338 | 415 | def _build_push_request_url(namespace: str) -> str:
|
339 | 416 | """
|
|
0 commit comments