Update restbuck client, create and read an order.
Update JSON converter, the id is not important, applied the DRY principle. Update the ORDER_HANDLER to use the meta_string_variable instead of meta_variable from req. Fix, the key in meta_variable_table, use c.key instead of c.item
This commit is contained in:
@@ -17,25 +17,102 @@ feature {NONE} -- Initialization
|
|||||||
local
|
local
|
||||||
h: LIBCURL_HTTP_CLIENT
|
h: LIBCURL_HTTP_CLIENT
|
||||||
sess: HTTP_CLIENT_SESSION
|
sess: HTTP_CLIENT_SESSION
|
||||||
|
resp : HTTP_CLIENT_RESPONSE
|
||||||
|
l_location : detachable READABLE_STRING_8
|
||||||
|
body : STRING
|
||||||
do
|
do
|
||||||
create h.make
|
create h.make
|
||||||
sess := h.new_session ("http://127.0.0.1:8080")
|
sess := h.new_session ("http://127.0.0.1:8080")
|
||||||
-- Create Order
|
-- Create Order
|
||||||
create_order (sess)
|
print ("%N Create Order %N")
|
||||||
|
resp := create_order (sess)
|
||||||
|
|
||||||
-- if id /= Void and then attached sess.get ("/order/" + id, Void) as r then
|
-- Read the Order
|
||||||
-- print (r.body)
|
print ("%N Read Order %N")
|
||||||
-- io.put_new_line
|
l_location := resp.headers.at ("Location")
|
||||||
-- end
|
resp := read_order (sess, l_location)
|
||||||
|
|
||||||
|
|
||||||
|
-- Update the Order
|
||||||
|
|
||||||
|
if attached resp.body as l_body then
|
||||||
|
body := l_body.as_string_8
|
||||||
|
body.replace_substring_all ("takeAway", "in Shop")
|
||||||
|
print ("%N Update Order %N")
|
||||||
|
resp := update_order (sess, l_location, body)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
update_order ( sess: HTTP_CLIENT_SESSION; uri : detachable READABLE_STRING_8; a_body : STRING) : HTTP_CLIENT_RESPONSE
|
||||||
|
local
|
||||||
|
l_headers: HASH_TABLE [READABLE_STRING_8,READABLE_STRING_8]
|
||||||
|
do
|
||||||
|
create Result.make
|
||||||
|
if attached uri as l_uri then
|
||||||
|
sess.set_base_url (l_uri)
|
||||||
|
Result := sess.put ("", Void, a_body )
|
||||||
|
if attached Result as r then
|
||||||
|
-- Show headers
|
||||||
|
l_headers := r.headers
|
||||||
|
from
|
||||||
|
l_headers.start
|
||||||
|
until
|
||||||
|
l_headers.after
|
||||||
|
loop
|
||||||
|
print (l_headers.key_for_iteration)
|
||||||
|
print (":")
|
||||||
|
print (l_headers.item_for_iteration)
|
||||||
|
l_headers.forth
|
||||||
|
io.put_new_line
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Show body
|
||||||
|
print (r.body)
|
||||||
|
io.put_new_line
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
create_order (sess: HTTP_CLIENT_SESSION)
|
read_order ( sess: HTTP_CLIENT_SESSION; uri : detachable READABLE_STRING_8) : HTTP_CLIENT_RESPONSE
|
||||||
|
local
|
||||||
|
l_headers: HASH_TABLE [READABLE_STRING_8,READABLE_STRING_8]
|
||||||
|
do
|
||||||
|
create Result.make
|
||||||
|
if attached uri as l_uri then
|
||||||
|
sess.set_base_url (l_uri)
|
||||||
|
Result := sess.get ("", Void)
|
||||||
|
if attached Result as r then
|
||||||
|
-- Show headers
|
||||||
|
l_headers := r.headers
|
||||||
|
from
|
||||||
|
l_headers.start
|
||||||
|
until
|
||||||
|
l_headers.after
|
||||||
|
loop
|
||||||
|
print (l_headers.key_for_iteration)
|
||||||
|
print (":")
|
||||||
|
print (l_headers.item_for_iteration)
|
||||||
|
l_headers.forth
|
||||||
|
io.put_new_line
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Show body
|
||||||
|
print (r.body)
|
||||||
|
io.put_new_line
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
create_order (sess: HTTP_CLIENT_SESSION) : HTTP_CLIENT_RESPONSE
|
||||||
local
|
local
|
||||||
s: READABLE_STRING_8
|
s: READABLE_STRING_8
|
||||||
j: JSON_PARSER
|
j: JSON_PARSER
|
||||||
id: detachable STRING
|
id: detachable STRING
|
||||||
context : HTTP_CLIENT_REQUEST_CONTEXT
|
context : HTTP_CLIENT_REQUEST_CONTEXT
|
||||||
|
l_headers: HASH_TABLE [READABLE_STRING_8,READABLE_STRING_8]
|
||||||
do
|
do
|
||||||
s := "[
|
s := "[
|
||||||
{
|
{
|
||||||
@@ -53,9 +130,24 @@ feature {NONE} -- Initialization
|
|||||||
|
|
||||||
create context.make
|
create context.make
|
||||||
context.headers.put ("application/json", "Content-Type")
|
context.headers.put ("application/json", "Content-Type")
|
||||||
if attached sess.post ("/order", context, s) as r then
|
Result := sess.post ("/order", context, s)
|
||||||
print (r.raw_header)
|
if attached Result as r then
|
||||||
|
-- Show the Headers
|
||||||
|
l_headers := r.headers
|
||||||
|
from
|
||||||
|
l_headers.start
|
||||||
|
until
|
||||||
|
l_headers.after
|
||||||
|
loop
|
||||||
|
print (l_headers.key_for_iteration)
|
||||||
|
print (":")
|
||||||
|
print (l_headers.item_for_iteration)
|
||||||
|
l_headers.forth
|
||||||
io.put_new_line
|
io.put_new_line
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-- Show the Response body
|
||||||
if attached r.body as m then
|
if attached r.body as m then
|
||||||
create j.make_parser (m)
|
create j.make_parser (m)
|
||||||
if j.is_parsed and attached j.parse_object as j_o then
|
if j.is_parsed and attached j.parse_object as j_o then
|
||||||
@@ -67,7 +159,6 @@ feature {NONE} -- Initialization
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ feature -- Conversion
|
|||||||
s_location ?= json.object (j.item (location_key), Void)
|
s_location ?= json.object (j.item (location_key), Void)
|
||||||
s_status ?= json.object (j.item (status_key), Void)
|
s_status ?= json.object (j.item (status_key), Void)
|
||||||
|
|
||||||
create o.make (s_id, s_location, s_status)
|
create o.make ("", s_location, s_status)
|
||||||
|
|
||||||
if attached {JSON_ARRAY} j.item (items_key) as l_val then
|
if attached {JSON_ARRAY} j.item (items_key) as l_val then
|
||||||
l_array := l_val.array_representation
|
l_array := l_val.array_representation
|
||||||
@@ -87,7 +87,7 @@ feature -- Conversion
|
|||||||
jv: JSON_OBJECT
|
jv: JSON_OBJECT
|
||||||
do
|
do
|
||||||
create Result.make
|
create Result.make
|
||||||
Result.put (json.value (o.id), id_key)
|
-- Result.put (json.value (o.id), id_key)
|
||||||
Result.put (json.value (o.location), location_key)
|
Result.put (json.value (o.location), location_key)
|
||||||
Result.put (json.value (o.status), status_key)
|
Result.put (json.value (o.status), status_key)
|
||||||
from
|
from
|
||||||
|
|||||||
@@ -66,9 +66,9 @@ feature -- HTTP Methods
|
|||||||
local
|
local
|
||||||
etag_util : ETAG_UTILS
|
etag_util : ETAG_UTILS
|
||||||
do
|
do
|
||||||
if attached req.meta_variable ("HTTP_IF_NONE_MATCH") as if_none_match then
|
if attached req.meta_string_variable ("HTTP_IF_NONE_MATCH") as if_none_match then
|
||||||
create etag_util
|
create etag_util
|
||||||
if if_none_match.as_string.same_string (etag_util.md5_digest (l_order.out).as_string_32) then
|
if if_none_match.same_string (etag_util.md5_digest (l_order.out).as_string_32) then
|
||||||
Result := True
|
Result := True
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -108,13 +108,17 @@ feature -- HTTP Methods
|
|||||||
-- If the request is a Conditional PUT, and it does not mat we response
|
-- If the request is a Conditional PUT, and it does not mat we response
|
||||||
-- 415, precondition failed.
|
-- 415, precondition failed.
|
||||||
local
|
local
|
||||||
l_post: STRING
|
l_put: STRING
|
||||||
l_order : detachable ORDER
|
l_order : detachable ORDER
|
||||||
|
id : STRING
|
||||||
do
|
do
|
||||||
|
if attached req.orig_path_info as orig_path then
|
||||||
|
id := get_order_id_from_path (orig_path)
|
||||||
req.input.read_string (req.content_length_value.as_integer_32)
|
req.input.read_string (req.content_length_value.as_integer_32)
|
||||||
l_post := req.input.last_string
|
l_put := req.input.last_string
|
||||||
l_order := extract_order_request(l_post)
|
l_order := extract_order_request(l_put)
|
||||||
if l_order /= Void and then db_access.orders.has_key (l_order.id) then
|
if l_order /= Void and then db_access.orders.has_key (id) then
|
||||||
|
l_order.set_id (id)
|
||||||
if is_valid_to_update(l_order) then
|
if is_valid_to_update(l_order) then
|
||||||
if is_conditional_put (req, l_order) then
|
if is_conditional_put (req, l_order) then
|
||||||
update_order( l_order)
|
update_order( l_order)
|
||||||
@@ -124,10 +128,11 @@ feature -- HTTP Methods
|
|||||||
end
|
end
|
||||||
else
|
else
|
||||||
--| FIXME: Here we need to define the Allow methods
|
--| FIXME: Here we need to define the Allow methods
|
||||||
handle_resource_conflict_response (l_post +"%N There is conflict while trying to update the order, the order could not be update in the current state", ctx, req, res)
|
handle_resource_conflict_response (l_put +"%N There is conflict while trying to update the order, the order could not be update in the current state", ctx, req, res)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
handle_bad_request_response (l_post +"%N is not a valid ORDER, maybe the order does not exist in the system", ctx, req, res)
|
handle_bad_request_response (l_put +"%N is not a valid ORDER, maybe the order does not exist in the system", ctx, req, res)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -138,9 +143,9 @@ feature -- HTTP Methods
|
|||||||
etag_util : ETAG_UTILS
|
etag_util : ETAG_UTILS
|
||||||
do
|
do
|
||||||
if attached retrieve_order (order.id) as l_order then
|
if attached retrieve_order (order.id) as l_order then
|
||||||
if attached req.meta_variable ("HTTP_IF_MATCH") as if_match then
|
if attached req.meta_string_variable ("HTTP_IF_MATCH") as if_match then
|
||||||
create etag_util
|
create etag_util
|
||||||
if if_match.as_string.same_string (etag_util.md5_digest (l_order.out).as_string_32) then
|
if if_match.same_string (etag_util.md5_digest (l_order.out).as_string_32) then
|
||||||
Result := True
|
Result := True
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ feature -- Access
|
|||||||
raw_header: READABLE_STRING_8
|
raw_header: READABLE_STRING_8
|
||||||
-- Raw http header of the response.
|
-- Raw http header of the response.
|
||||||
|
|
||||||
headers: LIST [TUPLE [key: READABLE_STRING_8; value: READABLE_STRING_8]]
|
headers: HASH_TABLE[READABLE_STRING_8,READABLE_STRING_8]
|
||||||
-- Computed table of http headers of the response.
|
-- Computed table of http headers of the response.
|
||||||
local
|
local
|
||||||
tb: like internal_headers
|
tb: like internal_headers
|
||||||
@@ -86,7 +86,7 @@ feature -- Access
|
|||||||
from until c <= n and not h[c].is_space loop
|
from until c <= n and not h[c].is_space loop
|
||||||
c := c + 1
|
c := c + 1
|
||||||
end
|
end
|
||||||
tb.force ([k, h.substring (c, l_end)])
|
tb.put (h.substring (c, l_end), k)
|
||||||
else
|
else
|
||||||
check header_has_colon: c > 0 end
|
check header_has_colon: c > 0 end
|
||||||
end
|
end
|
||||||
@@ -125,7 +125,9 @@ feature -- Change
|
|||||||
|
|
||||||
feature {NONE} -- Implementation
|
feature {NONE} -- Implementation
|
||||||
|
|
||||||
internal_headers: detachable ARRAYED_LIST [TUPLE [key: READABLE_STRING_8; value: READABLE_STRING_8]]
|
-- internal_headers: detachable ARRAYED_LIST [TUPLE [key: READABLE_STRING_8; value: READABLE_STRING_8]]
|
||||||
-- Internal cached value for the headers
|
-- Internal cached value for the headers
|
||||||
|
|
||||||
|
internal_headers: detachable HASH_TABLE[READABLE_STRING_8,READABLE_STRING_8]
|
||||||
|
-- Internal cached value for the headers
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ feature {NONE} -- Initialization
|
|||||||
across
|
across
|
||||||
l_vars as c
|
l_vars as c
|
||||||
loop
|
loop
|
||||||
meta_variables_table.force (new_string_value (c.key, c.item), c.item)
|
meta_variables_table.force (new_string_value (c.key, c.item), c.key)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
create meta_variables_table.make (0)
|
create meta_variables_table.make (0)
|
||||||
|
|||||||
Reference in New Issue
Block a user