Skip to content

Commit 7f27e9f

Browse files
committed
slack: factor out token-based api calls
1 parent 1959912 commit 7f27e9f

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

lib/api_remote.ml

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,23 @@ module Slack : Api.Slack = struct
9191

9292
let bearer_token_header access_token = sprintf "Authorization: Bearer %s" (Uri.pct_encode access_token)
9393

94+
let request_token_auth ~name ?headers ?body ~ctx meth path read =
95+
log#info "%s: starting request" name;
96+
let secrets = Context.get_secrets_exn ctx in
97+
match secrets.slack_access_token with
98+
| None -> Lwt.return @@ fmt_error "%s: failed to retrieve Slack access token" name
99+
| Some access_token ->
100+
let headers = bearer_token_header access_token :: Option.value ~default:[] headers in
101+
let url = sprintf "https://slack.com/api/%s" path in
102+
( match%lwt slack_api_request ?body ~headers meth url read with
103+
| Ok res -> Lwt.return @@ Ok res
104+
| Error e -> Lwt.return @@ fmt_error "%s: failure : %s" name e
105+
)
106+
107+
let read_unit s l =
108+
(* must read whole response to update lexer state *)
109+
ignore (Slack_j.read_ok_res s l)
110+
94111
(** [send_notification ctx msg] notifies [msg.channel] with the payload [msg];
95112
uses web API with access token if available, or with webhook otherwise *)
96113
let send_notification ~(ctx : Context.t) ~(msg : Slack_t.post_message_req) =
@@ -123,31 +140,11 @@ module Slack : Api.Slack = struct
123140
end
124141

125142
let send_chat_unfurl ~(ctx : Context.t) req =
126-
log#info "unfurling Slack links";
127-
let secrets = Context.get_secrets_exn ctx in
128-
match secrets.slack_access_token with
129-
| None -> Lwt.return @@ fmt_error "failed to retrieve Slack access token"
130-
| Some access_token ->
131-
let data = Slack_j.string_of_chat_unfurl_req req in
132-
log#info "%s" data;
133-
let url = "https://slack.com/api/chat.unfurl" in
134-
let headers = [ bearer_token_header access_token ] in
135-
let body = `Raw ("application/json", data) in
136-
( match%lwt slack_api_request ~body ~headers `POST url Slack_j.read_chat_unfurl_res with
137-
| Ok _res -> Lwt.return @@ Ok ()
138-
| Error e -> Lwt.return @@ fmt_error "%s\nfailed to unfurl Slack links" e
139-
)
143+
let data = Slack_j.string_of_chat_unfurl_req req in
144+
request_token_auth ~name:"unfurl slack links"
145+
~body:(`Raw ("application/json", data))
146+
~ctx `POST "chat.unfurl" read_unit
140147

141148
let send_auth_test ~(ctx : Context.t) () =
142-
log#info "retrieving bot information";
143-
let secrets = Context.get_secrets_exn ctx in
144-
match secrets.slack_access_token with
145-
| None -> Lwt.return @@ Error "failed to retrieve Slack access token"
146-
| Some access_token ->
147-
let url = "https://slack.com/api/auth.test" in
148-
let headers = [ bearer_token_header access_token ] in
149-
( match%lwt slack_api_request ~headers `GET url Slack_j.read_auth_test_res with
150-
| Ok res -> Lwt.return @@ Ok res
151-
| Error e -> Lwt.return @@ fmt_error "%s\nfailed to retrieve Slack auth info" e
152-
)
149+
request_token_auth ~name:"retrieve bot information" ~ctx `POST "auth.test" Slack_j.read_auth_test_res
153150
end

lib/slack.atd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ type chat_unfurl_req = {
112112
unfurls: unfurl map_as_object;
113113
}
114114

115-
type chat_unfurl_res = {
115+
type ok_res = {
116116
ok: bool;
117117
}
118118

0 commit comments

Comments
 (0)