Skip to content

Commit

Permalink
no response from register() needed
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-watttime committed Dec 22, 2023
1 parent 7b9d973 commit 7f5b3bb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
4 changes: 3 additions & 1 deletion tests/test_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def __init__(self, json_data, status_code):

def json(self):
return self.json_data

def raise_for_status(self):
assert self.status_code == 200

if (
(url == "https://api.watttime.org/register")
Expand Down Expand Up @@ -120,7 +123,6 @@ def test_parse_dates_with_datetime(self):
@mock.patch("requests.post", side_effect=mocked_register)
def test_mock_register(self, mock_post):
resp = self.base.register(email=os.getenv("WATTTIME_EMAIL"))
self.assertEqual(resp.status_code, 200)
self.assertEqual(len(mock_post.call_args_list), 1)


Expand Down
32 changes: 19 additions & 13 deletions watttime/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,23 @@ def _get_chunks(
# API response is inclusive, avoid overlap in chunks
chunks = [(s, e - timedelta(minutes=5)) for s, e in chunks[0:-1]] + [chunks[-1]]
return chunks

def register(self, email: str, organization: Optional[str] = None) -> requests.Response:
"""Register for the WattTime API, if you do not already have an account."""

def register(
self, email: str, organization: Optional[str] = None
) -> requests.Response:
url = f"{self.url_base}/register"
params = {
'username': self.username,
'password': self.password,
'email': email,
'org': organization,
"username": self.username,
"password": self.password,
"email": email,
"org": organization,
}

rsp = requests.post(url, json=params, timeout=20)
return rsp
rsp.raise_for_status()
print(
f"Successfully registered {self.username}, please check {email} for a verification email"
)


class WattTimeHistorical(WattTimeBase):
Expand Down Expand Up @@ -196,10 +200,10 @@ def get_historical_csv(
None
"""
df = self.get_historical_pandas(start, end, region, signal_type, model_date)

out_dir = Path.home() / "watttime_historical_csvs"
out_dir.mkdir(exist_ok=True)

start, end = self._parse_dates(start, end)
fp = out_dir / f"{region}_{signal_type}_{start.date()}_{end.date()}.csv"
df.to_csv(fp, index=False)
Expand Down Expand Up @@ -317,7 +321,9 @@ def get_forecast_pandas(
pd.DataFrame: _description_
"""
j = self.get_forecast_json(region, signal_type, model_date)
return pd.json_normalize(j, record_path="data", meta=["meta"] if include_meta else [])
return pd.json_normalize(
j, record_path="data", meta=["meta"] if include_meta else []
)

def get_historical_forecast_json(
self,
Expand Down Expand Up @@ -373,7 +379,7 @@ def get_historical_forecast_pandas(
)
out = pd.DataFrame()
for json in json_list:
for entry in json['data']:
for entry in json["data"]:
_df = pd.json_normalize(entry, record_path=["forecast"])
_df = _df.assign(generated_at=pd.to_datetime(entry["generated_at"]))
out = pd.concat([out, _df])
Expand Down

0 comments on commit 7f5b3bb

Please sign in to comment.