Difference between revisions of "Curl Batch Client"

From Textserver wiki
Jump to: navigation, search
(Created page with "<syntaxhighlight lang="bash" line="1" > </syntaxhighlight>")
 
Line 1: Line 1:
 
<syntaxhighlight lang="bash" line="1" >
 
<syntaxhighlight lang="bash" line="1" >
 +
#! /bin/sh
 +
 +
########################################################################
 +
#
 +
#  Example client to submit a batch request to TextServer
 +
# SERVICENAME service, and wait for the job to be finished.
 +
#
 +
#  Input must be a ZIP file containing one or more text files to analyze
 +
#  Output will be a ZIP file containing results for each input file in
 +
# the requested format (XML, json, conll)
 +
#
 +
########################################################################
 +
 +
# Get query parameters
 +
echo -n "Input ZIP file: "
 +
read FNAME
 +
echo -n "Output ZIP file: "
 +
read OUTFNAME
 +
echo -n "Language: "
 +
read LANGUAGE
 +
echo -n "Output format (xml,json,conll,naf): "
 +
read OUT
 +
echo -n "TextServer username: "
 +
read USERNAME
 +
echo -n "TextServer password: "
 +
read PWD
 +
 +
TEXTSERVER_URL="http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws"
 +
# URL for the requested service
 +
SERVICE="SERVICENAME";
 +
 +
# send request to server, retrieve job token ID
 +
TEMPF=`mktemp -d`
 +
curl -X POST  \
 +
  --form "username=$USERNAME"  \
 +
  --form "password=$PWD"  \
 +
  --form "language=$LANGUAGE" \
 +
  --form "file=@$FNAME" \
 +
  --form "output=$OUT" \
 +
  --form "interactive=0" \
 +
  $TEXTSERVER_URL"/processQuery/"$SERVICE 2>/dev/null >tokenID.tmp
 +
 +
# check for TextServer error codes "[TS-XXX]" (invalid parameters, etc..)
 +
if ( grep -q '\[TS\-' tokenID.tmp ); then
 +
  cat tokenID.tmp
 +
  rm tokenID.tmp
 +
  exit
 +
fi
 +
 +
# extract job token id from response
 +
TOKENID=`cat tokenID.tmp | sed '/<job_token_id>/ ! D; /<job_token_id>/ s#<job_token_id>\(.*\)</job_token_id>#\1#'`
 +
rm tokenID.tmp
 +
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>

Revision as of 13:15, 20 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 "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