annotate mergeclient.py @ 94:2ef34abbad8d 1.5

Use http error code 422 in CheckView when a merge is not possible. Previously the response used the code 500 in this case. This is not useful because it's not an internal server error when the check whether a PDF file can be merged fails because that's the point of the CheckView. The code used now means "Unprocessable Entity" and fits better. Part of mpuls/issue6009
author Bernhard Herzog <bh@intevation.de>
date Fri, 28 Apr 2017 20:56:22 +0200
parents 4645e50539ff
children 349d49bb69f4
rev   line source
21
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
1 #!/usr/bin/env python
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
3
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
4 import optparse
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
5 import sys
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
6
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
7 import requests
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
8
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
9
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
10 def main():
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
11 usage = "usage: %prog [options] file1 file2 [ file3, ...]"
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
12 parser = optparse.OptionParser(usage=usage)
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
13 parser.add_option("-s", "--host", default="localhost")
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
14 parser.add_option("-p", "--port", default="5000")
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
15 parser.add_option("-o", "--out", default="merged.pdf", dest="out")
63
3918c3c69485 Add ignore_file_errors option to mergeclient
Björn Ricks <bjoern.ricks@intevation.de>
parents: 21
diff changeset
16 parser.add_option("-i", "--ignore-file-errors", action="store_true",
3918c3c69485 Add ignore_file_errors option to mergeclient
Björn Ricks <bjoern.ricks@intevation.de>
parents: 21
diff changeset
17 default=False, dest="ignorefileerrors")
68
4645e50539ff Add support for posting files with own form names for each file
Björn Ricks <bjoern.ricks@intevation.de>
parents: 63
diff changeset
18 parser.add_option("-m", "--multi", action="store_true",
4645e50539ff Add support for posting files with own form names for each file
Björn Ricks <bjoern.ricks@intevation.de>
parents: 63
diff changeset
19 default=False, dest="multi")
21
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
20 (options, args) = parser.parse_args()
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
21
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
22 if len(args) < 2:
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
23 parser.print_usage()
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
24 sys.exit(1)
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
25
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
26 service = "merge"
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
27 url = "http://%s:%s/%s/" % (options.host, options.port, service)
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
28
63
3918c3c69485 Add ignore_file_errors option to mergeclient
Björn Ricks <bjoern.ricks@intevation.de>
parents: 21
diff changeset
29 if options.ignorefileerrors:
3918c3c69485 Add ignore_file_errors option to mergeclient
Björn Ricks <bjoern.ricks@intevation.de>
parents: 21
diff changeset
30 url += "?ignore_file_errors=1"
3918c3c69485 Add ignore_file_errors option to mergeclient
Björn Ricks <bjoern.ricks@intevation.de>
parents: 21
diff changeset
31
21
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
32 files = []
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
33
68
4645e50539ff Add support for posting files with own form names for each file
Björn Ricks <bjoern.ricks@intevation.de>
parents: 63
diff changeset
34 if options.multi:
4645e50539ff Add support for posting files with own form names for each file
Björn Ricks <bjoern.ricks@intevation.de>
parents: 63
diff changeset
35 for i, filename in enumerate(args):
4645e50539ff Add support for posting files with own form names for each file
Björn Ricks <bjoern.ricks@intevation.de>
parents: 63
diff changeset
36 files.append(("file%s" % i, open(filename, 'rb')))
4645e50539ff Add support for posting files with own form names for each file
Björn Ricks <bjoern.ricks@intevation.de>
parents: 63
diff changeset
37 else:
4645e50539ff Add support for posting files with own form names for each file
Björn Ricks <bjoern.ricks@intevation.de>
parents: 63
diff changeset
38 for filename in args:
4645e50539ff Add support for posting files with own form names for each file
Björn Ricks <bjoern.ricks@intevation.de>
parents: 63
diff changeset
39 files.append(('files', open(filename, 'rb')))
21
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
40
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
41 r = requests.post(url, files=files)
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
42
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
43 if r.status_code == 200:
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
44 with open(options.out, "wb") as f:
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
45 f.write(r.content)
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
46 print "OK"
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
47 else:
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
48 print "An error has occured"
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
49 print r.status_code, r.headers
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
50 print r.text
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
51 sys.exit(2)
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
52
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
53 if __name__ == "__main__":
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
54 main()
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)