Skip to content

Commit

Permalink
Set user-agent when making requests to the OSM API (#712)
Browse files Browse the repository at this point in the history
* Set user-agent when making requests to the OSM API

* Add header to test mock

* Fix get display_name

* Fix error in users.tests.test_utils
  • Loading branch information
willemarcel authored Aug 27, 2024
1 parent f69570a commit c1ef15b
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 10 deletions.
1 change: 1 addition & 0 deletions config/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,4 @@
)

OSMCHA_URL = env('OSMCHA_URL', default='https://osmcha.org')
OSM_API_USER_AGENT = {"User-Agent": "OSMCha osmcha-django v4"}
10 changes: 7 additions & 3 deletions osmchadjango/changeset/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ def import_replications(start, end):

def get_last_replication_id():
"""Get the id of the last replication file available on Planet OSM."""
state = requests.get("{}state.yaml".format(settings.OSM_PLANET_BASE_URL)).content
state = requests.get(
"{}state.yaml".format(settings.OSM_PLANET_BASE_URL),
headers=settings.OSM_API_USER_AGENT
).content
state = yaml.load(state, Loader)
return state.get("sequence")

Expand Down Expand Up @@ -132,11 +135,12 @@ def __init__(self, user, changeset_id):
def post_comment(self, message=None):
"""Post comment to changeset."""
response = self.client.request(
'POST',
"POST",
self.url,
headers=settings.OSM_API_USER_AGENT,
data="text={}".format(quote(message)).encode("utf-8"),
client_id=settings.SOCIAL_AUTH_OPENSTREETMAP_OAUTH2_KEY,
client_secret=settings.SOCIAL_AUTH_OPENSTREETMAP_OAUTH2_SECRET
client_secret=settings.SOCIAL_AUTH_OPENSTREETMAP_OAUTH2_SECRET,
)
if response.status_code == 200:
print(
Expand Down
3 changes: 3 additions & 0 deletions osmchadjango/changeset/tests/test_comment_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class MockResponse():
"{}/api/0.6/changeset/{}/comment/".format(
settings.OSM_SERVER_URL, self.harmful_changeset.id
),
headers=settings.OSM_API_USER_AGENT,
data="text={}".format(quote(message)).encode("utf-8"),
client_id=settings.SOCIAL_AUTH_OPENSTREETMAP_OAUTH2_KEY,
client_secret=settings.SOCIAL_AUTH_OPENSTREETMAP_OAUTH2_SECRET,
Expand Down Expand Up @@ -105,6 +106,7 @@ class MockResponse():
"https://www.openstreetmap.org/api/0.6/changeset/{}/comment/".format(
self.good_changeset.id
),
headers=settings.OSM_API_USER_AGENT,
data="text={}".format(quote(message)).encode("utf-8"),
client_id=settings.SOCIAL_AUTH_OPENSTREETMAP_OAUTH2_KEY,
client_secret=settings.SOCIAL_AUTH_OPENSTREETMAP_OAUTH2_SECRET,
Expand Down Expand Up @@ -137,6 +139,7 @@ class MockResponse():
"https://www.openstreetmap.org/api/0.6/changeset/{}/comment/".format(
self.changeset.id
),
headers=settings.OSM_API_USER_AGENT,
data="text={}".format(quote(message)).encode("utf-8"),
client_id=settings.SOCIAL_AUTH_OPENSTREETMAP_OAUTH2_KEY,
client_secret=settings.SOCIAL_AUTH_OPENSTREETMAP_OAUTH2_SECRET,
Expand Down
1 change: 1 addition & 0 deletions osmchadjango/changeset/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class MockRequest():
mock_oauth_client.assert_called_with(
"POST",
"https://www.openstreetmap.org/api/0.6/changeset/123456/comment/",
headers=settings.OSM_API_USER_AGENT,
data="text={}".format(quote("Reviewed in OSMCha and set as GOOD!")).encode(
"utf-8"
),
Expand Down
11 changes: 4 additions & 7 deletions osmchadjango/users/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# -*- coding: utf-8 -*-
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import ParseError

from django.conf import settings

from social_django.models import UserSocialAuth
Expand All @@ -25,9 +22,9 @@ def update_user_name(user):
"""
try:
uid = user.social_auth.get(provider='openstreetmap-oauth2').uid
url = '{}/api/0.6/user/{}/'.format(settings.OSM_SERVER_URL, uid)
data = ET.fromstring(requests.get(url).content)
display_name = data.find('user').get('display_name')
url = f'{settings.OSM_SERVER_URL}/api/0.6/user/{uid}.json'
data = requests.get(url, headers=settings.OSM_API_USER_AGENT).json()
display_name = data['user']['display_name']
if user.name != display_name:
user.name = display_name
user.save(update_fields=['name'])
Expand All @@ -36,5 +33,5 @@ def update_user_name(user):
print(
'User {} does not have a social_auth instance.'.format(user.username)
)
except ParseError:
except requests.exceptions.JSONDecodeError:
print('It was not possible to update user with uid {}.'.format(uid))
1 change: 1 addition & 0 deletions osmchadjango/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def get_access_token(self, code):
token_url=self.token_url,
code=code,
client_secret=settings.SOCIAL_AUTH_OPENSTREETMAP_OAUTH2_SECRET,
headers=settings.OSM_API_USER_AGENT,
)

def get_user_token(self, request, access_token, *args, **kwargs):
Expand Down

0 comments on commit c1ef15b

Please sign in to comment.