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
|
||||
h: LIBCURL_HTTP_CLIENT
|
||||
sess: HTTP_CLIENT_SESSION
|
||||
resp : HTTP_CLIENT_RESPONSE
|
||||
l_location : detachable READABLE_STRING_8
|
||||
body : STRING
|
||||
do
|
||||
create h.make
|
||||
sess := h.new_session ("http://127.0.0.1:8080")
|
||||
-- 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
|
||||
-- print (r.body)
|
||||
-- io.put_new_line
|
||||
-- end
|
||||
-- Read the Order
|
||||
print ("%N Read Order %N")
|
||||
l_location := resp.headers.at ("Location")
|
||||
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
|
||||
|
||||
|
||||
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
|
||||
s: READABLE_STRING_8
|
||||
j: JSON_PARSER
|
||||
id: detachable STRING
|
||||
context : HTTP_CLIENT_REQUEST_CONTEXT
|
||||
l_headers: HASH_TABLE [READABLE_STRING_8,READABLE_STRING_8]
|
||||
do
|
||||
s := "[
|
||||
{
|
||||
@@ -53,9 +130,24 @@ feature {NONE} -- Initialization
|
||||
|
||||
create context.make
|
||||
context.headers.put ("application/json", "Content-Type")
|
||||
if attached sess.post ("/order", context, s) as r then
|
||||
print (r.raw_header)
|
||||
io.put_new_line
|
||||
Result := sess.post ("/order", context, s)
|
||||
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
|
||||
end
|
||||
|
||||
|
||||
-- Show the Response body
|
||||
if attached r.body as m then
|
||||
create j.make_parser (m)
|
||||
if j.is_parsed and attached j.parse_object as j_o then
|
||||
@@ -67,7 +159,6 @@ feature {NONE} -- Initialization
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ feature -- Conversion
|
||||
s_location ?= json.object (j.item (location_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
|
||||
l_array := l_val.array_representation
|
||||
@@ -87,7 +87,7 @@ feature -- Conversion
|
||||
jv: JSON_OBJECT
|
||||
do
|
||||
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.status), status_key)
|
||||
from
|
||||
|
||||
@@ -66,9 +66,9 @@ feature -- HTTP Methods
|
||||
local
|
||||
etag_util : ETAG_UTILS
|
||||
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
|
||||
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
|
||||
end
|
||||
end
|
||||
@@ -108,26 +108,31 @@ feature -- HTTP Methods
|
||||
-- If the request is a Conditional PUT, and it does not mat we response
|
||||
-- 415, precondition failed.
|
||||
local
|
||||
l_post: STRING
|
||||
l_put: STRING
|
||||
l_order : detachable ORDER
|
||||
id : STRING
|
||||
do
|
||||
req.input.read_string (req.content_length_value.as_integer_32)
|
||||
l_post := req.input.last_string
|
||||
l_order := extract_order_request(l_post)
|
||||
if l_order /= Void and then db_access.orders.has_key (l_order.id) then
|
||||
if is_valid_to_update(l_order) then
|
||||
if is_conditional_put (req, l_order) then
|
||||
update_order( l_order)
|
||||
compute_response_put (ctx, req, res, l_order)
|
||||
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)
|
||||
l_put := req.input.last_string
|
||||
l_order := extract_order_request(l_put)
|
||||
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_conditional_put (req, l_order) then
|
||||
update_order( l_order)
|
||||
compute_response_put (ctx, req, res, l_order)
|
||||
else
|
||||
handle_precondition_fail_response ("", ctx, req, res)
|
||||
end
|
||||
else
|
||||
handle_precondition_fail_response ("", ctx, req, res)
|
||||
--| FIXME: Here we need to define the Allow methods
|
||||
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
|
||||
else
|
||||
--| 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_bad_request_response (l_put +"%N is not a valid ORDER, maybe the order does not exist in the system", ctx, req, res)
|
||||
end
|
||||
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)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -138,9 +143,9 @@ feature -- HTTP Methods
|
||||
etag_util : ETAG_UTILS
|
||||
do
|
||||
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
|
||||
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
|
||||
end
|
||||
else
|
||||
|
||||
@@ -41,7 +41,7 @@ feature -- Access
|
||||
raw_header: READABLE_STRING_8
|
||||
-- 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.
|
||||
local
|
||||
tb: like internal_headers
|
||||
@@ -86,7 +86,7 @@ feature -- Access
|
||||
from until c <= n and not h[c].is_space loop
|
||||
c := c + 1
|
||||
end
|
||||
tb.force ([k, h.substring (c, l_end)])
|
||||
tb.put (h.substring (c, l_end), k)
|
||||
else
|
||||
check header_has_colon: c > 0 end
|
||||
end
|
||||
@@ -125,7 +125,9 @@ feature -- Change
|
||||
|
||||
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_headers: detachable HASH_TABLE[READABLE_STRING_8,READABLE_STRING_8]
|
||||
-- Internal cached value for the headers
|
||||
end
|
||||
|
||||
@@ -33,7 +33,7 @@ feature {NONE} -- Initialization
|
||||
across
|
||||
l_vars as c
|
||||
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
|
||||
else
|
||||
create meta_variables_table.make (0)
|
||||
|
||||
Reference in New Issue
Block a user