Skip to content

Commit 021f232

Browse files
committed
fix: Pick unexpected values from response
Example ======= From Twitter oEmbed, embed content for User account has type=rich but 'url' is included.
1 parent 39df459 commit 021f232

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

oembedpy/consumer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,4 @@ def fetch_content(url: str, params: RequestParameters) -> types.Content:
6060
Type = data.get("type", "").title()
6161
if not (Type and hasattr(types, Type)):
6262
raise ValueError("Invalid type")
63-
return getattr(types, data["type"].title())(**data)
63+
return getattr(types, data["type"].title()).from_dict(data)

oembedpy/ext/sphinx.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
logging.error(msg)
1515
raise err
1616

17-
from oembedpy import consumer
17+
from oembedpy import consumer, discovery
1818

1919

2020
class oembed(nodes.General, nodes.Element): # noqa: D101,E501
@@ -30,8 +30,7 @@ class OembedDirective(Directive): # noqa: D101
3030
}
3131

3232
def run(self): # noqa: D102
33-
url = consumer.discover(self.arguments[0])
34-
url, params = consumer.parse(url)
33+
url, params = discovery.find_from_content(self.arguments[0])
3534
if "maxwidth" in self.options:
3635
params.max_width = self.options["maxwidth"]
3736
if "maxheight" in self.options:

oembedpy/types.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
Please see https://oembed.com/
55
"""
66
from dataclasses import dataclass
7-
from typing import Optional, Union
7+
from inspect import signature
8+
from typing import Any, Dict, Optional, Type, TypeVar, Union
9+
10+
T = TypeVar("T", bound="_Required")
811

912

1013
@dataclass
@@ -13,6 +16,18 @@ class _Required:
1316

1417
type: str
1518
version: str
19+
_extra: Dict[str, Any]
20+
21+
@classmethod
22+
def from_dict(cls: Type[T], data: Dict[str, Any]) -> T:
23+
cls_fields = {field for field in signature(cls).parameters}
24+
cls_kwargs, extra = {}, {}
25+
for k, v in data.items():
26+
if k in cls_fields:
27+
cls_kwargs[k] = v
28+
else:
29+
extra[k] = v
30+
return cls(**cls_kwargs, _extra=extra)
1631

1732

1833
@dataclass

0 commit comments

Comments
 (0)