Skip to content

Add enode link URL endpoint #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pv_site_api/fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,8 @@ def make_fake_status() -> PVSiteAPIStatus:
message="The API is up and running",
)
return pv_api_status


def make_fake_enode_link_url() -> str:
"""Make fake Enode link URL"""
return "https://enode.com"
23 changes: 22 additions & 1 deletion pv_site_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from fastapi import Depends, FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.openapi.utils import get_openapi
from fastapi.responses import FileResponse, JSONResponse
from fastapi.responses import FileResponse, JSONResponse, RedirectResponse
from pvlib import irradiance, location, pvsystem
from pvsite_datamodel.read.site import get_all_sites, get_site_by_uuid
from pvsite_datamodel.read.status import get_latest_status
Expand All @@ -28,6 +28,7 @@
)
from .fake import (
fake_site_uuid,
make_fake_enode_link_url,
make_fake_forecast,
make_fake_inverters,
make_fake_pv_generation,
Expand Down Expand Up @@ -358,6 +359,26 @@ def get_pv_estimate_clearsky(site_uuid: str, session: Session = Depends(get_sess
return res


@app.get("/enode/link", response_class=RedirectResponse)
def get_enode_link(redirect_uri: str, session: Session = Depends(get_session)):
"""
### Returns a URL from Enode that starts a user's Enode link flow.
"""
if int(os.environ["FAKE"]):
return make_fake_enode_link_url()

client = session.query(ClientSQL).first()
assert client is not None

with httpx.Client() as httpx_client:
data = {"vendorType": "inverter", "redirectUri": redirect_uri}
res = httpx_client.post(
f"https://enode-api.production.enode.io/users/{client.client_uuid}/link", data=data
).json()

return res["linkUrl"]
Comment on lines +373 to +379
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we can just do this with a fastapi RedirectResponse?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nevermind

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opted for this in the end. On the frontend, we can just use a fetch as usual, we just won't read the response body (Enode Link UI) and instead just history push the page to the Response.url.



@app.get("/inverters")
async def get_inverters(
session: Session = Depends(get_session),
Expand Down
11 changes: 6 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@
from pv_site_api.session import get_session


@pytest.fixture()
def non_mocked_hosts():
return ["testserver"]


@pytest.fixture(scope="session")
def engine():
"""Make database engine"""
Expand Down Expand Up @@ -70,6 +65,12 @@ def clients(db_session):
return clients


@pytest.fixture
def non_mocked_hosts() -> list:
"""Prevent TestClient fixture from being mocked"""
return ["testserver"]


@pytest.fixture()
def sites(db_session, clients):
"""Create some fake sites"""
Expand Down
25 changes: 25 additions & 0 deletions tests/test_enode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
def test_get_enode_link_fake(client, fake):
params = {"redirect_uri": "https://example.org"}
response = client.get("/enode/link", params=params, follow_redirects=False)

assert response.status_code == 307
assert len(response.headers["location"]) > 0


def test_get_enode_link(client, clients, httpx_mock):
test_enode_link_uri = "https://example.com"

httpx_mock.add_response(
url=f"https://enode-api.production.enode.io/users/{clients[0].client_uuid}/link",
json={"linkUrl": test_enode_link_uri},
)

params = {"redirect_uri": "https://example.org"}
response = client.get(
"/enode/link",
params=params,
follow_redirects=False,
)

assert response.status_code == 307
assert response.headers["location"] == test_enode_link_uri