Difference between revisions of "Curl Batch Client"

From Textserver wiki
Jump to: navigation, search
 
Line 16: Line 16:
 
echo -n "Input ZIP file: "
 
echo -n "Input ZIP file: "
 
read FNAME
 
read FNAME
echo -n "Output ZIP file: "
 
read OUTFNAME
 
 
echo -n "Language: "
 
echo -n "Language: "
 
read LANGUAGE
 
read LANGUAGE
Line 52: Line 50:
 
TOKENID=`cat tokenID.tmp | sed '/<job_token_id>/ ! D; /<job_token_id>/ s#<job_token_id>\(.*\)</job_token_id>#\1#'`
 
TOKENID=`cat tokenID.tmp | sed '/<job_token_id>/ ! D; /<job_token_id>/ s#<job_token_id>\(.*\)</job_token_id>#\1#'`
 
rm tokenID.tmp
 
rm tokenID.tmp
echo "Job submitted. Token id= "$TOKENID
+
echo "Job submitted. Token id="$TOKENID
 
+
# wait and poll periodically until job is complete
+
SECONDS=20
+
while ( test 1 -eq 1 ); do
+
  echo "Sleeping "$SECONDS" seconds."
+
  sleep $SECONDS
+
 
+
  echo "Polling server for job completion..."
+
  curl -X POST  \
+
      --form "username=$USERNAME"  \
+
      --form "tokenID=$TOKENID"  \
+
      $TEXTSERVER_URL"/resultRetrieve" 2>/dev/null >$OUTFNAME
+
 
+
  if ( grep -q '\[TS\-125\]' $OUTFNAME ); then
+
      # server returned [TS-125], which means job not finished
+
      echo "Job not finished yet"
+
      rm $OUTFNAME
+
  else
+
      # server returned 200 OK, stop waiting loop
+
      break
+
  fi
+
done
+
 
+
echo "Job finished. Results saved to "$OUTFNAME
+
 
+
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 12:04, 21 January 2016

 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 "Language: "
18 read LANGUAGE
19 echo -n "Output format (xml,json,conll,naf): "
20 read OUT
21 echo -n "TextServer username: "
22 read USERNAME
23 echo -n "TextServer password: "
24 read PWD
25 
26 TEXTSERVER_URL="http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws"
27 # URL for the requested service
28 SERVICE="SERVICENAME";
29 
30 # send request to server, retrieve job token ID
31 TEMPF=`mktemp -d`
32 curl -X POST  \
33    --form "username=$USERNAME"  \
34    --form "password=$PWD"  \
35    --form "language=$LANGUAGE" \
36    --form "file=@$FNAME" \
37    --form "output=$OUT" \
38    --form "interactive=0" \
39    $TEXTSERVER_URL"/processQuery/"$SERVICE 2>/dev/null >tokenID.tmp
40 
41 # check for TextServer error codes "[TS-XXX]" (invalid parameters, etc..)
42 if ( grep -q '\[TS\-' tokenID.tmp ); then
43   cat tokenID.tmp
44   rm tokenID.tmp
45   exit
46 fi
47 
48 # extract job token id from response
49 TOKENID=`cat tokenID.tmp | sed '/<job_token_id>/ ! D; /<job_token_id>/ s#<job_token_id>\(.*\)</job_token_id>#\1#'`
50 rm tokenID.tmp
51 echo "Job submitted. Token id="$TOKENID