Skip to content

Commit b0af0f2

Browse files
committed
update saving yahoo access token as json to replace saving fields to separate env vars
1 parent e0dba53 commit b0af0f2

File tree

10 files changed

+388
-334
lines changed

10 files changed

+388
-334
lines changed

DEPLOYMENT.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
make test_docs
8787
```
8888

89-
15. *(Optional)* Build the MkDocs documentation and PyPI package independent of deployment:
89+
15. *(Optional)* Build the PyPI package and MkDocs documentation independent of deployment:
9090

9191
```shell
9292
make docs

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,26 @@ query = YahooFantasySportsQuery(
204204
##### Persistent Authentication Using Access Token JSON
205205

206206
* YFPY *also* supports the use of a **single** environment variable by providing a valid JSON string in `YAHOO_ACCESS_TOKEN_JSON`. This environment variable is only used if `env_var_fallback=True` (default) when instantiating a YFPY query.
207+
* You can save the Yahoo access token fields as valid JSON with escaped double quotes (`"`) by invoking `YahooFantasySportsQuery.save_access_token_data_to_env_file` with `save_json_to_var_only=True` (instead of saving the Yahoo access token fields to individual environment variables as described in [Persistent Authentication Using Access Token Fields](#persistent-authentication-using-access-token-fields)) like below:
208+
```python
209+
from pathlib import Path
210+
211+
from yfpy.query import YahooFantasySportsQuery
212+
213+
query = YahooFantasySportsQuery(
214+
league_id="<YAHOO_LEAGUE_ID>",
215+
game_code="nfl",
216+
game_id=449,
217+
yahoo_consumer_key="<YAHOO_CONSUMER_KEY>",
218+
yahoo_consumer_secret="<YAHOO_CONSUMER_SECRET>",
219+
env_file_location=Path(".env")
220+
)
221+
222+
query.save_access_token_data_to_env_file(
223+
env_file_location=Path(".env"),
224+
save_json_to_var_only=True
225+
)
226+
```
207227
208228
<a name="querying-the-yahoo-fantasy-sports-api"></a>
209229
#### Querying the Yahoo Fantasy Sports API

docs/CNAME

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,5 @@ <h1 id="welcome-to-yfpy">Welcome to YFPY!<a class="headerlink" href="#welcome-to
162162

163163
<!--
164164
MkDocs version : 1.6.1
165-
Build Date UTC : 2024-09-23 00:34:40.848360+00:00
165+
Build Date UTC : 2024-09-23 01:53:35.014597+00:00
166166
-->

docs/query/index.html

Lines changed: 320 additions & 303 deletions
Large diffs are not rendered by default.

docs/quickstart/index.html

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -378,18 +378,15 @@
378378
<span class="n">query</span> <span class="o">=</span> <span class="n">YahooFantasySportsQuery</span><span class="p">(</span>
379379
<span class="n">test_league_id</span><span class="p">,</span>
380380
<span class="n">test_game_code</span><span class="p">,</span>
381-
<span class="n">test_game_id</span><span class="p">,</span>
381+
<span class="n">game_id</span><span class="o">=</span><span class="n">test_game_id</span><span class="p">,</span>
382382
<span class="n">yahoo_consumer_key</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;YAHOO_CONSUMER_KEY&quot;</span><span class="p">),</span>
383383
<span class="n">yahoo_consumer_secret</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;YAHOO_CONSUMER_SECRET&quot;</span><span class="p">),</span>
384384
<span class="c1"># yahoo_access_token_json=os.environ.get(&quot;YAHOO_ACCESS_TOKEN_JSON&quot;),</span>
385-
<span class="n">env_var_fallback</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
386-
<span class="n">env_file_location</span><span class="o">=</span><span class="n">Path</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span>
387-
<span class="n">save_token_data_to_env_file</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
388-
<span class="n">all_output_as_json_str</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
389-
<span class="n">offline</span><span class="o">=</span><span class="kc">False</span>
385+
<span class="n">env_file_location</span><span class="o">=</span><span class="n">project_dir</span><span class="p">,</span>
386+
<span class="n">save_token_data_to_env_file</span><span class="o">=</span><span class="kc">True</span>
390387
<span class="p">)</span>
391388

392-
<span class="c1"># query.save_access_token_data_to_env_file(project_dir, save_json_to_var=True)</span>
389+
<span class="c1"># query.save_access_token_data_to_env_file(project_dir, save_json_to_var_only=True)</span>
393390

394391
<span class="c1"># Manually override league key for example code to work</span>
395392
<span class="n">query</span><span class="o">.</span><span class="n">league_key</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">test_game_id</span><span class="si">}</span><span class="s2">.l.</span><span class="si">{</span><span class="n">test_league_id</span><span class="si">}</span><span class="s2">&quot;</span>

docs/readme/index.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,27 @@ <h5 id="persistent-authentication-using-access-token-fields">Persistent Authenti
389389
<h5 id="persistent-authentication-using-access-token-json">Persistent Authentication Using Access Token JSON<a class="headerlink" href="#persistent-authentication-using-access-token-json" title="Permanent link">&para;</a></h5>
390390
<ul>
391391
<li>YFPY <em>also</em> supports the use of a <strong>single</strong> environment variable by providing a valid JSON string in <code>YAHOO_ACCESS_TOKEN_JSON</code>. This environment variable is only used if <code>env_var_fallback=True</code> (default) when instantiating a YFPY query.</li>
392+
<li>You can save the Yahoo access token fields as valid JSON with escaped double quotes (<code>"</code>) by invoking <code>YahooFantasySportsQuery.save_access_token_data_to_env_file</code> with <code>save_json_to_var_only=True</code> (instead of saving the Yahoo access token fields to individual environment variables as described in <a href="#persistent-authentication-using-access-token-fields">Persistent Authentication Using Access Token Fields</a>) like below:</li>
392393
</ul>
394+
<div class="codehilite"><pre><span></span><code><span class="kn">from</span> <span class="nn">pathlib</span> <span class="kn">import</span> <span class="n">Path</span>
395+
396+
<span class="kn">from</span> <span class="nn">yfpy.query</span> <span class="kn">import</span> <span class="n">YahooFantasySportsQuery</span>
397+
398+
<span class="n">query</span> <span class="o">=</span> <span class="n">YahooFantasySportsQuery</span><span class="p">(</span>
399+
<span class="n">league_id</span><span class="o">=</span><span class="s2">&quot;&lt;YAHOO_LEAGUE_ID&gt;&quot;</span><span class="p">,</span>
400+
<span class="n">game_code</span><span class="o">=</span><span class="s2">&quot;nfl&quot;</span><span class="p">,</span>
401+
<span class="n">game_id</span><span class="o">=</span><span class="mi">449</span><span class="p">,</span>
402+
<span class="n">yahoo_consumer_key</span><span class="o">=</span><span class="s2">&quot;&lt;YAHOO_CONSUMER_KEY&gt;&quot;</span><span class="p">,</span>
403+
<span class="n">yahoo_consumer_secret</span><span class="o">=</span><span class="s2">&quot;&lt;YAHOO_CONSUMER_SECRET&gt;&quot;</span><span class="p">,</span>
404+
<span class="n">env_file_location</span><span class="o">=</span><span class="n">Path</span><span class="p">(</span><span class="s2">&quot;.env&quot;</span><span class="p">)</span>
405+
<span class="p">)</span>
406+
407+
<span class="n">query</span><span class="o">.</span><span class="n">save_access_token_data_to_env_file</span><span class="p">(</span>
408+
<span class="n">env_file_location</span><span class="o">=</span><span class="n">Path</span><span class="p">(</span><span class="s2">&quot;.env&quot;</span><span class="p">),</span>
409+
<span class="n">save_json_to_var_only</span><span class="o">=</span><span class="kc">True</span>
410+
<span class="p">)</span>
411+
</code></pre></div>
412+
393413
<p><a name="querying-the-yahoo-fantasy-sports-api"></a></p>
394414
<h4 id="querying-the-yahoo-fantasy-sports-api">Querying the Yahoo Fantasy Sports API<a class="headerlink" href="#querying-the-yahoo-fantasy-sports-api" title="Permanent link">&para;</a></h4>
395415
<ul>

docs/search/search_index.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

quickstart/quickstart.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,18 +271,15 @@ def get_league_player_limit():
271271
query = YahooFantasySportsQuery(
272272
test_league_id,
273273
test_game_code,
274-
test_game_id,
274+
game_id=test_game_id,
275275
yahoo_consumer_key=os.environ.get("YAHOO_CONSUMER_KEY"),
276276
yahoo_consumer_secret=os.environ.get("YAHOO_CONSUMER_SECRET"),
277277
# yahoo_access_token_json=os.environ.get("YAHOO_ACCESS_TOKEN_JSON"),
278-
env_var_fallback=True,
279-
env_file_location=Path(__file__).parent.parent,
280-
save_token_data_to_env_file=True,
281-
all_output_as_json_str=False,
282-
offline=False
278+
env_file_location=project_dir,
279+
save_token_data_to_env_file=True
283280
)
284281

285-
# query.save_access_token_data_to_env_file(project_dir, save_json_to_var=True)
282+
# query.save_access_token_data_to_env_file(project_dir, save_json_to_var_only=True)
286283

287284
# Manually override league key for example code to work
288285
query.league_key = f"{test_game_id}.l.{test_league_id}"

yfpy/query.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -371,38 +371,42 @@ def _retrieve_env_file_contents(env_file_path: Path) -> Dict[str, str]:
371371

372372
return env_file_content
373373

374-
def save_access_token_data_to_env_file(self, env_file_directory: Path, env_file_name: str = ".env",
375-
save_json_to_var: bool = False) -> None:
374+
def save_access_token_data_to_env_file(self, env_file_location: Path, env_file_name: str = ".env",
375+
save_json_to_var_only: bool = False) -> None:
376376
"""Saves the fields and values of a Yahoo access token into a .env file.
377377
378378
Args:
379-
env_file_directory (Path): The path to the directory where the target .env file is/will be located.
379+
env_file_location (:obj:`Path`, optional): Path to directory where existing .env file is located or new .env
380+
file should be generated.
380381
env_file_name (:obj:`str`, optional): The name of the target .env file (defaults to ".env").
381-
save_json_to_var (:obj:`bool`, optional): Boolean to determine whether or not to write a JSON string of
382+
save_json_to_var_only (:obj:`bool`, optional): Boolean to determine whether or not to write a JSON string of
382383
Yahoo access token fields to a YAHOO_ACCESS_TOKEN_JSON environment variable in the target .env file
384+
instead of writing Yahoo access token fields to separate environment variables in the target .env file.
383385
(defaults to False).
384386
385387
Returns:
386388
None
387389
388390
"""
389-
if env_file_directory:
390-
env_file_path = env_file_directory / env_file_name
391+
if env_file_location:
392+
env_file_path = env_file_location / env_file_name
391393
else:
392394
logger.warning("Missing argument env_file_location. Yahoo access token will NOT be saved to .env file.")
393395
# exit method without saving Yahoo access token data when no env_file_location argument is provided
394396
return
395397

396398
env_file_content = self._retrieve_env_file_contents(env_file_path)
397399

398-
# replace values of any matching environment variables in .env file with values from Yahoo access token fields
399-
for k, v in self._yahoo_access_token_dict.items():
400-
env_file_content[f"yahoo_{k}"] = v
401-
402-
# generate a JSON string with escaped double quotes using nested json.dumps() and write it to a
403-
# YAHOO_ACCESS_TOKEN_JSON environment variable if save_json_to_var is set to True
404-
if save_json_to_var:
400+
if save_json_to_var_only:
401+
# generate a JSON string with escaped double quotes using nested json.dumps() and write it to a
402+
# YAHOO_ACCESS_TOKEN_JSON environment variable if save_json_to_var_only is set to True instead of writing
403+
# Yahoo access token fields to separate environment variables in target .env file
405404
env_file_content["yahoo_access_token_json"] = json.dumps(json.dumps(self._yahoo_access_token_dict))
405+
else:
406+
# replace values of any matching environment variables in .env file with values from Yahoo access token
407+
# fields or add new environment variables to .env file if any fields are missing
408+
for k, v in self._yahoo_access_token_dict.items():
409+
env_file_content[f"yahoo_{k}"] = v
406410

407411
# write contents to .env file (overwrites contents if file exists or creates a new file if not)
408412
with open(env_file_path, "w") as env_file:

0 commit comments

Comments
 (0)