1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
module type Client = module type of Piaf.Client
type t =
| Basic of {
username: string;
password: string;
}
| ApiToken of string
let (meth : t) : (string, string) result =
let create_basic_credentials ~username ~password =
match Base64.encode (username ^ ":" ^ password) with
| Ok creds ->
Ok ("Basic " ^ creds)
| Error (`Msg e) ->
Error e
in
match meth with
| ApiToken token ->
create_basic_credentials ~username:token ~password:"api_token"
| Basic {username; password} ->
create_basic_credentials ~username ~password
module Client (Authentication : sig
val auth : t
end) : Client with type t = Piaf.Client.t = struct
include Piaf.Client
let = create_header Authentication.auth |> CCResult.get_or_failwith
let add_authorization = function
| None ->
Some ["Authorization", header]
| Some ->
Some (CCList.Assoc.set ~eq:CCString.equal "Authorization" header headers)
let request client ? =
let = add_authorization headers in
request client ?headers
let head client ? =
let = add_authorization headers in
head client ?headers
let get client ? =
let = add_authorization headers in
get client ?headers
let post client ? =
let = add_authorization headers in
post client ?headers
let put client ? =
let = add_authorization headers in
put client ?headers
let patch client ? =
let = add_authorization headers in
patch client ?headers
let delete client ? =
let = add_authorization headers in
delete client ?headers
module Oneshot = struct
let head ?config ? =
let = add_authorization headers in
Oneshot.head ?config ?headers
let get ?config ? =
let = add_authorization headers in
Oneshot.get ?config ?headers
let post ?config ? =
let = add_authorization headers in
Oneshot.post ?config ?headers
let put ?config ? =
let = add_authorization headers in
Oneshot.put ?config ?headers
let patch ?config ? =
let = add_authorization headers in
Oneshot.patch ?config ?headers
let delete ?config ? =
let = add_authorization headers in
Oneshot.delete ?config ?headers
let request ?config ? =
let = add_authorization headers in
Oneshot.request ?config ?headers
end
end