Examples

REST API using HTTPS

In this example we want to show that it is equally simple to interface via messages with systems that use a different protocol then the CellSpeak message protocol.

Systems that have an API, exchange messages using the HTTP protocol. The messages consist of a request (GET, PU, POST etc), a variable number of headers and optionally some content of the message. The CellSpeak VM formats incoming and outgoing HTTP messages to the CellSpeak format, so that they can be sent and received by the cell that is handling the communication.


(1)


(2)


(3)


(4)

14 REST API using HTTPS.celsrc
-.-
	RESTful API
	
	In this example we set up a communication with a server that exposes a REST API.
	Not surprisingly the set up of this example is very similar to the previous example.
	
	We will also use SSL/TSL over the communication link, which offers added 
	protection through encryption and certification. Setting up the secure comms is handled 
	by the Connection Manager and has no further effect on the code of the example.
	
	The API we use here is from a site made for testing HTTP exchanges, httpbin.org. 
	Many scenario's can be tried out using that excellent site.	
-.-
use Windows, Math, Strings, Editor, System
use HTTPTypes

-- The cel that starts this example - just create a window and a client
design RESTfulAPI is
	constructor is 
		-- Create a window for informative output
		cell Window = create MenuWindow("Test of a RESTful API")
		
		-- send a message to the system to set stdout	
		system <- stdout.Set(Window)
		
		-- And create a client to connect to a server.
		create ClientDesign
	end
end

-- The client sets up the comms to the server
design ClientDesign is 
	cell ConxMgr		
	ansi Server = "httpbin.org", SSLPort="443"

	constructor is 
		-- Get the connection manager
		system <- Service.Get("ConxMgr")	
	end
	
	on Service.Provider(ansi Service, cell Provider) do
		-- check 
		Service is "ConxMgr" ? ConxMgr = Provider : yield
		
		-- Ask the connection manager to connect to a host
(1)		ConxMgr <- TCPIP.SSL.Connect(Server,SSLPort)
	end
	
	-- if the connection was successful we get this message
	on TCPIP.SSL.Connected( ConxClass Conx) do
		-- give a message
		system <- println("TCPIP.SSL.Connected")
		
		-- as before we set the protocol and the handler
(2)		ConxMgr <- Set.Protocol(Conx, "HTTP", self)	
	end
	
	interface HTTP is	
		-- we get a message when the protocol has been set and we can start sending
		on Ready( cell Avatar) do
		
			-- we wil use these for the reply
			Header  RequestHeaders[]

			-- inform 
			system <- println("Client: Connection with server [Server] successful. Avatar: [Avatar] ")
			
			-- The HTTP headers that we want to use for this request	
			RequestHeaders = [	[ "Host", "httpbin.org"],
								[ "User-Agent", "CellSpeak 1.0"],
								[ "Accept", "*/*"],
								[ "Accept-Encoding", "gzip, deflate"] ]		
					
			-- One of the services available on httpbin.org	
			ansi URI = "/encoding/utf8"
		
			-- Now we send a GET request to the remote server 
(3)			Avatar <- GET(URI,"HTTP/1.1", RequestHeaders, null)				
		end
				
		-- Receive the response
(4)		on RESPONSE(ansi Version, ansi Status, ansi ReasonPhrase, Header[] Headers, byte[] Content) do	
			-- inform
			system <- println("[Version] [Status] [ReasonPhrase]")
						
			-- Print the headers..
			for each Header in Headers => system <- println("Header    [Header.Name]: [Header.Value]")
			
			-- and just show the content
			system <- print(Content)	
		end	
	interface end
end