Skip to content
This repository was archived by the owner on Jan 26, 2023. It is now read-only.

Commit ca1761b

Browse files
mark private methods with "_", add _reset_all_params()
1 parent 4194ad2 commit ca1761b

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed

gitcoin/client.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,21 @@ def __init__(self):
3030
"""Init params container for 'bounties' filters etc."""
3131
super().__init__()
3232
self.params = {
33-
'raw_data': (True, str),
3433
'experience_level': (True, str),
3534
'project_length': (True, str),
3635
'bounty_type': (True, str),
3736
'bounty_owner_address': (True, str),
37+
'bounty_owner_github_username': (True, str),
3838
'idx_status': (True, str),
3939
'network': (True, str),
40-
'bounty_owner_github_username': (True, str),
41-
'standard_bounties_id': (True, str),
40+
'standard_bounties_id': (True, int),
4241
'pk__gt': (False, int),
4342
'started': (False, str),
4443
'is_open': (False, bool),
4544
'github_url': (True, str),
4645
'fulfiller_github_username': (False, str),
4746
'interested_github_username': (False, str),
47+
'raw_data': (True, str),
4848
'order_by': (False, str),
4949
'limit': (False, int),
5050
'offset': (False, int)
@@ -60,27 +60,31 @@ def __init__(self, url, config):
6060
self.config = config
6161
self.params = {}
6262

63-
def add_param(self, name, value):
63+
def _add_param(self, name, value):
6464
"""Add query parameter with safeguards."""
6565
if self.config.has(name):
6666
is_multiple, normalize = self.config.get(name)
6767
if not is_multiple:
68-
self.del_param(name) # Throw away all previous values, if any.
68+
self._del_param(name) # Throw away all previous values, if any.
6969
if callable(normalize):
7070
value = normalize(value)
71-
self.add_param_unchecked(name, value)
71+
self._add_param_unchecked(name, value)
7272
return self
7373
else:
7474
msg = 'Tried to filter by unknown param "{name}".'
7575
raise KeyError(msg.format(name=name))
7676

77-
def del_param(self, name):
77+
def _del_param(self, name):
7878
"""Delete query parameter."""
7979
if name in self.params:
8080
del self.params[name]
8181
return self
8282

83-
def add_param_unchecked(self, name, value):
83+
def _reset_all_params(self):
84+
"""Delete all query parameters."""
85+
self.params = {}
86+
87+
def _add_param_unchecked(self, name, value):
8488
"""Add query parameter without safeguards.
8589
8690
This is available in case this API client is out-of-sync with the API.
@@ -93,28 +97,28 @@ def add_param_unchecked(self, name, value):
9397
def filter(self, **kwargs):
9498
"""Filter the result set."""
9599
for name, value in kwargs.items():
96-
self.add_param(name, value)
100+
self._add_param(name, value)
97101
return self
98102

99103
def order_by(self, sort):
100104
"""Sort the result set."""
101-
self.add_param('order_by', sort)
105+
self._add_param('order_by', sort)
102106
return self
103107

104108
def get_page(self, number=1, per_page=25):
105-
"""Get a page of the result set."""
106-
self.add_param('limit', per_page)
107-
self.add_param('offset', (number - 1) * per_page)
109+
"""Get one page of the resources list."""
110+
self._add_param('limit', per_page)
111+
self._add_param('offset', (number - 1) * per_page)
108112
return self._request_get()
109113

110114
def all(self):
111-
"""Get the complete result set."""
112-
self.del_param('limit')
113-
self.del_param('offset')
115+
"""List all resources."""
116+
self._del_param('limit')
117+
self._del_param('offset')
114118
return self._request_get()
115119

116120
def get(self, primary_key):
117-
"""Get 1 object by primary key."""
121+
"""Retrieve one resource by primary key."""
118122
return self._request_get('/'.join((self.url, str(primary_key))))
119123

120124
def _request_get(self, url=None):

tests/test_dry_run.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,34 @@ def test_del_param(self):
7171
},
7272
'kwargs': {},
7373
}
74-
result = api.bounties.filter(bounty_type='Feature').del_param('bounty_type').filter(bounty_type='Bug').get_page()
74+
result = api.bounties.filter(bounty_type='Feature')._del_param('bounty_type').filter(bounty_type='Bug').get_page()
75+
assert expected == result
76+
77+
def test_reset_all_params(self):
78+
api = Gitcoin()
79+
bounties_api = api.bounties
80+
expected = {
81+
'url': 'https://gitcoin.co/api/v0.1/bounties',
82+
'params': {
83+
'bounty_type': 'Feature',
84+
'offset': '0',
85+
'limit': '25',
86+
},
87+
'kwargs': {},
88+
}
89+
result = bounties_api.filter(bounty_type='Feature').get_page()
90+
assert expected == result
91+
bounties_api._reset_all_params()
92+
expected = {
93+
'url': 'https://gitcoin.co/api/v0.1/bounties',
94+
'params': {
95+
'bounty_type': 'Bug',
96+
'offset': '0',
97+
'limit': '25',
98+
},
99+
'kwargs': {},
100+
}
101+
result = bounties_api.filter(bounty_type='Bug').get_page()
75102
assert expected == result
76103

77104
def test_order_by(self):
@@ -121,7 +148,7 @@ def __init__(self):
121148
def test_raise_for_status(self):
122149
api = Gitcoin()
123150
with pytest.raises(ValueError): # ValueError only in mock setup
124-
result = api.bounties.add_param_unchecked('raise_for_status', True).all()
151+
result = api.bounties._add_param_unchecked('raise_for_status', True).all()
125152

126153
def test_extending_config_does_not_leak(self):
127154
class ExtendedBountyConfig(BountyConfig):

0 commit comments

Comments
 (0)