-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.py
More file actions
executable file
·82 lines (66 loc) · 2.3 KB
/
Copy pathsearch.py
File metadata and controls
executable file
·82 lines (66 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
"""
Basic search against the Serviceware Knowledge REST API.
Authenticates with username/password, then issues a single POST /search request
and prints the top hits.
Requires:
pip install requests
Usage:
python search.py
"""
import sys
import requests
# ---------------------------------------------------------------------------
# Configuration -- replace placeholders with values for your environment.
# ---------------------------------------------------------------------------
BASE_URL = "https://<your-instance>/sabio-web/services"
USERNAME = "<username>"
PASSWORD = "<password>"
QUERY = "vacation policy"
LIMIT = 10
def login(base_url: str, username: str, password: str) -> str:
"""Exchange credentials for a session token."""
response = requests.post(
f"{base_url}/authentication/credentials",
json={"login": username, "key": password},
headers={"Content-Type": "application/json; charset=utf-8"},
timeout=30,
)
response.raise_for_status()
token = response.json().get("data", {}).get("key")
if not token:
raise RuntimeError(f"Login failed: {response.text}")
return token
def search(base_url: str, token: str, query: str, limit: int) -> dict:
"""Run a single search request."""
response = requests.post(
f"{base_url}/search",
json={
"query": query,
"limit": limit,
"fields": ["id", "title", "resource", "score", "lastModified", "excerpt"],
},
headers={
"Content-Type": "application/json; charset=utf-8",
"sabio-auth-token": token,
},
timeout=30,
)
response.raise_for_status()
return response.json()
def main() -> int:
token = login(BASE_URL, USERNAME, PASSWORD)
payload = search(BASE_URL, token, QUERY, LIMIT)
data = payload.get("data", {})
total = data.get("total", 0)
hits = data.get("result", []) or []
print(f"Query: {QUERY!r} -- {total} total hits, showing {len(hits)}.")
for index, hit in enumerate(hits, start=1):
print(
f" {index:>2}. [{hit.get('resource', '?')}] "
f"{hit.get('title', '<untitled>')} "
f"(score={hit.get('score')}, id={hit.get('id')})"
)
return 0
if __name__ == "__main__":
sys.exit(main())