Skip to content

Commit

Permalink
Add a header kwarg in Client to force session headers
Browse files Browse the repository at this point in the history
Fixes #45
  • Loading branch information
jnth authored and browniebroke committed Sep 24, 2019
1 parent 796fbfc commit ad1d79b
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ unreleased
----------

- Add user's favorite albums, artists, tracks and playlists in user resource.
- Add ``headers`` kwarg in Client to force session headers.

1.1.2 (2019-06-02)
------------------
Expand Down
10 changes: 10 additions & 0 deletions deezer/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class Client:
This client provides several method to retrieve the content of most
sort of Deezer objects, based on their json structure.
Headers can be forced by using the ``headers`` kwarg.
For example, use ``Accept-Language`` header to force the output language.
>>> import deezer
>>> client = deezer.Client(headers={'Accept-Language': 'fr'})
"""

use_ssl = True
Expand Down Expand Up @@ -54,6 +60,10 @@ def __init__(self, **kwargs):
if kwargs.get("do_not_compress_reponse"):
self.session.headers.update({"Accept-Encoding": "identity"})

# Headers
if kwargs.get("headers"):
self.session.headers.update(kwargs.get("headers"))

self.options = kwargs
self._authorize_url = None

Expand Down
2 changes: 2 additions & 0 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ def setUp(self):
app_id="foo", app_secret="bar", do_not_compress_reponse=True
)
self.unsec_client = deezer.Client(use_ssl=False, do_not_compress_reponse=True)
self.client_fr = deezer.Client(headers={"Accept-Language": "fr"}) # French
self.client_ja = deezer.Client(headers={"Accept-Language": "ja"}) # Japanese
34 changes: 34 additions & 0 deletions tests/cassettes/TestClient.test_with_language_header_fr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
interactions:
- request:
body: null
headers:
Accept: ['*/*']
Accept-Encoding: ['gzip, deflate']
Accept-Language: [fr]
Connection: [keep-alive]
User-Agent: [python-requests/2.21.0]
method: GET
uri: https://api.deezer.com/genre/52
response:
body:
string: !!binary |
H4sIAAAAAAAAA7WSwYrCMBCGX2XJ2SZjatO1132NgMRkWrOYGJoUsorvblo8KCzeOsMMDMzPxwz/
jVhDuoZviFcOSUd+TsrHi//qR+XlBICtshHJhgSr0zTOK6eUQuwkk0wFSw3iFUeqL06yAf2IkjVc
MuvU8CI7RKfO5zexNj5Wy1qk5lom6jE9hVEyZ6OWDMGgALXluhcae9BoaqH3x7oVLd/VuwITuREV
LFF9Q1WS/obhBe3Q2MmtweYN5FIf6Uc7rHI2QC71EZ1Xefi28PLc/mWnvzB7ZHECuT8Afg6qn18C
AAA=
headers:
Content-Encoding: [gzip]
Content-Length: ['230']
Content-Type: [application/json; charset=utf-8]
Date: ['Tue, 24 Sep 2019 15:16:58 GMT']
P3P: [policyref="/w3c/p3p.xml" CP="IDC DSP COR CURa ADMa OUR IND PHY ONL COM
STA"]
Server: [Apache]
Set-Cookie: ['dzr_uniq_id=dzr_uniq_id_fr2c04e5ef81e520813c95100f38b135792d2aa4;
expires=Sun, 22-Mar-2020 15:16:58 GMT; Max-Age=15552000; path=/; domain=.deezer.com;
secure']
Vary: [Accept-Encoding]
X-Host: [blm-web-27]
status: {code: 200, message: OK}
version: 1
34 changes: 34 additions & 0 deletions tests/cassettes/TestClient.test_with_language_header_ja.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
interactions:
- request:
body: null
headers:
Accept: ['*/*']
Accept-Encoding: ['gzip, deflate']
Accept-Language: [ja]
Connection: [keep-alive]
User-Agent: [python-requests/2.21.0]
method: GET
uri: https://api.deezer.com/genre/52
response:
body:
string: !!binary |
H4sIAAAAAAAAA7WS3WrEIBCF38XrjRpdTZtnEYo/k9SyZiUmYLf03au2sC2UvYsyh5GZ44cyH8g7
NAp2QosOgEakdk6dqAq26sSr2r7lpqoZWpXfq8Z95+iEorfbvtaLXrctplERRXT02AHcYMX2GhSZ
YVlBEcEU8UHPcLe9pKAvlz9m65bUtbaE3a2c8ALbjzEpEnyyigB1IKnumZ2khYlacFzaZ8MHObAz
PxeYzEJ2tK3uiXZl47c4/0IHcH4PR7CZoLnEQ7rx8yHPpjSXeIjOh3x4X3i5yr/s7T3WGWmTgD6/
ANJmOVCFAgAA
headers:
Content-Encoding: [gzip]
Content-Length: ['237']
Content-Type: [application/json; charset=utf-8]
Date: ['Tue, 24 Sep 2019 15:20:10 GMT']
P3P: [policyref="/w3c/p3p.xml" CP="IDC DSP COR CURa ADMa OUR IND PHY ONL COM
STA"]
Server: [Apache]
Set-Cookie: ['dzr_uniq_id=dzr_uniq_id_fr90cf8af99ce975a3531b5dcc86a19a3ba2ecab;
expires=Sun, 22-Mar-2020 15:20:10 GMT; Max-Age=15552000; path=/; domain=.deezer.com;
secure']
Vary: [Accept-Encoding]
X-Host: [blm-web-115]
status: {code: 200, message: OK}
version: 1
16 changes: 16 additions & 0 deletions tests/test_client_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,19 @@ def test_advanced_search_complex_with_relation(self):
self.assertIsInstance(result, list)
self.assertEqual(result[0].title, "The joke")
self.assertIsInstance(result[0], deezer.resources.Track)

def test_with_language_header_fr(self):
"""Test by adding accept language headers"""
genre = self.client_fr.get_genre(52)
self.assertIsInstance(genre, deezer.resources.Genre)
self.assertEqual(genre.name, "Chanson fran\u00e7aise")
self.assertNotEqual(genre.name, "French Chanson")

def test_with_language_header_ja(self):
"""Test by adding accept language headers"""
genre = self.client_ja.get_genre(52)
self.assertIsInstance(genre, deezer.resources.Genre)
self.assertEqual(
genre.name, "\u30d5\u30ec\u30f3\u30c1\u30fb\u30b7\u30e3\u30f3\u30bd\u30f3"
)
self.assertNotEqual(genre.name, "French Chanson")

0 comments on commit ad1d79b

Please sign in to comment.