Updated, improved and cleaned RESTbucks example.
Moved it under "rest" subfolder.
This commit is contained in:
11
examples/rest/restbucks_CRUD/client/README.txt
Normal file
11
examples/rest/restbucks_CRUD/client/README.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
Make sure to have the Clib generated in the related cURL library
|
||||
|
||||
- if you use EiffelStudio >= 7.0
|
||||
check %ISE_LIBRARY%\library\cURL\spec\%ISE_C_COMPILER%\$ISE_PLATFORM
|
||||
or $ISE_LIBRARY/library/cURL/spec/$ISE_PLATFORM
|
||||
|
||||
- otherwise if you use earlier version
|
||||
check under ext/ise_library/curl/spec/...
|
||||
|
||||
And on Windows, be sure to get the libcurl.dll from %ISE_LIBRARY%\studio\spec\%ISE_PLATFORM%\bin\libcurl.dll
|
||||
|
||||
19
examples/rest/restbucks_CRUD/client/client.ecf
Normal file
19
examples/rest/restbucks_CRUD/client/client.ecf
Normal file
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="ISO-8859-1"?>
|
||||
<system xmlns="http://www.eiffel.com/developers/xml/configuration-1-15-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.eiffel.com/developers/xml/configuration-1-15-0 http://www.eiffel.com/developers/xml/configuration-1-15-0.xsd" name="client" uuid="D0059CEB-5F5C-4D21-8C71-842BD0F88468">
|
||||
<target name="client">
|
||||
<root class="RESTBUCK_CLIENT" feature="make"/>
|
||||
<file_rule>
|
||||
<exclude>/.git$</exclude>
|
||||
<exclude>/.svn$</exclude>
|
||||
<exclude>/EIFGENs$</exclude>
|
||||
</file_rule>
|
||||
<option warning="true" void_safety="all">
|
||||
</option>
|
||||
<setting name="console_application" value="true"/>
|
||||
<setting name="concurrency" value="scoop"/>
|
||||
<library name="base" location="$ISE_LIBRARY\library\base\base-safe.ecf"/>
|
||||
<library name="http_client" location="..\..\..\..\library\network\http_client\http_client-safe.ecf"/>
|
||||
<library name="json" location="$ISE_LIBRARY\contrib\library\text\parser\json\library\json-safe.ecf"/>
|
||||
<cluster name="src" location=".\src\" recursive="true"/>
|
||||
</target>
|
||||
</system>
|
||||
177
examples/rest/restbucks_CRUD/client/src/restbuck_client.e
Normal file
177
examples/rest/restbucks_CRUD/client/src/restbuck_client.e
Normal file
@@ -0,0 +1,177 @@
|
||||
note
|
||||
description : "Objects that ..."
|
||||
author : "$Author$"
|
||||
date : "$Date$"
|
||||
revision : "$Revision$"
|
||||
|
||||
class
|
||||
RESTBUCK_CLIENT
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make
|
||||
-- Initialize `Current'.
|
||||
local
|
||||
h: NET_HTTP_CLIENT
|
||||
sess: HTTP_CLIENT_SESSION
|
||||
resp : detachable HTTP_CLIENT_RESPONSE
|
||||
l_location : detachable READABLE_STRING_8
|
||||
body : STRING
|
||||
do
|
||||
create h
|
||||
sess := h.new_session ("http://127.0.0.1:" + server_port.out)
|
||||
-- Uncomment the following 2 lines, if you use fiddler2 web debugging tool
|
||||
-- sess.set_is_debug (True)
|
||||
-- sess.set_proxy ("127.0.0.1", 8888)
|
||||
|
||||
-- Create Order
|
||||
print ("> Create Order %N")
|
||||
resp := create_order (sess)
|
||||
display_response (resp)
|
||||
|
||||
|
||||
-- Read the Order
|
||||
l_location := resp.header ("Location")
|
||||
if l_location /= Void then
|
||||
print ("> Read Order from " + l_location + " %N")
|
||||
resp := read_order (sess, l_location)
|
||||
display_response (resp)
|
||||
else
|
||||
print ("> Previous order creation failed, no location returned!%N")
|
||||
end
|
||||
|
||||
-- Update the Order
|
||||
if resp /= Void and then attached resp.body as l_body then
|
||||
body := l_body.as_string_8
|
||||
body.replace_substring_all ("takeAway", "in Shop")
|
||||
print ("> Update Order: change location from [takeAway] to [in Shop] %N")
|
||||
resp := update_order (sess, l_location, body)
|
||||
display_response (resp)
|
||||
end
|
||||
|
||||
-- Pay the Order
|
||||
if resp /= Void and then attached resp.body as l_body then
|
||||
body := l_body.as_string_8
|
||||
body.replace_substring_all ("%"status%":%"submitted%"", "%"status%":%"paid%"")
|
||||
print ("> Pay Order: should trigger validation error!%N")
|
||||
resp := update_order (sess, l_location, body)
|
||||
display_response (resp)
|
||||
end
|
||||
end
|
||||
|
||||
update_order (sess: HTTP_CLIENT_SESSION; uri: detachable READABLE_STRING_8; a_body: STRING): detachable HTTP_CLIENT_RESPONSE
|
||||
local
|
||||
context : HTTP_CLIENT_REQUEST_CONTEXT
|
||||
do
|
||||
if uri /= Void then
|
||||
sess.set_base_url (uri)
|
||||
create context.make
|
||||
context.headers.put ("application/json", "Content-Type")
|
||||
Result := sess.put ("", context, a_body )
|
||||
end
|
||||
end
|
||||
|
||||
read_order (sess: HTTP_CLIENT_SESSION; uri: detachable READABLE_STRING_8): detachable HTTP_CLIENT_RESPONSE
|
||||
do
|
||||
if uri /= Void then
|
||||
sess.set_base_url (uri)
|
||||
Result := sess.get ("", Void)
|
||||
end
|
||||
end
|
||||
|
||||
create_order (sess: HTTP_CLIENT_SESSION) : HTTP_CLIENT_RESPONSE
|
||||
local
|
||||
s: READABLE_STRING_8
|
||||
j: JSON_PARSER
|
||||
context : HTTP_CLIENT_REQUEST_CONTEXT
|
||||
do
|
||||
s := "[
|
||||
{
|
||||
"location":"takeAway",
|
||||
"items":[
|
||||
{
|
||||
"name":"Late",
|
||||
"option":"skim",
|
||||
"size":"Small",
|
||||
"quantity":1
|
||||
}
|
||||
]
|
||||
}
|
||||
]"
|
||||
|
||||
create context.make
|
||||
context.headers.put ("application/json", "Content-Type")
|
||||
Result := sess.post ("/order", context, s)
|
||||
end
|
||||
|
||||
display_response (resp: detachable HTTP_CLIENT_RESPONSE)
|
||||
do
|
||||
io.error.put_string (create {STRING}.make_filled ('-', 40))
|
||||
io.error.put_new_line
|
||||
if resp = Void then
|
||||
io.error.put_string ("ERROR: No response~%N")
|
||||
else
|
||||
if resp.error_occurred and attached resp.error_message as err_msg then
|
||||
io.error.put_string ("[ERROR] ")
|
||||
io.error.put_string (err_msg)
|
||||
io.error.put_new_line
|
||||
end
|
||||
|
||||
-- Display response status and header
|
||||
io.error.put_string ("Status code: " + resp.status.out + "%N")
|
||||
across
|
||||
resp.headers as l_headers
|
||||
loop
|
||||
io.error.put_string (l_headers.item.name)
|
||||
io.error.put_string (":")
|
||||
io.error.put_string (l_headers.item.value)
|
||||
io.error.put_new_line
|
||||
end
|
||||
|
||||
-- Show the Response body
|
||||
if not resp.error_occurred and attached resp.body as m then
|
||||
io.error.put_string (m)
|
||||
io.error.put_new_line
|
||||
end
|
||||
end
|
||||
io.error.put_string (create {STRING}.make_filled ('-', 40))
|
||||
io.error.put_new_line
|
||||
end
|
||||
|
||||
feature {NONE} -- Implementation
|
||||
|
||||
server_port: INTEGER
|
||||
local
|
||||
f: PLAIN_TEXT_FILE
|
||||
p: PATH
|
||||
s: STRING
|
||||
do
|
||||
create p.make_current
|
||||
p := p.extended ("..").extended ("server.ini")
|
||||
create f.make_with_path (p)
|
||||
if f.exists and then f.is_access_readable then
|
||||
f.open_read
|
||||
from
|
||||
until
|
||||
f.exhausted or f.end_of_file or Result > 0
|
||||
loop
|
||||
f.read_line
|
||||
s := f.last_string
|
||||
if s.starts_with_general ("port=") then
|
||||
s.remove_head (5)
|
||||
if s.is_integer then
|
||||
Result := s.to_integer
|
||||
end
|
||||
end
|
||||
end
|
||||
f.close
|
||||
end
|
||||
if Result <= 0 then
|
||||
Result := 80
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user