Skip to content

Commit

Permalink
Remove operator and value in payload if they are None in AdvancedParams
Browse files Browse the repository at this point in the history
Sample:

```python
{
    'app': 'vanilla-search',
    'query': {
        'name': 'query',
        'text': 'himawari',
        'isFirstpage': False,
        'strictRefine': False,
        'removeDuplicates': False,
        'action': 'search',
        'page': 1,
        'pageSize': 10,
        'advanced': {
            'collection': '/user_needs_database/snwg-assessments-2020/',
            # 'value': None, 'operator': None,
        }
    }
}
```
  • Loading branch information
NISH1001 committed Feb 27, 2024
1 parent 8878504 commit 60db648
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
41 changes: 21 additions & 20 deletions pynequa/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@


class API:
'''
API Class handles all HTTP Requests
"""
API Class handles all HTTP Requests
Attributes:
base_url(string): REST API base URL for Sinequa instance
access_token(string): token for Sinequa authentication
'''
Attributes:
base_url(string): REST API base URL for Sinequa instance
access_token(string): token for Sinequa authentication
"""

def __init__(self, access_token: str, base_url: str) -> None:
if not access_token or not base_url:
Expand All @@ -21,29 +21,30 @@ def __init__(self, access_token: str, base_url: str) -> None:
self.base_url = base_url

def _get_headers(self) -> Dict:
headers = {
"Authorization": f"Bearer {self.access_token}"
}
headers = {"Authorization": f"Bearer {self.access_token}"}
return headers

def _get_url(self, endpoint) -> str:
return os.path.join(self.base_url, endpoint)

def get(self, endpoint) -> Dict:
"""
This method handles GET method.
This method handles GET method.
"""
session = requests.Session()
resp = session.get(self._get_url(endpoint=endpoint),
headers=self._get_headers())
session.close
return resp.json()
with requests.Session() as session:
resp = session.get(
self._get_url(endpoint=endpoint), headers=self._get_headers()
)
return resp.json()

def post(self, endpoint, payload) -> Dict:
"""
This method handles POST method.
This method handles POST method.
"""
session = requests.Session()
resp = session.post(self._get_url(endpoint=endpoint),
headers=self._get_headers(), json=payload)
return resp.json()
with requests.Session() as session:
resp = session.post(
self._get_url(endpoint=endpoint),
headers=self._get_headers(),
json=payload,
)
return resp.json()
13 changes: 8 additions & 5 deletions pynequa/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class TreeParams(AbstractParams):
Possible values: '=', '!=', '<', '<=', '>=', '>', 'between', 'not between'.
value (str): The filter value (required).
"""

box: str = ""
column: str = ""
op: str = ""
Expand Down Expand Up @@ -85,7 +86,7 @@ def generate_payload(self, **kwargs) -> Dict:
class AdvancedParams(AbstractParams):
col_name: str = ""
col_value: str = None
value: str or int = None
value: str or int = None
operator: str = None
debug: bool = False

Expand All @@ -96,10 +97,13 @@ def generate_payload(self, **kwargs) -> Dict:
"""
payload = {
self.col_name: self.col_value,
"value": self.value,
"operator": self.operator
}

if self.value:
payload["value"] = self.value
if self.operator:
payload["operator"] = self.operator

if self.debug:
logger.debug(payload)

Expand All @@ -111,8 +115,7 @@ class QueryParams(AbstractParams):
name: str = "" # required
action: Optional[str] = None
search_text: str = "" # required
select_params: Optional[List[SelectParams]
] = field(default_factory=lambda: [])
select_params: Optional[List[SelectParams]] = field(default_factory=lambda: [])
additional_select_clause: Optional[str] = None
additional_where_clause: Optional[str] = None
open_params: Optional[List[OpenParams]] = field(default_factory=lambda: [])
Expand Down

0 comments on commit 60db648

Please sign in to comment.