Examples

Google Translation API

This is another example of how to use an API, but this time a commercially available API. The only difference with the previous exampel is that you will need to obtain a commercial or a test key from Google before you can actually run this example (check out the Google API console on how to obtain a key)

The purpose of the example is to show how simple it is to add a translation service to an application.

Google Translation API.celsrc
use Windows, Math, Strings, Editor, System
use HTTPTypes

-- The cel to start the application
design GoogleApi is
	constructor is 
		-- Create a window
		cell Window = create MenuWindow("Test of a Google API")
		
		-- send a message to the system to set stdout, so other cells can use it as well	
		system <- stdout.Set(Window)
		
		-- And create a client to connect to a server.
		create ClientDesign
	end
end

-- The server replies to requests from the browser
design ClientDesign is 

	cell ConxMgr		
	ansi Server ="translation.googleapis.com", Port="443"

	constructor is 	
		-- Get the connection manager
		system <- Service.Get("ConxMgr")	
	end
	
	on Service.Provider(ansi Service, cell Provider) do
		-- check that we have received the right service - otherwise yield
		Service is "ConxMgr" ? ConxMgr = Provider : yield
		
		-- Ask the connection manager to connect to the server
		ConxMgr <- TCPIP.SSL.Connect(Server,Port)
	end
	
	-- if the connection was successful we get this message
	on TCPIP.SSL.Connected( ConxClass Conx) do	
		-- Set the protocol
		ConxMgr <- Set.Protocol(Conx, "HTTP", self)	
	end
	
	-- we get a message when the protocol has been set ..
	on HTTP.Ready( cell Avatar) do	
		-- we wil use these for the reply
		Header  RequestHeaders[]

		-- inform 
		system <- println("*** HTTPS *** Client [self]: Connection with server [Server] successful. Cell: [Avatar] ")
		
		-- Your API key here ( For a real world application, you would want to keep this in a seperate file !)
		var GoogleApiKey = "your key here"
			
		-- The HTTP headers that we want to use for this response
		RequestHeaders = [	[ "Host", Server],
							[ "User-Agent", "CellSpeak 1.0"],
							[ "Accept", "*/*"]  ]
								
		-- The sentence we want to translate
		ansi URI = "/language/translate/v2?key=[GoogleApiKey]&source=en&target=de&q=Hello%20world&q=My%20name%20is%20Jeff"
		
		-- Now we send a GET request to the remote server 
		Avatar <- GET(URI, "HTTP/1.1", RequestHeaders, null)	
	end
			
	interface HTTP is
		
		-- We received a get request
		on GET(ansi URI, ansi Version, Header[] Headers, byte[] Content) do
		
			-- we wil use these for the reply
			Header  ResponseHeaders[]
	
			-- inform
			system <- println("*** Client [self]: Server [sender] GET request URI=[URI] Version=[Version]")
			
			-- Print the headers
			for each Header in Headers do
				system <- println("Header    [Header.Name]: [Header.Value]")
			end		
			
		end
	
		-- We received a get request
		on RESPONSE(ansi Version, ansi Status, ansi ReasonPhrase, Header[] Headers, byte[] Content) do
		
			-- we wil use these for the reply
			Header  ResponseHeaders[]
	
			-- inform
			system <- println("[Version] [Status] [ReasonPhrase]")
						
			-- Print the headers
			for each Header in Headers do
				system <- println("Header    [Header.Name]: [Header.Value]")
			end	

			-- show content
			system <- print(Content)		
		end
	interface end
end