Difference between revisions of "Example Clients"

From Textserver wiki
Jump to: navigation, search
(Example Clients for Interactive Jobs)
(Example Clients for Retrieving Results of Batch Requests)
 
(22 intermediate revisions by the same user not shown)
Line 16: Line 16:
 
== Example Clients for Interactive Jobs ==
 
== Example Clients for Interactive Jobs ==
  
Below there is a list of example of how to call a service named <tt>SERVICENAME</tt> from different programming languages
+
Below there is a list of example of how to send an interactive request to a service named <tt>SERVICENAME</tt> using different programming languages.
You'll need to replace <tt>SERVICENAME</tt> with the actual name of the desired service, and adapt the list of parameters in the multipart form to those expected by the service.
+
You'll need to replace <tt>SERVICENAME</tt> with the actual name of the desired service, and adapt the list of parameters in the request to those expected by the service.
  
=== Python ===
+
* '''[[Python2 Interactive Client|Python2]]'''
 +
* '''[[Python3 Interactive Client|Python3]]'''
 +
* '''[[Java Interactive Client|Java]]'''
 +
* '''[[Perl Interactive Client|Perl]]'''
 +
* '''[[PHP Interactive Client|PHP]]'''
 +
* '''[[curl Interactive Client|bash (using curl)]]'''
  
<syntaxhighlight lang="python" line="1" >
+
== Example Clients for Batch Jobs ==
#! /usr/bin/python
+
  
########################################################################
+
Below there is a list of example of how to send a batch request to a service named <tt>SERVICENAME</tt> using different programming languages.
#
+
You'll need to replace <tt>SERVICENAME</tt> with the actual name of the desired service, and adapt the list of parameters in the multipart form to those expected by the service.
#  Example client to submit an interactive request to TextServer
+
# SERVICENAME service.  
+
#  Input may be a plain text or a text file.  No ZIP files.
+
#  Output will be in the requested format (XML, json, conll)
+
#
+
########################################################################
+
 
+
 
+
# import required libraries
+
import urllib2
+
# You may need to install "poster" python module for these two:
+
from poster.encode import multipart_encode
+
from poster.streaminghttp import register_openers
+
 
+
# Register the streaming http handlers with urllib2
+
register_openers()
+
  
# set query elements
+
These clients send the request and obtain the job Token ID from TextServer. This Token ID can be used later to retrieve the results of the job
text = raw_input('Text to analyze: ')
+
lang = raw_input('Language: ')
+
out = raw_input('Output format (xml,json,conll,naf): ')
+
user = raw_input('TextServer Username: ')
+
pwd = raw_input('TextServer Password: ')
+
  
# Encode query in a form-data.
+
* '''[[Python2 Batch Client|Python2]]'''
# 'headers' contains the necessary Content-Type and Content-Length.
+
* '''[[Python3 Batch Client|Python3]]'''
# 'datagen' is a generator object that yields the encoded parameters.
+
* '''[[Java Batch Client|Java]]'''
datagen, headers = multipart_encode({'username':user,
+
* '''[[Perl Batch Client|Perl]]'''
                                    'password':pwd,
+
* '''[[PHP Batch Client|PHP]]'''
                                    'text_input':text,
+
* '''[[curl Batch Client|bash (using curl)]]'''
                                    'language':lang,
+
                                    'output':out,
+
                                    'interactive':'1'
+
                                    } )
+
# service URL
+
service = "SERVICENAME"
+
url = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws/processQuery/"+service
+
  
# Create the Request object
+
== Example Clients for Retrieving Results of Batch Requests ==
request = urllib2.Request(url, datagen, headers)
+
try:
+
  # Actually do the request, and get the response
+
  resp = urllib2.urlopen(request).read()
+
  
except urllib2.HTTPError, e:
+
Below there is a list of examples of how to retrieve the result of a batch job from a remote client.
  # handle connection errors
+
The client just needs the username and the job Token ID to get the results.
  print e, "-", e.read()
+
  exit()
+
  
# No error, appropriately process response
+
* '''[[Batch Results Retrieval in Python2|Python2]]'''
# (e.g. parsing XML or JSON, and doing clever stuff with the content)
+
* '''[[Batch Results Retrieval in Python3|Python3]]'''
print resp
+
* '''[[Batch Results Retrieval in Java|Java]]'''
</syntaxhighlight>
+
* '''[[Batch Results Retrieval in Perl|Perl]]'''
 +
* '''[[Batch Results Retrieval in PHP|PHP]]'''
 +
* '''[[Batch Results Retrieval in curl|bash (using curl)]]'''

Latest revision as of 10:29, 24 August 2018

To call a TextServer service from a client program running on your side, this client needs to send the appropriate request in the right format.

The request is a standard HTTP POST request with a multiform part containing the same information you would submit via the web interface.

Below you'll find a list of example client programs in different programming languages.

The same code can be used to access any TextServer service with small variations:

  • Simply change the service name in the URL to access a different service. You can find out the service name at the top of the "Execute" form of your subscribed services.
  • Replace the parameters in the example client with the parametes that the called service requires. Rules are simple:
    • Language identification service does not require a "language" parameter, but the other services do.
    • All services require a "interactive" parameter stating whether the job is interactive or batch.
    • All services require either a "text_input" parameter (for interactive jobs) or a "input_file" parameter (for batch jobs).
    • You can find out the name of any other specific parameters for a service in the "Execute" form of your subscribed services.

Example Clients for Interactive Jobs

Below there is a list of example of how to send an interactive request to a service named SERVICENAME using different programming languages. You'll need to replace SERVICENAME with the actual name of the desired service, and adapt the list of parameters in the request to those expected by the service.

Example Clients for Batch Jobs

Below there is a list of example of how to send a batch request to a service named SERVICENAME using different programming languages. You'll need to replace SERVICENAME with the actual name of the desired service, and adapt the list of parameters in the multipart form to those expected by the service.

These clients send the request and obtain the job Token ID from TextServer. This Token ID can be used later to retrieve the results of the job

Example Clients for Retrieving Results of Batch Requests

Below there is a list of examples of how to retrieve the result of a batch job from a remote client. The client just needs the username and the job Token ID to get the results.