Skip to content

Commit

Permalink
💫 [IMPR] Allow to configure headers in parsers requests (refs #3861)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chatewgne committed Dec 19, 2023
1 parent e2f05be commit fdbbaa2
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ CHANGELOG

- Add rules fixture on sensitive area (#3470)
- Change condition on signage & blade to select many of them (#3847)
- Set User-Agent header in requests from Parsers (#3861)
- Allow to set headers in requests from Parsers (#3861)

**Documentation**

Expand Down
6 changes: 3 additions & 3 deletions geotrek/common/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class Parser:
"""
provider: Allow to differentiate multiple Parser for the same model
default_language: Allow to define which language this parser will populate by default
headers: Allow to configure headers on parser requests
"""
label = None
model = None
Expand All @@ -97,7 +98,7 @@ class Parser:
natural_keys = {}
field_options = {}
default_language = None
user_agent = "Geotrek"
headers = {"User-Agent": "Geotrek-Admin"}

def __init__(self, progress_cb=None, user=None, encoding='utf8'):
self.warnings = {}
Expand Down Expand Up @@ -559,8 +560,7 @@ def request_or_retry(self, url, verb='get', **kwargs):
assert try_get > 0
while try_get:
action = getattr(requests, verb)
headers = {"User-Agent": self.user_agent}
response = action(url, headers=headers, allow_redirects=True, **kwargs)
response = action(url, headers=self.headers, allow_redirects=True, **kwargs)
if response.status_code in settings.PARSER_RETRY_HTTP_STATUS:
logger.info("Failed to fetch url {}. Retrying ...".format(url))
sleep(settings.PARSER_RETRY_SLEEP_TIME)
Expand Down

0 comments on commit fdbbaa2

Please sign in to comment.