Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix advanced params #28

Merged
merged 3 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
19 changes: 11 additions & 8 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 @@ -94,11 +95,14 @@ def generate_payload(self, **kwargs) -> Dict:
This method generates payload for
AdvancedParams.
"""
payload = {
self.col_name: self.col_value,
"value": self.value,
"operator": self.operator
}
payload = {}
# To prevent payloads with empty values
if self.col_name and self.col_value:
payload[self.col_name] = self.col_value
if self.value:
anisbhsl marked this conversation as resolved.
Show resolved Hide resolved
payload["value"] = self.value
if self.operator:
anisbhsl marked this conversation as resolved.
Show resolved Hide resolved
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
Loading