Skip to content
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

Create recipe fix #381

Merged
merged 4 commits into from
Dec 30, 2024
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
22 changes: 15 additions & 7 deletions poetry.lock

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

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ aiohttp = ">=3.0.0"
yarl = ">=1.6.0"
mashumaro = "^3.15"
orjson = ">=3.9.0"
awesomeversion = "^24.6.0"

[tool.poetry.group.dev.dependencies]
codespell = "2.3.0"
Expand Down
17 changes: 16 additions & 1 deletion src/aiomealie/mealie.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import asyncio
import json
from awesomeversion import AwesomeVersion
from dataclasses import dataclass
from importlib import metadata
from typing import TYPE_CHECKING, Any, Self
Expand Down Expand Up @@ -57,6 +58,7 @@ class MealieClient:
request_timeout: int = 10
_close_session: bool = False
household_support: bool | None = None
_version: str | None = None

async def _request(
self,
Expand Down Expand Up @@ -173,6 +175,14 @@ async def define_household_support(self) -> bool:
self.household_support = True
return self.household_support

@property
async def version(self) -> str:
"""Return the version, retrieve from get_about if not stored."""
if not self._version:
about = await self.get_about()
self._version = about.version
return self._version

def _versioned_path(self, path_end: str) -> str:
"""Return the path with a prefix based on household support detected."""
if self.household_support:
Expand Down Expand Up @@ -217,7 +227,12 @@ async def get_recipe(self, recipe_id_or_slug: str) -> Recipe:
async def import_recipe(self, url: str, include_tags: bool = False) -> Recipe:
"""Import a recipe."""
data = {"url": url, "include_tags": include_tags}
response = await self._post("api/recipes/create-url", data)
version = AwesomeVersion(self._version)
if version.valid and version < AwesomeVersion("2.0.0"):
mealie_uri = "api/recipes/create-url"
else:
mealie_uri = "api/recipes/create/url"
response = await self._post(mealie_uri, data)
return await self.get_recipe(json.loads(response))

async def get_mealplan_today(self) -> list[Mealplan]:
Expand Down
2 changes: 1 addition & 1 deletion tests/__snapshots__/test_mealie.ambr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# serializer version: 1
# name: test_about
dict({
'version': 'v1.10.2',
'version': 'v2.4.1',
})
# ---
# name: test_groups_self
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/about.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "v1.10.2"
"version": "v2.4.1"
}
6 changes: 3 additions & 3 deletions tests/test_mealie.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ async def test_bad_request_error(
) -> None:
"""Test not found error from mealie."""
responses.post(
f"{MEALIE_URL}/api/recipes/create-url",
f"{MEALIE_URL}/api/recipes/create/url",
status=400,
body=load_fixture("bad_request_error.json"),
)
Expand Down Expand Up @@ -271,7 +271,7 @@ async def test_importing_recipe(
) -> None:
"""Test importing recipe."""
responses.post(
f"{MEALIE_URL}/api/recipes/create-url",
f"{MEALIE_URL}/api/recipes/create/url",
status=201,
body=load_fixture("scrape_recipe.json"),
)
Expand All @@ -287,7 +287,7 @@ async def test_importing_recipe(
== snapshot
)
responses.assert_called_with(
f"{MEALIE_URL}/api/recipes/create-url",
f"{MEALIE_URL}/api/recipes/create/url",
METH_POST,
headers=HEADERS,
params=None,
Expand Down
Loading