Skip to content

Commit 1e94b2f

Browse files
committed
generalize shared http request code
1 parent 5618a48 commit 1e94b2f

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

lib/api_remote.ml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ module Github : Api.Github = struct
1717
Option.value_map token ~default:headers ~f:(fun v -> sprintf "Authorization: token %s" v :: headers)
1818

1919
let get_config ~(ctx : Context.t) ~repo =
20+
let secrets = Context.get_secrets_exn ctx in
2021
let url = contents_url ~repo ~path:ctx.config_filename in
21-
let headers = build_headers ?token:ctx.gh_token () in
22-
match%lwt http_get ~headers url with
22+
let headers = build_headers ?token:secrets.gh_token () in
23+
match%lwt http_request ~headers `GET url with
2324
| Error e ->
2425
log#error "error while querying %s: %s" url e;
2526
Lwt.return @@ fmt_error "failed to get config from file %s" url
@@ -41,9 +42,10 @@ module Github : Api.Github = struct
4142
)
4243

4344
let get_api_commit ~(ctx : Context.t) ~repo ~sha =
45+
let secrets = Context.get_secrets_exn ctx in
4446
let url = commits_url ~repo ~sha in
45-
let headers = build_headers ?token:ctx.gh_token () in
46-
match%lwt http_get ~headers url with
47+
let headers = build_headers ?token:secrets.gh_token () in
48+
match%lwt http_request ~headers `GET url with
4749
| Ok res -> Lwt.return @@ Ok (Github_j.api_commit_of_string res)
4850
| Error e ->
4951
log#error "error while querying %s: %s" url e;
@@ -55,8 +57,9 @@ module Slack : Api.Slack = struct
5557

5658
let send_notification ~chan ~msg ~url =
5759
let data = Slack_j.string_of_webhook_notification msg in
60+
let body = `Raw ("application/json", data) in
5861
log#info "sending to %s : %s" chan data;
59-
match%lwt http_post ~path:url ~data with
62+
match%lwt http_request ~body `POST url with
6063
| Ok _ -> Lwt.return @@ Ok ()
6164
| Error e ->
6265
log#error "error while querying %s: %s" url e;

lib/common.ml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,18 @@ end
3737
let decode_string_pad s =
3838
String.rstrip ~drop:(List.mem [ '='; ' '; '\n'; '\r'; '\t' ] ~equal:Char.equal) s |> Base64.decode_string
3939

40-
let http_get ?headers path =
41-
match%lwt Web.http_request_lwt ~ua:"monorobot" ~verbose:true ?headers `GET path with
40+
let http_request ?headers ?body meth path =
41+
match%lwt Web.http_request_lwt ~ua:"monorobot" ~verbose:true ?headers ?body meth path with
4242
| `Ok s -> Lwt.return @@ Ok s
4343
| `Error e -> Lwt.return @@ Error e
4444

45-
let http_post ~path ~data =
46-
let body = `Raw ("application/json", data) in
47-
match%lwt Web.http_request_lwt ~verbose:true ~body `POST path with
48-
| `Ok res -> Lwt.return @@ Ok res
49-
| `Error e -> Lwt.return @@ Error e
50-
5145
let get_local_file path =
5246
try%lwt
5347
let%lwt data = Lwt_io.with_file ~mode:Lwt_io.input path (fun ic -> Lwt_io.read ic) in
5448
Lwt.return @@ Ok data
5549
with exn -> Lwt.return @@ Error (Exn.str exn)
5650

57-
let write_to_local_file ~path ~data =
51+
let write_to_local_file ~data path =
5852
try%lwt
5953
let%lwt () =
6054
Lwt_io.with_file ~flags:[ O_CREAT; O_WRONLY; O_TRUNC ] ~mode:Lwt_io.output path (fun oc -> Lwt_io.write oc data)

0 commit comments

Comments
 (0)