Skip to content

Commit 375e1ef

Browse files
authored
Merge pull request #40 from mattmanley/add_user_endpoints
Added new user methods
2 parents 016f899 + 97db670 commit 375e1ef

7 files changed

Lines changed: 172 additions & 6 deletions

File tree

docs/usage/functionality.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ Module: task
2222
:members:
2323
:show-inheritance:
2424

25+
Module: user
26+
-----------------------------------------------------
27+
28+
.. automodule:: maproulette.api.user
29+
:members:
30+
:show-inheritance:
31+
2532
Module: configuration
2633
-----------------------------------------------------
2734

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import maproulette
2+
import json
3+
4+
# Create a configuration object using a url and API key
5+
config = maproulette.Configuration(api_key='{YOUR_API_KEY}')
6+
7+
# Create an API object using your config object
8+
api = maproulette.User(config)
9+
10+
# Specify the user IDs to grant privileges to
11+
user_ids = [123, 456, 789]
12+
13+
# Specify the project ID to update the privileges for
14+
project_id = '147'
15+
16+
# Specify what level of access you want to grant this user (1 - Admin, 2 - Write, 3 - Read)
17+
group = '2'
18+
19+
# Print the API response
20+
print(json.dumps(api.add_user_list_to_project(user_ids=user_ids,
21+
project_id=project_id,
22+
group_type=group), indent=4, sort_keys=True))

examples/add_user_to_project.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import maproulette
2+
import json
3+
4+
# Create a configuration object using a url and API key
5+
config = maproulette.Configuration(api_key='{YOUR_API_KEY}')
6+
7+
# Create an API object using your config object
8+
api = maproulette.User(config)
9+
10+
# Specify the user ID to grant privileges to
11+
user_id = '{SOME_USER_ID}'
12+
13+
# Specify the project ID to update the privileges for
14+
project_id = '{YOUR_PROJECT_ID}'
15+
16+
# Specify what level of access you want to grant this user (1 - Admin, 2 - Write, 3 - Read)
17+
group = '2'
18+
19+
# Print the API response
20+
print(json.dumps(api.add_user_to_project(user_id=user_id,
21+
project_id=project_id,
22+
group_type=group), indent=4, sort_keys=True))

examples/find_user_by_username.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import maproulette
2+
import json
3+
4+
# Create a configuration object using a url and API key
5+
config = maproulette.Configuration(api_key='{YOUR_API_KEY}')
6+
7+
# Create an API object using your config object
8+
api = maproulette.User(config)
9+
10+
# We want to fetch a user with a particular name
11+
username = '{YOUR_USERNAME}'
12+
13+
# Print the API response
14+
print(json.dumps(api.find_user_by_username(username), indent=4, sort_keys=True))

maproulette/api/maproulette_server.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ def get(self, endpoint, params=None):
5757
"""
5858
response = self.session.get(
5959
self.url + endpoint,
60-
params=params)
60+
params=params
61+
)
6162
try:
6263
response.raise_for_status()
6364
except requests.exceptions.HTTPError as e:
@@ -89,16 +90,19 @@ def get(self, endpoint, params=None):
8990
"status": response.status_code
9091
}
9192

92-
def post(self, endpoint, body=None):
93+
def post(self, endpoint, body=None, params=None):
9394
"""Method that completes a POST request to the MapRoulette API
9495
9596
:param endpoint: the server endpoint to use for the POST request
9697
:param body: the body of the request (optional)
98+
:param params: the parameters that pertain to the request (optional)
9799
:returns: a JSON object containing the API response
98100
"""
99101
response = self.session.post(
100102
self.url + endpoint,
101-
json=body)
103+
params=params,
104+
json=body
105+
)
102106
try:
103107
response.raise_for_status()
104108
except requests.exceptions.HTTPError as e:
@@ -132,16 +136,19 @@ def post(self, endpoint, body=None):
132136
"status": response.status_code
133137
}
134138

135-
def put(self, endpoint, body=None):
139+
def put(self, endpoint, body=None, params=None):
136140
"""Method that completes a PUT request to the MapRoulette API
137141
138142
:param endpoint: the server endpoint to use for the PUT request
139143
:param body: the body of the request (optional)
144+
:param params: the parameters that pertain to the request (optional)
140145
:returns: a JSON object containing the response code and the API response if
141146
"""
142147
response = self.session.put(
143148
self.url + endpoint,
144-
json=body)
149+
params=params,
150+
json=body
151+
)
145152
try:
146153
response.raise_for_status()
147154
except requests.exceptions.HTTPError as e:

maproulette/api/user.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""This module contains the methods that the user will use directly to interact with MapRoulette users"""
22

