streamsx.inet package¶
Internet protocol integration for IBM Streams¶
For details of implementing applications in Python for IBM Streams including IBM Cloud Pak for Data and the Streaming Analytics service running on IBM Cloud see:
This package exposes SPL operators in the com.ibm.streamsx.inet toolkit as Python methods.
Overview¶
Provides functions to run HTTP requests.
- The following method types are supported:
GET
DELETE
PUT
POST
Sample¶
A simple example of a Streams application that emits http requests:
from streamsx.topology.topology import *
from streamsx.topology.schema import CommonSchema, StreamSchema
from streamsx.topology.context import submit
import streamsx.inet as inet
topo = Topology()
# HTTP GET REQUEST
s1 = topo.source(['http://httpbin.org/get']).as_string()
result_http_get = inet.request_get(s1)
result_http_get.print()
# HTTP PUT REQUEST
s2 = topo.source(['hello world']).as_string()
result_http_put = inet.request_put(s2, url='http://httpbin.org/put', content_type='text/plain')
result_http_put.print()
submit('STREAMING_ANALYTICS_SERVICE', topo)
# Use for IBM Streams including IBM Cloud Pak for Data
# submit ('DISTRIBUTED', topo)
Types¶
HttpResponseSchema
- Structured schema containing HTTP GET/PUT/POST/DELETE response values.
The functions returns a stream with this schema.
tuple<
rstring status,
int32 statusCode,
rstring contentEncoding,
rstring contentType,
list<rstring> responseHeader,
rstring responseData
>
-
streamsx.inet.
download_toolkit
(url=None, target_dir=None)¶ Downloads the latest Inet toolkit from GitHub.
Example for updating the Inet toolkit for your topology with the latest toolkit from GitHub:
import streamsx.inet as inet # download Inet toolkit from GitHub inet_toolkit_location = inet.download_toolkit() # add the toolkit to topology streamsx.spl.toolkit.add_toolkit(topology, inet_toolkit_location)
Example for updating the topology with a specific version of the Inet toolkit using a URL:
import streamsx.inet as inet url310 = 'https://github.com/IBMStreams/streamsx.inet/releases/download/v3.1.0/streamsx.inet.toolkit-3.1.0-el6-amd64-70c49d5-20190320-1318.tgz' inet_toolkit_location = inet.download_toolkit(url=url310) streamsx.spl.toolkit.add_toolkit(topology, inet_toolkit_location)
- Parameters
url (str) – Link to toolkit archive (*.tgz) to be downloaded. Use this parameter to download a specific version of the toolkit.
target_dir (str) – the directory where the toolkit is unpacked to. If a relative path is given, the path is appended to the system temporary directory, for example to /tmp on Unix/Linux systems. If target_dir is
None
a location relative to the system temporary directory is chosen.
- Returns
the location of the downloaded Inet toolkit
- Return type
str
Note
This function requires an outgoing Internet connection
New in version 1.2.
-
streamsx.inet.
request_delete
(stream, url=None, url_attribute=None, extra_header_attribute=None, ssl_accept_all_certificates=False, name=None)¶ Issues HTTP DELETE requests. For each input tuple a DELETE request is issued and the response is on the returned stream. You can specifiy the URL either dynamic (part of input stream) or static (as parameter).
Example with URL as part of the input stream of type
CommonSchema.String
. The parametersurl
andurl_attribute
can be omitted in this case:import streamsx.inet as inet s = topo.source(['http://httpbin.org/delete']).as_string() result_http_del = inet.request_delete(s) result_http_del.print()
- Parameters
stream (streamsx.topology.topology.Stream) – Stream of tuples containing the HTTP request url. Supports
streamsx.topology.schema.StreamSchema
(schema for a structured stream) orCommonSchema.String
as input.url (str) – String containing the URL to send HTTP requests to.
url_attribute (str) – Attribute name of the input stream containing the URL to send HTTP requests to. Use this as alternative to the ‘url’ parameter.
extra_header_attribute (str) – Attribute name of the input stream containing one extra header to send with the request, the attribute must contain a string in the form Header-Name: value. If the attribute value is an empty string, no additional header is send.
ssl_accept_all_certificates (bool) – Accept all SSL certificates, even those that are self-signed. Setting this option will allow potentially insecure connections. Default is false.
name (str) – Sink name in the Streams context, defaults to a generated name.
- Returns
Output Stream with schema
HttpResponseSchema
.- Return type
-
streamsx.inet.
request_get
(stream, url=None, url_attribute=None, extra_header_attribute=None, ssl_accept_all_certificates=False, name=None)¶ Issues HTTP GET requests. For each input tuple a GET request is issued and the response is on the returned stream. You can specifiy the URL either dynamic (part of input stream) or static (as parameter).
Example with URL as part of the input stream of type
CommonSchema.String
. The parametersurl
andurl_attribute
can be omitted in this case:import streamsx.inet as inet s = topo.source(['http://httpbin.org/get']).as_string() result_http_get = inet.request_get(s) result_http_get.print()
- Parameters
stream (streamsx.topology.topology.Stream) – Stream of tuples containing the HTTP request url. Supports
streamsx.topology.schema.StreamSchema
(schema for a structured stream) orCommonSchema.String
as input.url (str) – String containing the URL to send HTTP requests to.
url_attribute (str) – Attribute name of the input stream containing the URL to send HTTP requests to. Use this as alternative to the ‘url’ parameter.
extra_header_attribute (str) – Attribute name of the input stream containing one extra header to send with the request, the attribute must contain a string in the form Header-Name: value. If the attribute value is an empty string, no additional header is send.
ssl_accept_all_certificates (bool) – Accept all SSL certificates, even those that are self-signed. Setting this option will allow potentially insecure connections. Default is false.
name (str) – Sink name in the Streams context, defaults to a generated name.
- Returns
Output Stream with schema
HttpResponseSchema
.- Return type
-
streamsx.inet.
request_post
(stream, url=None, url_attribute=None, body_attribute=None, content_type=None, content_type_attribute=None, extra_header_attribute=None, ssl_accept_all_certificates=False, name=None)¶ Issues HTTP POST requests. For each input tuple a POST request is issued and the response is on the returned stream. You can specifiy the URL either dynamic (part of input stream) or static (as parameter).
Example with URL as part of the input stream of type
CommonSchema.String
. The parametersurl
andurl_attribute
can be omitted in this case:import streamsx.inet as inet s = topo.source(['http://httpbin.org/post']).as_string() result_http_post = inet.request_post(s) result_http_post.print()
Example with URL as part of the input stream and content type as parameter:
import streamsx.inet as inet s = topo.source(['http://httpbin.org/post']).as_string() result_http_post = inet.request_post(s, content_type='application/x-www-form-urlencoded') result_http_post.print()
- Parameters
stream (streamsx.topology.topology.Stream) – Stream of tuples containing the HTTP request url. Supports
streamsx.topology.schema.StreamSchema
(schema for a structured stream) orCommonSchema.String
as input.url (str) – String containing the URL to send HTTP requests to.
url_attribute (str) – Attribute name of the input stream containing the URL to send HTTP requests to. Use this as alternative to the ‘url’ parameter.
body_attribute (str) – Request body attribute for POST method that accepts an entity.
content_type (str) – MIME content type of entity for POST requests. If not specified the default ‘application/json’ is used.
content_type_attribute (str) – Attribute name of the input stream containing the MIME content type. Use this as alternative to the ‘content_type’ parameter.
extra_header_attribute (str) – Attribute name of the input stream containing one extra header to send with the request, the attribute must contain a string in the form Header-Name: value. If the attribute value is an empty string, no additional header is send.
ssl_accept_all_certificates (bool) – Accept all SSL certificates, even those that are self-signed. Setting this option will allow potentially insecure connections. Default is false.
name (str) – Sink name in the Streams context, defaults to a generated name.
- Returns
Output Stream with schema
HttpResponseSchema
.- Return type
-
streamsx.inet.
request_put
(stream, url=None, url_attribute=None, body_attribute=None, content_type=None, content_type_attribute=None, extra_header_attribute=None, ssl_accept_all_certificates=False, name=None)¶ Issues HTTP PUT requests. For each input tuple a PUT request is issued and the response is on the returned stream. You can specifiy the URL either dynamic (part of input stream) or static (as parameter).
Example with parameters
url
,content_type
and input stream containing the request body:import streamsx.inet as inet s = topo.source(['hello world']).as_string() result_http_put = inet.request_put(s, url='http://httpbin.org/put', content_type='text/plain') result_http_put.print()
- Parameters
stream (streamsx.topology.topology.Stream) – Stream of tuples containing the HTTP request url. Supports
streamsx.topology.schema.StreamSchema
(schema for a structured stream) orCommonSchema.String
as input.url (str) – String containing the URL to send HTTP requests to.
url_attribute (str) – Attribute name of the input stream containing the URL to send HTTP requests to. Use this as alternative to the ‘url’ parameter.
body_attribute (str) – Request body attribute for PUT method that accepts an entity.
content_type (str) – MIME content type of entity for PUT requests. If not specified the default ‘application/json’ is used.
content_type_attribute (str) – Attribute name of the input stream containing the MIME content type. Use this as alternative to the ‘content_type’ parameter.
extra_header_attribute (str) – Attribute name of the input stream containing one extra header to send with the request, the attribute must contain a string in the form Header-Name: value. If the attribute value is an empty string, no additional header is send.
ssl_accept_all_certificates (bool) – Accept all SSL certificates, even those that are self-signed. Setting this option will allow potentially insecure connections. Default is false.
name (str) – Sink name in the Streams context, defaults to a generated name.
- Returns
Output Stream with schema
HttpResponseSchema
.- Return type