Skip to content

Commit 83a0d8b

Browse files
committed
Merge branch 'master' into pr/6
2 parents acd94eb + 011b4cc commit 83a0d8b

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,6 @@ venv.bak/
104104
.mypy_cache/
105105
.DS_Store
106106
example.py
107+
108+
# pycharm
109+
.idea

README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ from pyxivapi.models import Filter, Sort
5858

5959

6060
async def fetch_example_results(session):
61-
client = pyxivapi.XIVAPIClient(session=session, api_key="your_key_here")
61+
client = pyxivapi.XIVAPIClient(api_key="your_key_here")
6262

6363
# Search Lodestone for a character
6464
character = await client.character_search(
@@ -146,9 +146,6 @@ async def fetch_example_results(session):
146146

147147
if __name__ == '__main__':
148148
logging.basicConfig(level=logging.INFO, format='%(message)s', datefmt='%H:%M')
149-
150-
loop = asyncio.get_event_loop()
151-
session = aiohttp.ClientSession(loop=loop)
152-
loop.run_until_complete(fetch_example_results(session))
149+
loop.run_until_complete(fetch_example_results())
153150

154151
```

pyxivapi/client.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import logging
2-
from typing import List
2+
from typing import List, Optional
3+
4+
from aiohttp import ClientSession
35

46
from .exceptions import XIVAPIBadRequest, XIVAPIForbidden, XIVAPINotFound, XIVAPIServiceUnavailable, \
57
XIVAPIInvalidLanguage, XIVAPIError, XIVAPIInvalidIndex, XIVAPIInvalidColumns, XIVAPIInvalidAlgo
@@ -14,15 +16,17 @@ class XIVAPIClient:
1416
Asynchronous client for accessing XIVAPI's endpoints.
1517
Parameters
1618
------------
17-
session: aiohttp.ClientSession()
18-
The aiohttp session used with which to make http requests
1919
api_key: str
2020
The API key used for identifying your application with XIVAPI.com.
21+
session: Optional[ClientSession]
22+
Optionally include your aiohttp session
2123
"""
24+
base_url = "https://xivapi.com"
25+
languages = ["en", "fr", "de", "ja"]
2226

23-
def __init__(self, session, api_key):
24-
self.session = session
27+
def __init__(self, api_key: str, session: Optional[ClientSession] = None) -> None:
2528
self.api_key = api_key
29+
self._session = session
2630

2731
self.base_url = "https://xivapi.com"
2832
self.languages = ["en", "fr", "de", "ja"]
@@ -31,6 +35,14 @@ def __init__(self, session, api_key):
3135
"match_phrase_prefix", "multi_match", "query_string"
3236
]
3337

38+
39+
@property
40+
def session(self) -> ClientSession:
41+
if self._session is None or self._session.closed:
42+
self._session = ClientSession()
43+
return self._session
44+
45+
3446
@timed
3547
async def character_search(self, world, forename, surname, page=1):
3648
"""|coro|

0 commit comments

Comments
 (0)