Skip to content

Commit

Permalink
feat: add ability to add a bookmark to an episode
Browse files Browse the repository at this point in the history
  • Loading branch information
browniebroke committed May 9, 2023
1 parent dac1530 commit 6dc32ac
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/deezer/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ def _process_json(
object_class = Resource
elif resource_type:
object_class = resource_type
elif item.get("results") is True:
return True
else:
raise DeezerUnknownResource(f"Unable to find resource type for {result!r}")
assert object_class is not None # nosec B101
Expand Down Expand Up @@ -165,7 +167,7 @@ def request(
json_data = response.json()
if not isinstance(json_data, dict):
return json_data
if "error" in json_data:
if "error" in json_data and json_data["error"]:
raise DeezerErrorResponse(json_data)
return self._process_json(
json_data,
Expand Down
14 changes: 14 additions & 0 deletions src/deezer/resources/episode.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,17 @@ def _infer_missing_field(self, item) -> Any:
elif item == "share":
return f"{self.link}?utm_source=deezer&utm_content=episode-{self.id}&utm_medium=web"
return super()._infer_missing_field(item)

def add_bookmark(self, offset: int, **kwargs) -> bool:
"""
Sets a bookmark on the episode.
:param offset: The offset where the bookmark is set, must be between 0 and 100.
:returns: a boolean that tells if the operation was successful
"""
return self.client.request(
"POST",
f"episode/{self.id}/bookmark",
offset=offset,
**kwargs,
)
56 changes: 56 additions & 0 deletions tests/resources/cassettes/TestEpisode.test_add_bookmark.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
interactions:
- request:
body: null
headers:
Accept:
- "*/*"
Accept-Encoding:
- identity
Connection:
- keep-alive
Content-Length:
- "0"
User-Agent:
- python-requests/2.30.0
method: POST
uri: https://api.deezer.com/episode/343457312/bookmark?access_token=dummy&offset=55
response:
body:
string: '{"error":[],"results":true}'
headers:
Access-Control-Allow-Credentials:
- "true"
Access-Control-Allow-Headers:
- X-Requested-With, Content-Type, Authorization, Origin, Accept, Accept-Encoding
Access-Control-Allow-Methods:
- POST, GET, OPTIONS, DELETE, PUT
Access-Control-Expose-Headers:
- Location
Access-Control-Max-Age:
- "86400"
Cache-Control:
- no-store, no-cache, must-revalidate
Connection:
- keep-alive
Content-Length:
- "27"
Content-Type:
- application/json; charset=utf-8
Expires:
- Thu, 19 Nov 1981 08:52:00 GMT
Pragma:
- no-cache
Server:
- Apache
Strict-Transport-Security:
- max-age=31536000; includeSubDomains
X-Content-Type-Options:
- nosniff
X-Host:
- blm-web-136
x-org:
- FR
status:
code: 200
message: OK
version: 1
11 changes: 11 additions & 0 deletions tests/resources/test_episode.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,14 @@ def test_access_non_inferable_field(self, client):
},
)
assert episode.duration == 3254

def test_add_bookmark(self, client_token):
episode = deezer.Episode(
client_token,
json={
"id": 343457312,
"type": "episode",
},
)
result = episode.add_bookmark(55)
assert result is True

0 comments on commit 6dc32ac

Please sign in to comment.