Coverage for api/utils/image_proxy/photon.py: 89%

17 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 06:14 +0000

1from django.conf import settings 

2 

3 

4def get_photon_request_params( 

5 parsed_image_url, 

6 is_full_size: bool, 

7 is_compressed: bool, 

8 headers: dict, 

9): 

10 """ 

11 Photon options documented here: 

12 https://developer.wordpress.com/docs/photon/api/ 

13 """ 

14 params = {} 

15 

16 if not is_full_size: 

17 params["w"] = settings.THUMBNAIL_WIDTH_PX 

18 

19 if is_compressed: 

20 params["quality"] = settings.THUMBNAIL_QUALITY 

21 

22 if parsed_image_url.query: 

23 # No need to URL encode this string because requests will already 

24 # pass the `params` object to `urlencode` before it appends it to the 

25 # request URL. 

26 params["q"] = parsed_image_url.query 

27 

28 if parsed_image_url.scheme == "https": 28 ↛ 36line 28 didn't jump to line 36 because the condition on line 28 was always true

29 # Photon defaults to HTTP without this parameter 

30 # which will cause some providers to fail (if they 

31 # do not serve over HTTP and do not have a redirect) 

32 params["ssl"] = "true" 

33 

34 # Photon excludes the protocol, so we need to reconstruct the url + port + path 

35 # to send as the "path" of the Photon request 

36 domain = parsed_image_url.netloc 

37 path = parsed_image_url.path 

38 upstream_url = f"{settings.PHOTON_ENDPOINT}{domain}{path}" 

39 

40 if settings.PHOTON_AUTH_KEY: 40 ↛ 41line 40 didn't jump to line 41 because the condition on line 40 was never true

41 headers["X-Photon-Authentication"] = settings.PHOTON_AUTH_KEY 

42 

43 return upstream_url, params, headers