Skip to content

Commit 011b4cc

Browse files
authored
Merge pull request #5 from curtiscook/c/session
Make sessions optional
2 parents 946f922 + 83b5fb4 commit 011b4cc

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
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: 14 additions & 8 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, XIVAPIInvalidLanguage, XIVAPIError, XIVAPIInvalidIndex, XIVAPIInvalidColumns
57
from .decorators import timed
@@ -13,19 +15,23 @@ class XIVAPIClient:
1315
Asynchronous client for accessing XIVAPI's endpoints.
1416
Parameters
1517
------------
16-
session: aiohttp.ClientSession()
17-
The aiohttp session used with which to make http requests
1818
api_key: str
1919
The API key used for identifying your application with XIVAPI.com.
20+
session: Optional[ClientSession]
21+
Optionally include your aiohttp session
2022
"""
23+
base_url = "https://xivapi.com"
24+
languages = ["en", "fr", "de", "ja"]
2125

22-
def __init__(self, session, api_key):
23-
self.session = session
26+
def __init__(self, api_key: str, session: Optional[ClientSession] = None) -> None:
2427
self.api_key = api_key
28+
self._session = session
2529

26-
self.base_url = "https://xivapi.com"
27-
self.languages = ["en", "fr", "de", "ja"]
28-
30+
@property
31+
def session(self) -> ClientSession:
32+
if self._session is None or self._session.closed:
33+
self._session = ClientSession()
34+
return self._session
2935

3036
@timed
3137
async def character_search(self, world, forename, surname, page=1):

0 commit comments

Comments
 (0)