-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Eio conversion of reset, status, history
- Loading branch information
1 parent
e78f9bf
commit 7e165c7
Showing
16 changed files
with
179 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,25 @@ | ||
(include_subdirs no) | ||
|
||
(executables | ||
(names test_http_api_eval test_http_api_instance test_http_api_verify) | ||
(names | ||
test_http_api_eval | ||
test_http_api_instance | ||
test_http_api_verify | ||
test_eio) | ||
(public_names | ||
test_http_api_eval | ||
test_http_api_instance | ||
test_http_api_verify) | ||
(libraries containers imandra_http_api_client eio eio_main lwt_eio) | ||
(modules test_http_api_eval test_http_api_instance test_http_api_verify)) | ||
test_http_api_verify | ||
test_eio) | ||
(libraries | ||
containers | ||
cohttp-eio | ||
eio | ||
imandra_http_api_client | ||
eio_main | ||
lwt_eio) | ||
(modules | ||
test_http_api_eval | ||
test_http_api_instance | ||
test_http_api_verify | ||
test_eio)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Log = (val Logs.src_log (Logs.Src.create "imandra-http-api-local")) | ||
|
||
let () = | ||
let open Imandra_http_api_client in | ||
Logs.set_reporter (Logs.format_reporter ()); | ||
Logs.set_level (Some Logs.Debug); | ||
let config = Main_eio.Config.make ~base_uri:"http://127.0.0.1:3000" () in | ||
|
||
let response http ~sw = | ||
Log.debug (fun k -> k "Sending query to server..."); | ||
let result = Main_eio.reset config http ~sw in | ||
match result with | ||
| Ok st -> | ||
Log.app (fun k -> | ||
k "Got ok response: %a" | ||
CCFormat.(some Api.Response.pp_capture) | ||
st.capture) | ||
| Error err -> | ||
(match err with | ||
| `Error_decoding_response err -> | ||
Log.err (fun k -> | ||
k "Decoding error: %a@." Decoders_yojson.Basic.Decode.pp_error err) | ||
| `Error_response (code, _err) -> | ||
Log.err (fun k -> | ||
k "Error response: Code = %s" (Cohttp.Code.string_of_status code))) | ||
in | ||
|
||
Eio_main.run @@ fun env -> | ||
let http = Cohttp_eio.Client.make ~https:None env#net in | ||
Eio.Switch.run @@ fun sw -> response http ~sw |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
module Config = struct | ||
type t = { | ||
base_uri: Uri.t; | ||
auth_token: string option; | ||
} | ||
|
||
let make ?auth_token ~base_uri () = | ||
{ base_uri = Uri.of_string base_uri; auth_token } | ||
end | ||
|
||
module E = Api.Encoders (Decoders_yojson.Basic.Encode) | ||
module D = Api.Decoders (Decoders_yojson.Basic.Decode) | ||
|
||
type error = | ||
[ `Error_response of | ||
Cohttp.Code.status_code * Api.Response.error Api.Response.with_capture | ||
| `Error_decoding_response of Decoders_yojson.Basic.Decode.error | ||
] | ||
|
||
let build_uri (c : Config.t) path = Uri.with_path c.base_uri path | ||
|
||
let default_headers (c : Config.t) = | ||
let other_headers = [] in | ||
let auth_header = | ||
match c.auth_token with | ||
| None -> [] | ||
| Some t -> [ "Authorization", Format.asprintf "Bearer %s" t ] | ||
in | ||
other_headers @ auth_header | ||
|
||
let make_body enc x : Cohttp_eio.Body.t = | ||
Decoders_yojson.Basic.Encode.encode_string enc x |> Cohttp_eio.Body.of_string | ||
|
||
let read_response dec (s : Cohttp_eio.Body.t) = | ||
match | ||
Decoders_yojson.Basic.Decode.decode_string | ||
(D.Response.with_capture dec) | ||
(s |> Eio.Flow.read_all) | ||
with | ||
| Ok err -> Ok err | ||
| Error e -> Error (`Error_decoding_response e) | ||
|
||
let read_error (s : Cohttp_eio.Body.t) = | ||
match | ||
Decoders_yojson.Basic.Decode.decode_string | ||
D.Response.(with_capture error) | ||
(s |> Eio.Flow.read_all) | ||
with | ||
| Ok err -> Ok err | ||
| Error e -> Error (`Error_decoding_response e) | ||
|
||
let read (dec : 'a Decoders_yojson.Basic.Decode.decoder) | ||
((resp, body) : Http.Response.t * Cohttp_eio.Body.t) = | ||
let status = Cohttp.Response.status resp in | ||
if status = `OK then | ||
read_response dec body |> CCResult.flat_map (fun ok -> Ok ok) | ||
else | ||
read_error body | ||
|> CCResult.flat_map (fun err -> Error (`Error_response (status, err))) | ||
|
||
(* let net = | ||
let open Eio.Std in | ||
let net = Eio_mock.Net.make "mocknet" in | ||
Eio_mock.Net.on_getaddrinfo net | ||
[ `Return [ `Tcp (Eio.Net.Ipaddr.V4.loopback, 443) ] ]; | ||
let conn = Eio_mock.Flow.make "connection" in | ||
Eio_mock.Net.on_connect net [ `Return conn ]; | ||
net *) | ||
|
||
(* let eval (c : Config.t) (req : Api.Request.eval_req_src) ~sw cl = | ||
let uri = build_uri c "/eval/by-src" in | ||
let headers = default_headers c |> Cohttp.Header.of_list in | ||
let body = make_body E.Request.eval_req_src req in | ||
(* let res = Cohttp_eio.Client.call ~headers ~body cl `POST uri ~sw in *) | ||
let res = Cohttp_eio.Client.get ~headers cl ~sw uri in | ||
read D.Response.eval_result res *) | ||
let get_history (c : Config.t) cl ~sw = | ||
let uri = build_uri c "/history" in | ||
let headers = default_headers c |> Cohttp.Header.of_list in | ||
let res = Cohttp_eio.Client.get cl ~sw uri ~headers in | ||
read Decoders_yojson.Basic.Decode.string res | ||
|
||
let get_status (c : Config.t) cl ~sw = | ||
let uri = build_uri c "/status" in | ||
let headers = default_headers c |> Cohttp.Header.of_list in | ||
let res = Cohttp_eio.Client.get cl ~sw uri ~headers in | ||
read Decoders_yojson.Basic.Decode.string res | ||
|
||
let reset (c : Config.t) cl ~sw = | ||
let uri = build_uri c "/reset" in | ||
let headers = default_headers c |> Cohttp.Header.of_list in | ||
let res = Cohttp_eio.Client.call cl ~sw `POST uri ~headers in | ||
read D.Response.reset_result res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters