Add Json API to some endpoints #5
Loading…
Reference in a new issue
No description provided.
Delete branch "json_responses"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This PR uses the
Accept: application/json
header to return json responses for some endpoints:/compare/:build_left/:build_right
/job/:job/build/latest/**
/job/:job/build/latest
@ -336,0 +337,4 @@
"uuid", `String (Uuidm.to_string build);
] |> Yojson.Basic.to_string
in
Dream.json ~status:`OK json_response |> Lwt_result.ok
Tbh here I think I would just keep the redirect and rely on
job_build
returning a json object that includes the uuid.The client would then need to follow redirects, of course.
@ -411,0 +413,4 @@
"job_name", `String job_name;
"uuid", `String (Uuidm.to_string build.uuid);
"platform", `String build.platform;
"build_start_time", `String (Ptime.to_rfc3339 build.start);
For consistency maybe drop the
build_
prefix? Else I guess we should writebuild_uuid
as well? :-)@ -541,0 +569,4 @@
} in
match id_opt with
| None -> Lwt.return_ok default_file
| Some id -> Model.build_artifact_by_id id conn
I don't like that we make up a main binary if it doesn't exist. Can we use an option? For example, "0" is not a valid sha256 checksum,
-1
is not a valid size...Also, I think this function is only used in order to get the binary size? If so, then we can return that directly (as an option)
@ -584,0 +636,4 @@
"platform", `String build_right.platform;
"build_start_time", `String (Ptime.to_rfc3339 build_right.start);
"build_finish_time", `String (Ptime.to_rfc3339 build_right.finish);
"build_size", `Int (build_right_file.size)
Then here we could make
build_size
optional or have it be`Null
if the file isNone
.I pushed a few commits that address my own comments.
I also tweaked a bit the json fields. In particular, I dropped the
build_
prefix of the fields and tried to make the field names and members more consistent between the endpoints. The compare_builds json output now also has a"main_binary": true
field even if it's somewhat redundant given the main binary size can tell you that.@PixieDust: Please let me know if you agree and are happy with the changes
Thank you @reynir . These changes are great :)
@ -77,1 +83,3 @@
| Error (text, status) -> Dream.respond ~status text
| Error (text, status) ->
if is_accept_json req then
let json_response = Yojson.Basic.to_string (`Assoc [ "error", `String text ]) in
This could certainly be made into a much more complicated object. I don't have strong opinions on that.
There are "standards" out there suggesting you use an "errors" field with an array of error objects: https://jsonapi.org/format/#error-objects
Yes that is probably the right way to go
I merged it as is. We can revisit the errors later.