예제 #1
0
 def update_event_total(self, event_identifier, total):
     self.__validate_event_identifier(event_identifier=event_identifier)
     url = self._base_api_url + 'event/{}/total/'.format(event_identifier)
     response = UrlCall(url=url,
                        timeout=self._timeout).post(data={'total': total})
     decoded_content = response.content.decode('utf-8')
     return json.loads(decoded_content)['update_successful']
예제 #2
0
 def get_event_donations(self,
                         event_identifier,
                         time_bounds=(),
                         limit=None):
     self.__validate_event_identifier(event_identifier=event_identifier)
     params_added = False
     # TODO: Rewrite to properly use query parameters built into requests instead of manually concatenating
     url = self._base_api_url + 'event/{}/donations/'.format(
         event_identifier)
     if len(time_bounds) == 2:
         lower_bound, upper_bound = time_bounds[0], time_bounds[1]
         if not isinstance(lower_bound, int) or not isinstance(
                 upper_bound, int):
             raise IllegalArgumentException(
                 'Time bounds must be a tuple of 2 integers')
         url += '?lower={}&upper={}'.format(lower_bound, upper_bound)
         params_added = True
     if limit is not None:
         if limit <= 0:
             raise IllegalArgumentException('Limit must be 1 or more')
         url += '&' if params_added else '?'
         url += 'limit={}'.format(limit)
         params_added = True
     response = UrlCall(url=url, timeout=self._timeout).get()
     decoded_content = response.content.decode('utf-8')
     converted_content = json.loads(decoded_content)['donations']
     if isinstance(converted_content, list):
         donations = [
             Donation.from_json(donation) for donation in converted_content
         ]
     else:
         donations = Donation.from_json(converted_content)
     return donations
예제 #3
0
 def send_heartbeat(self, source, state, timestamp=None):
     if timestamp is None or not isinstance(timestamp, int):
         timestamp = int(time.time())
     data = dict(state=state, source=source, timestamp=timestamp)
     url = self._base_api_url + 'heartbeat/'
     response = UrlCall(url=url, timeout=self._timeout).post(data=data)
     decoded_content = response.content.decode('utf-8')
     return json.loads(decoded_content)['received']
예제 #4
0
 def get_donation_distribution(self, event_identifier):
     self.__validate_event_identifier(event_identifier=event_identifier)
     url = self._base_api_url + 'event/{}/donations/distribution'.format(
         event_identifier)
     response = UrlCall(url=url, timeout=self._timeout).get()
     decoded_content = response.content.decode('utf-8')
     converted_content = json.loads(decoded_content)
     return converted_content['distribution']
예제 #5
0
 def get_average_donation_amount(self, event_identifier):
     self.__validate_event_identifier(event_identifier=event_identifier)
     url = self._base_api_url + 'event/{}/donations/average'.format(
         event_identifier)
     response = UrlCall(url=url, timeout=self._timeout).get()
     decoded_content = response.content.decode('utf-8')
     converted_content = json.loads(decoded_content)
     return round(float(converted_content['average_donation_amount']), 3)
예제 #6
0
 def get_latest_event_donation(self, event_identifier):
     self.__validate_event_identifier(event_identifier=event_identifier)
     url = self._base_api_url + 'event/{}/donations/largest'.format(
         event_identifier)
     response = UrlCall(url=url, timeout=self._timeout).get()
     decoded_content = response.content.decode('utf-8')
     converted_content = json.loads(decoded_content)
     return Donation.from_dict(converted_content)
예제 #7
0
 def get_event_info(self, identifier):
     url = self._base_api_url + 'event/{}'.format(identifier)
     decoded_content = UrlCall(
         url=url, timeout=self._timeout).get().content.decode('utf-8')
     content = json.loads(decoded_content)
     if 'message' in content.keys():
         raise NonExistentEventException(
             'Event with identifier {} does not exist'.format(identifier))
     return content
예제 #8
0
 def get_all_events(self):
     url = self._base_api_url + 'events'
     response = UrlCall(url=url, timeout=self._timeout).get()
     decoded_content = response.content.decode('utf-8')
     converted_content = json.loads(decoded_content)
     return [
         EventConfiguration(configuration_values=config)
         for config in converted_content['events']
     ]
예제 #9
0
 def get_time_bound_donation_count(self, event_identifier, lower_time_bound,
                                   upper_time_bound):
     self.__validate_event_identifier(event_identifier=event_identifier)
     if not isinstance(lower_time_bound, int) or not isinstance(
             upper_time_bound, int):
         raise IllegalArgumentException('Time bounds must be integers')
     if not upper_time_bound > lower_time_bound or (lower_time_bound < 0 or
                                                    upper_time_bound < 0):
         raise IllegalArgumentException(
             'Time bounds must be positive integers with upper larger than lower'
         )
     url = self._base_api_url + 'event/{}/donations/count?lower={}&upper={}'.format(
         event_identifier, lower_time_bound, upper_time_bound)
     response = UrlCall(url=url, timeout=self._timeout).get()
     decoded_content = response.content.decode('utf-8')
     converted_content = json.loads(decoded_content)
     return int(converted_content['count'])
예제 #10
0
 def register_donation(self, donation):
     url = self._base_api_url + 'donation/'
     response = UrlCall(url=url,
                        timeout=self._timeout).post(data=donation.to_dict())
     decoded_content = response.content.decode('utf-8')
     return json.loads(decoded_content)['received']
예제 #11
0
 def update_event(self, event_configuration):
     url = self._base_api_url + 'event/'
     response = UrlCall(url=url, timeout=self._timeout).post(
         data=event_configuration.configuration_values)
     decoded_content = response.content.decode('utf-8')
     return json.loads(decoded_content)['update_successful']
예제 #12
0
 def get_event_existence(self, identifier):
     url = self._base_api_url + 'event/exists/{}/'.format(identifier)
     decoded_content = UrlCall(
         url=url, timeout=self._timeout).get().content.decode('utf-8')
     return json.loads(decoded_content)['event_exists']
예제 #13
0
 def get_index(self):
     return json.loads(
         UrlCall(url=self._base_api_url,
                 timeout=self._timeout).get().content.decode('utf-8'))
예제 #14
0
 def get_event_total(self, event_identifier):
     self.__validate_event_identifier(event_identifier=event_identifier)
     url = self._base_api_url + 'event/{}/total/'.format(event_identifier)
     response = UrlCall(url=url, timeout=self._timeout).get()
     decoded_content = response.content.decode('utf-8')
     return float(json.loads(decoded_content)['total'])
예제 #15
0
 def test_paths_return_200(self, url):
     response = UrlCall(url=url).get()
     assert 200 == response.status_code