Skip to content

Commit 2bcad1d

Browse files
committed
slack: use atd adapter for all web api requests
1 parent 7c4ebdb commit 2bcad1d

File tree

4 files changed

+25
-31
lines changed

4 files changed

+25
-31
lines changed

lib/api.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ module type Github = sig
1313
end
1414

1515
module type Slack = sig
16-
val send_notification : ctx:Context.t -> msg:post_message_req -> (unit, string) Result.t Lwt.t
16+
val send_notification : ctx:Context.t -> msg:post_message_req -> unit slack_response Lwt.t
1717

18-
val send_chat_unfurl : ctx:Context.t -> chat_unfurl_req -> (unit, string) Result.t Lwt.t
18+
val send_chat_unfurl : ctx:Context.t -> chat_unfurl_req -> unit slack_response Lwt.t
1919

2020
val send_auth_test : ctx:Context.t -> unit -> auth_test_res slack_response Lwt.t
2121
end

lib/api_remote.ml

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ end
6767
module Slack : Api.Slack = struct
6868
let log = Log.from "slack"
6969

70-
let slack_http_request ?headers ?body meth url read =
70+
let query_error_msg url e = sprintf "error while querying %s: %s" url e
71+
72+
let slack_api_request ?headers ?body meth url read =
7173
match%lwt http_request ?headers ?body meth url with
72-
| Error e -> Lwt.return @@ Error (sprintf "error while querying %s: %s" url e)
74+
| Error e -> Lwt.return @@ Error (query_error_msg url e)
7375
| Ok s -> Lwt.return @@ Slack_j.slack_response_of_string read s
7476

7577
let bearer_token_header access_token = sprintf "Authorization: Bearer %s" (Uri.pct_encode access_token)
@@ -79,7 +81,6 @@ module Slack : Api.Slack = struct
7981
let send_notification ~(ctx : Context.t) ~(msg : Slack_t.post_message_req) =
8082
log#info "sending to %s" msg.channel;
8183
let build_error e = fmt_error "%s\nfailed to send Slack notification" e in
82-
let build_query_error url e = build_error @@ sprintf "error while querying %s: %s" url e in
8384
let secrets = Context.get_secrets_exn ctx in
8485
let headers, url, webhook_mode =
8586
match Context.hook_of_channel ctx msg.channel with
@@ -95,17 +96,16 @@ module Slack : Api.Slack = struct
9596
let data = Slack_j.string_of_post_message_req msg in
9697
let body = `Raw ("application/json", data) in
9798
log#info "data: %s" data;
98-
( match%lwt http_request ~body ~headers `POST url with
99-
(* error detection in response: slack uses status codes for webhooks versus a 200 code w/ `error` field for web api *)
100-
| Ok s ->
101-
if webhook_mode then Lwt.return @@ Ok ()
102-
else (
103-
let res = Slack_j.post_message_res_of_string s in
104-
if res.ok then Lwt.return @@ Ok ()
105-
else Lwt.return @@ build_query_error url (Option.value ~default:"an unknown error occurred" res.error)
106-
)
107-
| Error e -> Lwt.return @@ build_query_error url e
108-
)
99+
if webhook_mode then begin
100+
match%lwt http_request ~body ~headers `POST url with
101+
| Ok _res -> Lwt.return @@ Ok ()
102+
| Error e -> Lwt.return @@ build_error (query_error_msg url e)
103+
end
104+
else begin
105+
match%lwt slack_api_request ~body ~headers `POST url Slack_j.read_post_message_res with
106+
| Ok _res -> Lwt.return @@ Ok ()
107+
| Error e -> Lwt.return @@ build_error e
108+
end
109109

110110
let send_chat_unfurl ~(ctx : Context.t) req =
111111
log#info "unfurling Slack links";
@@ -118,15 +118,9 @@ module Slack : Api.Slack = struct
118118
let url = "https://slack.com/api/chat.unfurl" in
119119
let headers = [ bearer_token_header access_token ] in
120120
let body = `Raw ("application/json", data) in
121-
( match%lwt http_request ~body ~headers `POST url with
122-
| Ok s ->
123-
let res = Slack_j.chat_unfurl_res_of_string s in
124-
if res.ok then Lwt.return @@ Ok ()
125-
else (
126-
let msg = Option.value ~default:"an unknown error occurred" res.error in
127-
Lwt.return @@ fmt_error "%s\nfailed to unfurl Slack links" msg
128-
)
129-
| Error e -> Lwt.return @@ fmt_error "error while querying %s: %s\nfailed to unfurl Slack links" url e
121+
( match%lwt slack_api_request ~body ~headers `POST url Slack_j.read_chat_unfurl_res with
122+
| Ok _res -> Lwt.return @@ Ok ()
123+
| Error e -> Lwt.return @@ fmt_error "%s\nfailed to unfurl Slack links" e
130124
)
131125

132126
let send_auth_test ~(ctx : Context.t) () =
@@ -137,8 +131,8 @@ module Slack : Api.Slack = struct
137131
| Some access_token ->
138132
let url = "https://slack.com/api/auth.test" in
139133
let headers = [ bearer_token_header access_token ] in
140-
( match%lwt slack_http_request ~headers `GET url Slack_j.read_auth_test_res with
134+
( match%lwt slack_api_request ~headers `GET url Slack_j.read_auth_test_res with
141135
| Ok res -> Lwt.return @@ Ok res
142-
| Error e -> Lwt.return @@ Error (sprintf "%s\nfailed to retrieve Slack auth info" e)
136+
| Error e -> Lwt.return @@ fmt_error "%s\nfailed to retrieve Slack auth info" e
143137
)
144138
end

lib/atd_adapters.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ module Branch_filters_adapter = List_or_default_field.Make (struct
4747
let default_value = `List []
4848
end)
4949

50+
(** Error detection in Slack API response. The web API communicates errors using
51+
an `error` field rather than status codes. Note, on the other hand, that
52+
webhooks do use status codes to communicate errors. *)
5053
module Slack_response_adapter : Atdgen_runtime.Json_adapter.S = struct
5154
let normalize (x : Yojson.Safe.t) =
5255
match x with

lib/slack.atd

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ type post_message_req = {
6464
}
6565

6666
type post_message_res = {
67-
ok: bool;
68-
?channel: string nullable;
69-
?error: string nullable;
67+
channel: string;
7068
}
7169

7270
type link_shared_link = {
@@ -116,7 +114,6 @@ type chat_unfurl_req = {
116114

117115
type chat_unfurl_res = {
118116
ok: bool;
119-
?error: string option;
120117
}
121118

122119
type auth_test_res = {

0 commit comments

Comments
 (0)