Skip to content

Commit

Permalink
update saving yahoo access token as json to replace saving fields to …
Browse files Browse the repository at this point in the history
…separate env vars
  • Loading branch information
uberfastman committed Sep 23, 2024
1 parent e0dba53 commit b0af0f2
Show file tree
Hide file tree
Showing 10 changed files with 388 additions and 334 deletions.
2 changes: 1 addition & 1 deletion DEPLOYMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
make test_docs
```

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

```shell
make docs
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,26 @@ query = YahooFantasySportsQuery(
##### Persistent Authentication Using Access Token JSON

* 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.
* 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:
```python
from pathlib import Path
from yfpy.query import YahooFantasySportsQuery
query = YahooFantasySportsQuery(
league_id="<YAHOO_LEAGUE_ID>",
game_code="nfl",
game_id=449,
yahoo_consumer_key="<YAHOO_CONSUMER_KEY>",
yahoo_consumer_secret="<YAHOO_CONSUMER_SECRET>",
env_file_location=Path(".env")
)
query.save_access_token_data_to_env_file(
env_file_location=Path(".env"),
save_json_to_var_only=True
)
```
<a name="querying-the-yahoo-fantasy-sports-api"></a>
#### Querying the Yahoo Fantasy Sports API
Expand Down
1 change: 0 additions & 1 deletion docs/CNAME

This file was deleted.

2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,5 @@ <h1 id="welcome-to-yfpy">Welcome to YFPY!<a class="headerlink" href="#welcome-to

<!--
MkDocs version : 1.6.1
Build Date UTC : 2024-09-23 00:34:40.848360+00:00
Build Date UTC : 2024-09-23 01:53:35.014597+00:00
-->
623 changes: 320 additions & 303 deletions docs/query/index.html

Large diffs are not rendered by default.

11 changes: 4 additions & 7 deletions docs/quickstart/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -378,18 +378,15 @@
<span class="n">query</span> <span class="o">=</span> <span class="n">YahooFantasySportsQuery</span><span class="p">(</span>
<span class="n">test_league_id</span><span class="p">,</span>
<span class="n">test_game_code</span><span class="p">,</span>
<span class="n">test_game_id</span><span class="p">,</span>
<span class="n">game_id</span><span class="o">=</span><span class="n">test_game_id</span><span class="p">,</span>
<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>
<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>
<span class="c1"># yahoo_access_token_json=os.environ.get(&quot;YAHOO_ACCESS_TOKEN_JSON&quot;),</span>
<span class="n">env_var_fallback</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<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>
<span class="n">save_token_data_to_env_file</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<span class="n">all_output_as_json_str</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
<span class="n">offline</span><span class="o">=</span><span class="kc">False</span>
<span class="n">env_file_location</span><span class="o">=</span><span class="n">project_dir</span><span class="p">,</span>
<span class="n">save_token_data_to_env_file</span><span class="o">=</span><span class="kc">True</span>
<span class="p">)</span>

<span class="c1"># query.save_access_token_data_to_env_file(project_dir, save_json_to_var=True)</span>
<span class="c1"># query.save_access_token_data_to_env_file(project_dir, save_json_to_var_only=True)</span>

<span class="c1"># Manually override league key for example code to work</span>
<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>
Expand Down
20 changes: 20 additions & 0 deletions docs/readme/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,27 @@ <h5 id="persistent-authentication-using-access-token-fields">Persistent Authenti
<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>
<ul>
<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>
<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>
</ul>
<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>

<span class="kn">from</span> <span class="nn">yfpy.query</span> <span class="kn">import</span> <span class="n">YahooFantasySportsQuery</span>

<span class="n">query</span> <span class="o">=</span> <span class="n">YahooFantasySportsQuery</span><span class="p">(</span>
<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>
<span class="n">game_code</span><span class="o">=</span><span class="s2">&quot;nfl&quot;</span><span class="p">,</span>
<span class="n">game_id</span><span class="o">=</span><span class="mi">449</span><span class="p">,</span>
<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>
<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>
<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>
<span class="p">)</span>

<span class="n">query</span><span class="o">.</span><span class="n">save_access_token_data_to_env_file</span><span class="p">(</span>
<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>
<span class="n">save_json_to_var_only</span><span class="o">=</span><span class="kc">True</span>
<span class="p">)</span>
</code></pre></div>

<p><a name="querying-the-yahoo-fantasy-sports-api"></a></p>
<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>
<ul>
Expand Down
2 changes: 1 addition & 1 deletion docs/search/search_index.json

Large diffs are not rendered by default.

11 changes: 4 additions & 7 deletions quickstart/quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,18 +271,15 @@ def get_league_player_limit():
query = YahooFantasySportsQuery(
test_league_id,
test_game_code,
test_game_id,
game_id=test_game_id,
yahoo_consumer_key=os.environ.get("YAHOO_CONSUMER_KEY"),
yahoo_consumer_secret=os.environ.get("YAHOO_CONSUMER_SECRET"),
# yahoo_access_token_json=os.environ.get("YAHOO_ACCESS_TOKEN_JSON"),
env_var_fallback=True,
env_file_location=Path(__file__).parent.parent,
save_token_data_to_env_file=True,
all_output_as_json_str=False,
offline=False
env_file_location=project_dir,
save_token_data_to_env_file=True
)

# query.save_access_token_data_to_env_file(project_dir, save_json_to_var=True)
# query.save_access_token_data_to_env_file(project_dir, save_json_to_var_only=True)

# Manually override league key for example code to work
query.league_key = f"{test_game_id}.l.{test_league_id}"
Expand Down
30 changes: 17 additions & 13 deletions yfpy/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,38 +371,42 @@ def _retrieve_env_file_contents(env_file_path: Path) -> Dict[str, str]:

return env_file_content

def save_access_token_data_to_env_file(self, env_file_directory: Path, env_file_name: str = ".env",
save_json_to_var: bool = False) -> None:
def save_access_token_data_to_env_file(self, env_file_location: Path, env_file_name: str = ".env",
save_json_to_var_only: bool = False) -> None:
"""Saves the fields and values of a Yahoo access token into a .env file.
Args:
env_file_directory (Path): The path to the directory where the target .env file is/will be located.
env_file_location (:obj:`Path`, optional): Path to directory where existing .env file is located or new .env
file should be generated.
env_file_name (:obj:`str`, optional): The name of the target .env file (defaults to ".env").
save_json_to_var (:obj:`bool`, optional): Boolean to determine whether or not to write a JSON string of
save_json_to_var_only (:obj:`bool`, optional): Boolean to determine whether or not to write a JSON string of
Yahoo access token fields to a YAHOO_ACCESS_TOKEN_JSON environment variable in the target .env file
instead of writing Yahoo access token fields to separate environment variables in the target .env file.
(defaults to False).
Returns:
None
"""
if env_file_directory:
env_file_path = env_file_directory / env_file_name
if env_file_location:
env_file_path = env_file_location / env_file_name
else:
logger.warning("Missing argument env_file_location. Yahoo access token will NOT be saved to .env file.")
# exit method without saving Yahoo access token data when no env_file_location argument is provided
return

env_file_content = self._retrieve_env_file_contents(env_file_path)

# replace values of any matching environment variables in .env file with values from Yahoo access token fields
for k, v in self._yahoo_access_token_dict.items():
env_file_content[f"yahoo_{k}"] = v

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

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

0 comments on commit b0af0f2

Please sign in to comment.