Skip to content

Commit 2b970fe

Browse files
authored
Merge pull request #6 from curtiscook/c/wildcard
Allow multiple string algos
2 parents 011b4cc + b730453 commit 2b970fe

File tree

4 files changed

+37
-13
lines changed

4 files changed

+37
-13
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ import pyxivapi
5757
from pyxivapi.models import Filter, Sort
5858

5959

60-
async def fetch_example_results(session):
60+
async def fetch_example_results():
6161
client = pyxivapi.XIVAPIClient(api_key="your_key_here")
6262

6363
# Search Lodestone for a character
@@ -113,7 +113,8 @@ async def fetch_example_results(session):
113113
name="Defiance",
114114
indexes=["Action", "PvPAction", "CraftAction"],
115115
columns=["ID", "Name", "Icon", "Description", "ClassJobCategory.Name", "ClassJobLevel", "ActionCategory.Name"],
116-
filters=filters
116+
filters=filters,
117+
string_algo="match"
117118
)
118119

119120
# Search ingame data for matches against a given query. Includes item, minion, mount & achievement descriptions, quest dialog & more.
@@ -141,7 +142,7 @@ async def fetch_example_results(session):
141142
# Get all categories of posts from the Lodestone (cached evert 15 minutes)
142143
lodestone = await client.lodestone_all()
143144

144-
await session.close()
145+
await client.session.close()
145146

146147

147148
if __name__ == '__main__':

pyxivapi/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
__author__ = 'Lethys'
33
__license__ = 'MIT'
44
__copyright__ = 'Copyright 2019 (c) Lethys'
5-
__version__ = '0.1.2'
5+
__version__ = '0.2.0'
66

77
from .client import XIVAPIClient
88
from .exceptions import *

pyxivapi/client.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
from aiohttp import ClientSession
55

6-
from .exceptions import XIVAPIBadRequest, XIVAPIForbidden, XIVAPINotFound, XIVAPIServiceUnavailable, XIVAPIInvalidLanguage, XIVAPIError, XIVAPIInvalidIndex, XIVAPIInvalidColumns
6+
from .exceptions import XIVAPIBadRequest, XIVAPIForbidden, XIVAPINotFound, XIVAPIServiceUnavailable, \
7+
XIVAPIInvalidLanguage, XIVAPIError, XIVAPIInvalidIndex, XIVAPIInvalidColumns, XIVAPIInvalidAlgo
78
from .decorators import timed
89
from .models import Filter, Sort
910

@@ -27,12 +28,21 @@ def __init__(self, api_key: str, session: Optional[ClientSession] = None) -> Non
2728
self.api_key = api_key
2829
self._session = session
2930

31+
self.base_url = "https://xivapi.com"
32+
self.languages = ["en", "fr", "de", "ja"]
33+
self.string_algos = [
34+
"custom", "wildcard", "wildcard_plus", "fuzzy", "term", "prefix", "match", "match_phrase",
35+
"match_phrase_prefix", "multi_match", "query_string"
36+
]
37+
38+
3039
@property
3140
def session(self) -> ClientSession:
3241
if self._session is None or self._session.closed:
3342
self._session = ClientSession()
3443
return self._session
3544

45+
3646
@timed
3747
async def character_search(self, world, forename, surname, page=1):
3848
"""|coro|
@@ -248,7 +258,7 @@ async def pvpteam_by_id(self, lodestone_id):
248258

249259

250260
@timed
251-
async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]=(), sort: Sort=None, page=1, language="en"):
261+
async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]=(), sort: Sort=None, page=1, language="en", string_algo="match"):
252262
"""|coro|
253263
Search for data from on specific indexes.
254264
Parameters
@@ -271,6 +281,10 @@ async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]
271281
Optional[language: str]
272282
The two character length language code that indicates the language to return the response in. Defaults to English (en).
273283
Valid values are "en", "fr", "de" & "ja"
284+
Optional[string_algo: str]
285+
The search algorithm to use for string matching (default = "match")
286+
Valid values are "custom", "wildcard", "wildcard_plus", "fuzzy", "term", "prefix", "match", "match_phrase",
287+
"match_phrase_prefix", "multi_match", "query_string"
274288
"""
275289

276290
if len(indexes) == 0:
@@ -282,14 +296,17 @@ async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]
282296
if len(columns) == 0:
283297
raise XIVAPIInvalidColumns("Please specify at least one column to return in the resulting data.")
284298

299+
if string_algo not in self.string_algos:
300+
raise XIVAPIInvalidAlgo(f'"{string_algo}" is not a supported string_algo for XIVAPI')
301+
285302
body = {
286303
"indexes": ",".join(list(set(indexes))),
287304
"columns": "ID",
288-
"body" : {
305+
"body": {
289306
"query": {
290307
"bool": {
291308
"should": [{
292-
"match": {
309+
string_algo: {
293310
"NameCombined_en": {
294311
"query": name,
295312
"fuzziness": "AUTO",
@@ -298,7 +315,7 @@ async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]
298315
}
299316
}
300317
}, {
301-
"match": {
318+
string_algo: {
302319
"NameCombined_de": {
303320
"query": name,
304321
"fuzziness": "AUTO",
@@ -307,7 +324,7 @@ async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]
307324
}
308325
}
309326
}, {
310-
"match": {
327+
string_algo: {
311328
"NameCombined_fr": {
312329
"query": name,
313330
"fuzziness": "AUTO",
@@ -316,7 +333,7 @@ async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]
316333
}
317334
}
318335
}, {
319-
"match": {
336+
string_algo: {
320337
"NameCombined_ja": {
321338
"query": name,
322339
"fuzziness": "AUTO",
@@ -391,7 +408,6 @@ async def index_by_id(self, index, content_id: int, columns=(), language="en"):
391408
async with self.session.get(url, params=params) as response:
392409
return await self.process_response(response)
393410

394-
395411
@timed
396412
async def lore_search(self, query, language="en"):
397413
"""|coro|

pyxivapi/exceptions.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,11 @@ class XIVAPIError(Exception):
7272
"""
7373
XIVAPI error
7474
"""
75-
pass
75+
pass
76+
77+
78+
class XIVAPIInvalidAlgo(Exception):
79+
"""
80+
Invalid String Algo
81+
"""
82+
pass

0 commit comments

Comments
 (0)