3-
import json
43
from maproulette.api.maproulette_server import MapRouletteServer
54

65

@@ -9,3 +8,58 @@ class User(MapRouletteServer):
98

109
def __init__(self, config):
1110
super().__init__(configuration=config)
11+
12+
def find_user_by_username(self, username, limit=10, page=0):
13+
"""Method to search for a user based on a specific username
14+
15+
:param username: the username to search for.
16+
:param limit: the limit to the number of results returned in the response. Default is 10
17+
:param page: used in conjunction with the limit parameter to page through X number of responses. Default is 0.
18+
:returns: the API response from the GET request
19+
"""
20+
query_params = {
21+
"limit": str(limit),
22+
"page": str(page)
23+
}
24+
response = self.get(
25+
endpoint=f"/users/find/{username}",
26+
params=query_params
27+
)
28+
return response
29+
30+
def add_user_to_project(self, user_id, project_id, group_type, is_osm_user_id='true'):
31+
"""Method to add a user to a project group
32+
33+
:param user_id: the user ID to add to the specified project group
34+
:param project_id: the ID of the project
35+
:param group_type: the group type to add the user to (1 - Admin, 2 - Write, 3 - Read)
36+
:param is_osm_user_id: whether or not the specified user ID is an OSM user ID. Default is 'false'.
37+
:returns: the API response from the POST request
38+
"""
39+
query_params = {
40+
"isOSMUserId": str(is_osm_user_id)
41+
}
42+
response = self.post(
43+
endpoint=f"/user/{user_id}/project/{project_id}/{group_type}",
44+
params=query_params
45+
)
46+
return response
47+
48+
def add_user_list_to_project(self, user_ids, project_id, group_type, is_osm_user_id='true'):
49+
"""Method to add a user to a project group
50+
51+
:param user_ids: a list of user IDs to add to the specified project group. IDs should be integers.
52+
:param project_id: the ID of the project
53+
:param group_type: the group type to add the user to (1 - Admin, 2 - Write, 3 - Read)
54+
:param is_osm_user_id: whether or not the specified user ID is an OSM user ID. Default is 'false'.
55+
:returns: the API response from the PUT request
56+
"""
57+
query_params = {
58+
"isOSMUserId": str(is_osm_user_id)
59+
}
60+
response = self.put(
61+
endpoint=f"/user/project/{project_id}/{group_type}",
62+
params=query_params,
63+
body=user_ids
64+
)
65+
return response

tests/test_user_api.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import maproulette
2+
import unittest
3+
from unittest.mock import patch
4+
5+
6+
class TestUserAPI(unittest.TestCase):
7+
8+
config = maproulette.Configuration(api_key="API_KEY")
9+
api = maproulette.User(config)
10+
11+
@patch('maproulette.api.maproulette_server.requests.Session.get')
12+
def test_find_user_by_username(self, mock_request, api_instance=api):
13+
test_username = 'my_username_123'
14+
mock_request.return_value.status_code = '200'
15+
response = api_instance.find_user_by_username(test_username)
16+
self.assertEqual(response['status'], '200')
17+
18+
@patch('maproulette.api.maproulette_server.requests.Session.post')
19+
def test_add_user_to_project(self, mock_request, api_instance=api):
20+
test_user_id = '12345'
21+
test_project_id = '6789'
22+
test_group = '2'
23+
mock_request.return_value.status_code = '200'
24+
response = api_instance.add_user_to_project(user_id=test_user_id,
25+
project_id=test_project_id,
26+
group_type=test_group,
27+
is_osm_user_id='true')
28+
self.assertEqual(response['status'], '200')
29+
30+
@patch('maproulette.api.maproulette_server.requests.Session.put')
31+
def test_add_user_list_to_project(self, mock_request, api_instance=api):
32+
test_user_ids = [123, 456, 789]
33+
test_project_id = '6789'
34+
test_group = '2'
35+
mock_request.return_value.status_code = '200'
36+
response = api_instance.add_user_list_to_project(user_ids=test_user_ids,
37+
project_id=test_project_id,
38+
group_type=test_group,
39+
is_osm_user_id='true')
40+
self.assertEqual(response['status'], '200')

0 commit comments

Comments
 (0)