Skip to content

Commit

Permalink
added classmethod for initializing Sinequa class
Browse files Browse the repository at this point in the history
  • Loading branch information
anisbhsl committed Oct 11, 2023
1 parent 241eaea commit 1a03ec7
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 76 deletions.
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
# Pynequa
A python library to handle communication with Sinequa REST API.
A python library to handle communication with Sinequa REST API.

[![Documentation Status](https://readthedocs.org/projects/pynequa/badge/?version=latest)](https://pynequa.readthedocs.io/en/latest/?badge=latest)
![Build](https://github.com/NASA-IMPACT/pynequa/actions/workflows/pkg_build_check.yml/badge.svg)

> Sinequa is an enterprise search tool. It provides a cognitive search and analytics platform that helps organizations gain insights from their structured and unstructured data spread across various sources, including databases, documents, emails, websites, and more.
## Installation
## Installation

```
$ pip install pynequa
```

## Example Usage
```
import pynequa
import pynequa
from pynequa.models import QueryParams
# provide following config parameters
# provide following config parameters
config = {
"base_url": "",
"app_name": "",
"base_url": "",
"app_name": "",
"access_token":"",
"query_name": ""
}
#initialize a Sinequa connector instance
sinequa=pynequa.Sinequa(config=config)
#initialize a Sinequa connector instance
sinequa=pynequa.Sinequa.from_config(config)
params = QueryParams()
params.search_text = "<your_search_text>"
..... #other params
..... #other params
#perform a search query operation
#perform a search query operation
results=sinequa.search_query(params)
```


## Feature Roadmap
Implement following REST endpoints to manage requests with Sinequa API.
## Feature Roadmap
Implement following REST endpoints to manage requests with Sinequa API.




**Search Endpoints:**
- [x] search.app
- [x] search.app
- [x] search.dataset
- [x] search.query
- [x] queryintent
Expand All @@ -64,12 +64,12 @@ Implement following REST endpoints to manage requests with Sinequa API.
- [ ] search.suggest
- [ ] search.custom
- [ ] suggestField
- [ ] engine.sql
- [ ] engine.sql

**Indexing Endpoints**
- [ ] indexing.collection
- [ ] indexing.customCollection
- [ ] indexing.partition
- [ ] indexing.partition

**Operating Task Endpoints**
- [ ] operation.actionStatus
Expand All @@ -83,15 +83,15 @@ Implement following REST endpoints to manage requests with Sinequa API.

**General Endpoints**
- [ ] audit.notify
- [ ] admin.config
- [ ] dev.plugin
- [ ] multi
- [ ] admin.config
- [ ] dev.plugin
- [ ] multi

## Development
Check [DEVELOPERS GUIDE](DEVELOPMENT.md) for details.
Check [DEVELOPERS GUIDE](DEVELOPMENT.md) for details.
## Contributing

When contributing to this repository, please first discuss the change you wish to make via issue, email, or any other method with the authors of this repository before making a change.
When contributing to this repository, please first discuss the change you wish to make via issue, email, or any other method with the authors of this repository before making a change.

## License

Expand Down
20 changes: 11 additions & 9 deletions pynequa/api/api.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import requests
import os
from typing import Dict

import os
import requests


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

def __init__(self, config) -> None:
self.access_token = config["access_token"]
self.base_url = config["base_url"]
def __init__(self, access_token: str, base_url: str) -> None:
if not access_token or not base_url:
raise ValueError("access_token and base_url must not be empty")

self.access_token = access_token
self.base_url = base_url

def _get_headers(self) -> Dict:
headers = {
Expand Down
119 changes: 76 additions & 43 deletions pynequa/core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from pynequa.api import API
from __future__ import annotations
from typing import Optional, List, Dict

from pynequa.api import API
from pynequa.models import QueryParams, TreeParams


Expand All @@ -8,17 +10,48 @@ class Sinequa(API):
Sinequa API Client for Python
Attributes:
access_token(str): authentication token for Sinequa
base_url(str): base URL for hosted Sinequa instance
app_name(str): name of Sinequa app
query_name(str): name of search query web service
query_name(str): name of search query web service
'''
app_name: str
query_name: str

def __init__(self, config: Dict) -> None:
super().__init__(config)
self.app_name = config["app_name"] # name of application
def __init__(
self,
access_token: str,
base_url: str,
app_name: str = "vanilla-search",
query_name: str = "query",
) -> None:
super().__init__(access_token=access_token, base_url=base_url)
self.app_name = app_name # name of application
# name of search query web service
self.query_name = config["query_name"]
self.query_name = query_name

@classmethod
def from_config(cls, cfg: Dict) -> Sinequa:
'''
This method creates an instance of Sinequa class from a configuration
dictionary.
Args:
cfg (Dict): A dictionary containing configuration parameters.
It should include the following keys:
- "access_token": A valid access token for authentication.
- "base_url": The base URL for the Sinequa API.
- "app_name": Name of the Sinequa application.
- "query_name": Name of the search query service.
Returns:
Sinequa: An instance of Sinequa class initialized with given configuration
'''
return cls(
access_token=cfg["access_token"],
base_url=cfg["base_url"],
app_name=cfg["app_name"],
query_name=cfg["query_name"],
)

@staticmethod
def _prepare_kwargs(payload: Dict, kwargs: Dict) -> Dict:
Expand All @@ -28,10 +61,10 @@ def _prepare_kwargs(payload: Dict, kwargs: Dict) -> Dict:

def search_app(self, pre_login: bool = False, mode: str = "debug") -> Dict:
'''
This method retrieves SBA configuration before and after login.
This method retrieves SBA configuration before and after login.
Args:
pre_login(bool): false by default.
Args:
pre_login(bool): false by default.
mode(str): debug by default (debug|release)
'''
endpoint = "search.app"
Expand All @@ -46,13 +79,13 @@ def search_dataset(self, parameters: Optional[Dict],
datasets: Optional[List[str]]) -> Dict:
'''
This method retrieves datasets through SQL queries. The response is a
list of available datasets with their respective names and descriptions.
list of available datasets with their respective names and descriptions.
Args:
parameters(dict): dictionary of parameters that can be used in SQL fragments
datasets(list): list of datasets
Returns:
Dict: search dataset response
Dict: search dataset response
'''
endpoint = "search.dataset"
Expand All @@ -70,10 +103,10 @@ def search_query(self, query_params: QueryParams) -> Dict:
This method performs search query.
Args:
query_params(QueryParams): query parameters as defined in QueryParams class
query_params(QueryParams): query parameters as defined in QueryParams class
Returns:
Dict: response data of this request
Dict: response data of this request
'''
endpoint = "search.query"

Expand All @@ -87,13 +120,13 @@ def search_query(self, query_params: QueryParams) -> Dict:

def query_intent(self, intent_text: str) -> Dict:
'''
This method evaluates SBA query intents from query intent sets.
This method evaluates SBA query intents from query intent sets.
Args:
intent_text(str): indicates the text that should trigger query intent
Returns:
Dict: Query Intent response
Dict: Query Intent response
'''
endpoint = "queryintent"
Expand All @@ -113,14 +146,14 @@ def query_intent(self, intent_text: str) -> Dict:
def search_profile(self, profile_name: str, query_params: QueryParams,
response_type: str = "SearchCursor") -> Dict:
'''
This method searches for Sienequa profile.
This method searches for Sienequa profile.
Args:
profile_name(str): profile name
response_type(str): default will be SearchCursor
response_type(str): default will be SearchCursor
query_params(QueryParams): will carry query parameters in payload
Returns:
Returns:
Dict: response data for Sinequa profile search.
'''
endpoint = "search.profile"
Expand All @@ -136,16 +169,16 @@ def search_profile(self, profile_name: str, query_params: QueryParams,
def search_user_settings(self, action: str = "load",
user_settings: Dict = {}) -> Dict:
'''
This method provides user settings
This method provides user settings
Args:
app_name(str): name of application for which user setting should be handled
action(str): search action (load|save|patch)
user_settings(Dict): user settings to be saved or patched (see official
user_settings(Dict): user settings to be saved or patched (see official
documentation for more info)
Returns:
Dict: search response based upon action
Returns:
Dict: search response based upon action
'''
endpoint = "search.usersettings"
payload = {
Expand All @@ -159,7 +192,7 @@ def search_user_settings(self, action: str = "load",
def search_preview(self, query_params: QueryParams, action: str = "get",
origin: str = "", id: str = "") -> Dict:
'''
This method retrieves preview data for a product.
This method retrieves preview data for a product.
Args:
query_params(QueryParams): query params with text to be searched
Expand Down Expand Up @@ -196,7 +229,7 @@ def search_query_export(self, web_service: str, type: str, format: str,
max_count(int): maximum of number of documents to include in export(optiona)
Returns:
Dict: response for search export
Dict: response for search export
'''
endpoint = "search.queryexport"
payload = {
Expand Down Expand Up @@ -239,10 +272,10 @@ def search_similardocuments(self, source_doc_id: str,
Args:
source_doc_id(str): identifier of document for which to retrieve similar documents
query_params(QueryParams): query params
query_params(QueryParams): query params
Returns:
Dict: search response for similar documents
Returns:
Dict: search response for similar documents
'''
endpoint = "search.similardocuments"

Expand All @@ -257,12 +290,12 @@ def search_similardocuments(self, source_doc_id: str,

def search_query_links(self, web_sevice: str, query_params: QueryParams) -> Dict:
'''
This method retrieves sponsored links for the passed query.
This method retrieves sponsored links for the passed query.
Args:
web_service(str): name of corresponding sponsored links web service
Returns:
Dict: response for query links search
Returns:
Dict: response for query links search
'''
endpoint = "search.querylinks"
Expand All @@ -277,20 +310,20 @@ def search_ratings(self, action: str, docid: str, ratings_column: str,
avg_column: str, ratings_distribution: List[str], rating: int,
update_doc_weight: bool) -> Dict:
'''
This method makes it possible to get, set and delete ratings associated with a
document.
This method makes it possible to get, set and delete ratings associated with a
document.
Args:
action(str): get|set|delete
docid(str): document id
docid(str): document id
ratings_column(str): name of column in which to store rating
avg_column(str): name of column to store average rating
ratings_distribution(List[str]): array of possible ratings
rating(int): sets the action parameter (optional)
update_doc_weight(bool): indicates whether to update the doc weight
update_doc_weight(bool): indicates whether to update the doc weight
according to rating (optional)
Returns:
Dict: response for ratings search
Returns:
Dict: response for ratings search
'''
endpoint = "search.ratings"
payload = {
Expand All @@ -310,15 +343,15 @@ def search_ratings(self, action: str, docid: str, ratings_column: str,
def search_profile_subtree(self, profile: str, query_params: QueryParams,
tree_params: TreeParams) -> Dict:
'''
This method allows to select a subtree.
This method allows to select a subtree.
Args:
Args:
profile(str): profile name
query_params(QueryParams): search parameters
query_params(QueryParams): search parameters
tree_params(TreeParams): defines the sub-tree to retrieve
Returns:
Dict: returns subtree profile response
Returns:
Dict: returns subtree profile response
'''
endpoint = "search.profile.subtree"
payload = {
Expand Down
Loading

0 comments on commit 1a03ec7

Please sign in to comment.