Changeset 60:ec70c7dc68b6 in stamper


Ignore:
Timestamp:
Dec 28, 2014, 12:57:07 PM (6 years ago)
Author:
Borja Lopez <borja@…>
Branch:
default
Phase:
public
Message:

Improvements on the code to push stamps to a remote collector server:

  • The collector server returns now a list of exposed api methods/urls after logging in. That means we do not need to set the url to the listener anymore (stamper can learn from the collector's response)
  • The collector handles fine now push actions to add/append stamps to its database, returning the number of stamps that are added, updated or deleted to/from the database. That means we can show a bit more information to the user after the push
  • The responses from the collector are json-encoded, so now we try to de-serialize the data, showing an error if that is not posible (badly-formatted json, error/exception, etc).
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • etc/stamper.conf

    r58 r60  
    1818base_url = http://localhost:8000
    1919login_path = /stamper/login/
    20 listen_path = /stamper/listen/
    2120user = CHANGEME
    2221password = SECRET
  • stamper/stamper.py

    r59 r60  
    454454        stamps = json.dumps(stamps, indent=4)
    455455        http_client = HTTPClient(self.collector['base_url'])
    456         http_client.post(self.collector['login_path'],
    457                          {'username': self.collector['user'],
    458                           'password': self.collector['password']})
    459         http_client.post(self.collector['listen_path'], {'stamps': stamps})
     456        response = http_client.post(self.collector['login_path'],
     457                                    {'username': self.collector['user'],
     458                                    'password': self.collector['password']})
     459        # response is a json-encoded list of end-points from the collector api
     460        try:
     461            api = json.loads(response)
     462        except ValueError:
     463            print('[error] No valid api end points can be retrieved')
     464            return
     465
     466        response = http_client.post(api['push'], {'stamps': stamps})
     467
     468        # response is a json-encoded dict, containing lists of added, updated
     469        # and deleted stamps (on the other side)
     470        try:
     471            results = json.loads(response)
     472        except ValueError:
     473            print('[error] stamps pushed, can not retrieve results information')
     474            return
     475
     476        # display information
     477        for k in results.keys():
     478            print('%(stamps)d stamps %(action)s' % {'stamps': len(results[k]),
     479                                                    'action': k})
Note: See TracChangeset for help on using the changeset viewer.