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
use Windows, Math, Strings, Editor, System
use HTTPTypes
design RESTfulAPI is
constructor is
cell Window = create MenuWindow("Test of a RESTful API")
system <- stdout.Set(Window)
create ClientDesign
end
end
design ClientDesign is
cell ConxMgr
ansi Server = "httpbin.org", SSLPort="443"
constructor is
system <- Service.Get("ConxMgr")
end
on Service.Provider(ansi Service, cell Provider) do
Service is "ConxMgr" ? ConxMgr = Provider : yield
(1) ConxMgr <- TCPIP.SSL.Connect(Server,SSLPort)
end
on TCPIP.SSL.Connected( ConxClass Conx) do
system <- println("TCPIP.SSL.Connected")
(2) ConxMgr <- Set.Protocol(Conx, "HTTP", self)
end
interface HTTP is
on Ready( cell Avatar) do
Header RequestHeaders[]
system <- println("Client: Connection with server [Server] successful. Avatar: [Avatar] ")
RequestHeaders = [ [ "Host", "httpbin.org"],
[ "User-Agent", "CellSpeak 1.0"],
[ "Accept", "*/*"],
[ "Accept-Encoding", "gzip, deflate"] ]
ansi URI = "/encoding/utf8"
(3) Avatar <- GET(URI,"HTTP/1.1", RequestHeaders, null)
end
(4) on RESPONSE(ansi Version, ansi Status, ansi ReasonPhrase, Header[] Headers, byte[] Content) do
system <- println("[Version] [Status] [ReasonPhrase]")
for each Header in Headers => system <- println("Header [Header.Name]: [Header.Value]")
system <- print(Content)
end
interface end
end