33
44from 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
78from .decorators import timed
89from .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|
0 commit comments