initialise debian/
[debian/make-magic.git] / mclient.py
1 #! /usr/bin/env python
2
3 '''overly simple command line client to access the magic HTTP API
4
5 TODO: Make useful as something other than an unintuitive debugging tool
6 '''
7
8 import sys
9 import requests
10 import random
11
12 base_url = 'http://localhost:4554/'
13
14 class CLIhandler(object):
15         def cmd_tasks(self):
16                 print requests.get(base_url+'task').content
17         def cmd_task_create(self, json_task_data):
18                 print requests.post(base_url+'task/create', headers={'Content-Type':'application/json'}, data=json_task_data).content
19         def cmd_metadata(self, uuid):
20                 print requests.get(base_url+'task/'+uuid+'/metadata').content
21         def cmd_update_metadata(self, uuid, json_item_data):
22                 print requests.get(base_url+'task/'+uuid+'/metadata').content
23                 print
24                 print requests.post(base_url+'task/'+uuid+'/metadata', headers={'Content-Type':'application/json'}, data=json_item_data).content
25         def cmd_task(self, uuid):
26                 print requests.get(base_url+'task/'+uuid).content
27         def cmd_item(self, uuid, item):
28                 print requests.get(base_url+'task/'+uuid+'/'+item).content
29         def cmd_item_state(self, uuid, item):
30                 print requests.get(base_url+'task/'+uuid+'/'+item+'/state').content
31         def cmd_update_item(self, uuid, item, json_item_data):
32                 print requests.get(base_url+'task/'+uuid+'/'+item).content
33                 print
34                 print requests.post(base_url+'task/'+uuid+'/'+item, headers={'Content-Type':'application/json'}, data=json_item_data).content
35         def cmd_update_item_state(self, uuid, item, old_state, new_state):
36                 '''Example state update
37                 This is a good example of where we can do item updates that will only work if the item
38                 is in the same state as we think it is by passing a 'onlyif' dict.
39                 
40                 Also, if we are doing something like changing state to lock an item for work, we want to
41                 make sure that if someone else is doing the same, we have a way of figuring out who actually
42                 got the lock (simply checking if the state changed is not enough information as someone else
43                 may be trying to change the state to the same thing; If we're both changing it to IN_PROGRESS,
44                 we don't want to both assume that we were the one to change it to that)
45
46                 We guard against this by setting an attribute to a random value, and checking when we get
47                 a response that that random value is what we expect.  There is nothing magical about the
48                 attribute we set, but if all workers don't use the same attribute name, it's not going to be
49                 as useful.  This should only be a problem if you're using different worker codebases against
50                 the same task
51                 '''
52                 # show the existing state just for demonstration purposes. We don't actually use it
53                 print "existing state:",
54                 self.cmd_item_state(uuid,item)
55                 token = random.randint(1,2**48)
56                 updatewith = '{"state": "%s", "_change_state_token": %d, "onlyif": {"state": "%s"}}' % (new_state, token, old_state)
57                 print "updating request:",updatewith
58                 print requests.post(base_url+'task/'+uuid+'/'+item, headers={'Content-Type':'application/json'}, data=updatewith).content
59         def cmd_items_ready(self, uuid):
60                 print requests.get(base_url+'task/'+uuid+'/available').content
61         def cmd_task_delete(self, uuid):
62                 print requests.delete(base_url+'task/'+uuid).content
63
64         commands = property(lambda self: [n[4:] for n in dir(self) if n[:4] == 'cmd_'])
65         def call(self, cmd, *args):  return getattr(self, 'cmd_'+cmd)(*args)
66
67 if __name__ == '__main__':
68         clih = CLIhandler()
69         if len(sys.argv) < 2:
70                 print >> sys.stderr, sys.argv[0],'[',' | '.join(clih.commands),']'
71         else:
72                 clih.call(sys.argv[1], *sys.argv[2:])