Curl Batch Client

From Textserver wiki
Revision as of 13:15, 20 January 2016 by Padro (Talk | contribs)

Jump to: navigation, search
 1 #! /bin/sh 
 2 
 3 ########################################################################
 4 #
 5 #  Example client to submit a batch request to TextServer 
 6 # SERVICENAME service, and wait for the job to be finished.
 7 #
 8 #  Input must be a ZIP file containing one or more text files to analyze
 9 #  Output will be a ZIP file containing results for each input file in
10 # the requested format (XML, json, conll)
11 #
12 ########################################################################
13 
14 # Get query parameters
15 echo -n "Input ZIP file: "
16 read FNAME
17 echo -n "Output ZIP file: "
18 read OUTFNAME
19 echo -n "Language: "
20 read LANGUAGE
21 echo -n "Output format (xml,json,conll,naf): "
22 read OUT
23 echo -n "TextServer username: "
24 read USERNAME
25 echo -n "TextServer password: "
26 read PWD
27 
28 TEXTSERVER_URL="http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws"
29 # URL for the requested service
30 SERVICE="SERVICENAME";
31 
32 # send request to server, retrieve job token ID
33 TEMPF=`mktemp -d`
34 curl -X POST  \
35    --form "username=$USERNAME"  \
36    --form "password=$PWD"  \
37    --form "language=$LANGUAGE" \
38    --form "file=@$FNAME" \
39    --form "output=$OUT" \
40    --form "interactive=0" \
41    $TEXTSERVER_URL"/processQuery/"$SERVICE 2>/dev/null >tokenID.tmp
42 
43 # check for TextServer error codes "[TS-XXX]" (invalid parameters, etc..)
44 if ( grep -q '\[TS\-' tokenID.tmp ); then
45   cat tokenID.tmp
46   rm tokenID.tmp
47   exit
48 fi
49 
50 # extract job token id from response
51 TOKENID=`cat tokenID.tmp | sed '/<job_token_id>/ ! D; /<job_token_id>/ s#<job_token_id>\(.*\)</job_token_id>#\1#'`
52 rm tokenID.tmp
53 echo "Job submitted. Token id= "$TOKENID
54 
55 # wait and poll periodically until job is complete
56 SECONDS=20
57 while ( test 1 -eq 1 ); do
58   echo "Sleeping "$SECONDS" seconds." 
59   sleep $SECONDS
60 
61   echo "Polling server for job completion..."
62   curl -X POST  \
63       --form "username=$USERNAME"  \
64       --form "tokenID=$TOKENID"  \
65       $TEXTSERVER_URL"/resultRetrieve" 2>/dev/null >$OUTFNAME
66 
67   if ( grep -q '\[TS\-125\]' $OUTFNAME ); then
68       # server returned [TS-125], which means job not finished
69       echo "Job not finished yet"
70       rm $OUTFNAME
71   else
72       # server returned 200 OK, stop waiting loop
73       break
74   fi
75 done
76 
77 echo "Job finished. Results saved to "$OUTFNAME