Skip to content

Commit

Permalink
Bug Fix: email for API calls (#291)
Browse files Browse the repository at this point in the history
* added email for API calls to be set in .env file and removed self.email from resource.py

* changed EMAIL to NREL_API_EMAIL

* added space after comma seperator
  • Loading branch information
elenya-grant authored Mar 28, 2024
1 parent e993c8a commit 3fe5d2b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ solar and storage.
[https://developer.nrel.gov/signup/](https://developer.nrel.gov/signup/)


7. To set up the `NREL_API_KEY` required for resource downloads, you can create an Environment Variable called
`NREL_API_KEY`. Otherwise, you can keep the key in a new file called ".env" in the root directory of this project.
7. To set up the `NREL_API_KEY` and `NREL_API_EMAIL` required for resource downloads, you can create Environment Variables called `NREL_API_KEY` and `NREL_API_EMAIL`. Otherwise, you can keep the key in a new file called ".env" in the root directory of this project.

Create a file ".env" that contains the single line:
```
```
NREL_API_KEY=key
[email protected]
```

8. Verify setup by running tests:
Expand Down
1 change: 0 additions & 1 deletion hopp/simulation/technologies/resource/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def __init__(self, lat, lon, year, **kwargs):
self.name = 'hybrid-systems'
self.affiliation = 'NREL'
self.reason = 'hybrid-analysis'
self.email = '[email protected]'
self.mailing_list = 'true'

# paths
Expand Down
4 changes: 2 additions & 2 deletions hopp/simulation/technologies/resource/solar_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import csv
from PySAM.ResourceTools import SAM_CSV_to_solar_data

from hopp.utilities.keys import get_developer_nrel_gov_key
from hopp.utilities.keys import get_developer_nrel_gov_key, get_developer_nrel_gov_email
from hopp.utilities.log import hybrid_logger as logger
from hopp.simulation.technologies.resource.resource import Resource
from hopp import ROOT_DIR
Expand Down Expand Up @@ -71,7 +71,7 @@ def __init__(
def download_resource(self):
url = '{base}?wkt=POINT({lon}+{lat})&names={year}&leap_day={leap}&interval={interval}&utc={utc}&full_name={name}&email={email}&affiliation={affiliation}&mailing_list={mailing_list}&reason={reason}&api_key={api}&attributes={attr}'.format(
base=BASE_URL, year=self.year, lat=self.latitude, lon=self.longitude, leap=self.leap_year, interval=self.interval,
utc=self.utc, name=self.name, email=self.email,
utc=self.utc, name=self.name, email=get_developer_nrel_gov_email(),
mailing_list=self.mailing_list, affiliation=self.affiliation, reason=self.reason, api=get_developer_nrel_gov_key(),
attr=self.solar_attributes)

Expand Down
4 changes: 2 additions & 2 deletions hopp/simulation/technologies/resource/wind_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Union
from PySAM.ResourceTools import SRW_to_wind_data

from hopp.utilities.keys import get_developer_nrel_gov_key
from hopp.utilities.keys import get_developer_nrel_gov_key, get_developer_nrel_gov_email
from hopp.simulation.technologies.resource.resource import Resource
from hopp import ROOT_DIR

Expand Down Expand Up @@ -121,7 +121,7 @@ def download_resource(self):

if self.source == "WTK":
url = '{base}?year={year}&lat={lat}&lon={lon}&hubheight={hubheight}&api_key={api_key}&email={email}'.format(
base=WTK_BASE_URL, year=self.year, lat=self.latitude, lon=self.longitude, hubheight=height, api_key=get_developer_nrel_gov_key(), email=self.email
base=WTK_BASE_URL, year=self.year, lat=self.latitude, lon=self.longitude, hubheight=height, api_key=get_developer_nrel_gov_key(), email=get_developer_nrel_gov_email()
)
elif self.source == "TAP":
url = '{base}?height={hubheight}m&lat={lat}&lon={lon}&year={year}'.format(
Expand Down
18 changes: 17 additions & 1 deletion hopp/utilities/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
import os

developer_nrel_gov_key = ""
developer_nrel_gov_email = ""


def set_developer_nrel_gov_key(key: str):
global developer_nrel_gov_key
developer_nrel_gov_key = key

def set_developer_nrel_gov_email(email: str):
global developer_nrel_gov_email
developer_nrel_gov_email = email

def get_developer_nrel_gov_key():
global developer_nrel_gov_key
Expand All @@ -20,6 +23,16 @@ def get_developer_nrel_gov_key():
"Section 2 of 'Installing from Package Repositories' in the README.md")
return developer_nrel_gov_key

def get_developer_nrel_gov_email():
global developer_nrel_gov_email
if developer_nrel_gov_email is None:
raise ValueError("Please provide NREL Developer email using `set_developer_nrel_gov_email`"
"(`from hopp.utilities.keys import set_developer_nrel_gov_email`) \n"
" Ensure your Developer email is set either as a `EMAIL` Environment Variable or"
" using the .env file method. For details on setting up .env, "
"please see Section 7 of 'Installing from Source' or "
"Section 2 of 'Installing from Package Repositories' in the README.md")
return developer_nrel_gov_email

def set_nrel_key_dot_env(path=None):
if path and os.path.exists(path):
Expand All @@ -28,5 +41,8 @@ def set_nrel_key_dot_env(path=None):
r = find_dotenv(usecwd=True)
load_dotenv(r)
NREL_API_KEY = os.getenv("NREL_API_KEY")
NREL_API_EMAIL = os.getenv("NREL_API_EMAIL")
if NREL_API_KEY is not None:
set_developer_nrel_gov_key(NREL_API_KEY)
if NREL_API_EMAIL is not None:
set_developer_nrel_gov_email(NREL_API_EMAIL)

0 comments on commit 3fe5d2b

Please sign in to comment.