@@ -2,81 +2,62 @@ import gleam/dynamic.{Dynamic}
2
2
import gleam/http . { Method }
3
3
import gleam/list
4
4
5
- external type Charlist ;
5
+ external type Charlist
6
6
7
- external fn binary_to_list(String) -> Charlist
8
- = "erlang" "binary_to_list"
7
+ external fn binary_to_list ( String ) -> Charlist =
8
+ "erlang" "binary_to_list"
9
9
10
- external fn list_to_binary(Charlist) -> String
11
- = "erlang" "list_to_binary"
10
+ external fn list_to_binary ( Charlist ) -> String =
11
+ "erlang" "list_to_binary"
12
12
13
- external type ErlHttpOption;
13
+ external type ErlHttpOption
14
14
15
15
type BodyFormat {
16
16
Binary
17
- };
17
+ }
18
18
19
19
type ErlOption {
20
20
BodyFormat ( BodyFormat )
21
- };
21
+ }
22
22
23
23
external fn erl_request (
24
24
Method ,
25
25
tuple ( Charlist, List(tuple(Charlist, Charlist)), Charlist, String),
26
26
List(ErlHttpOption),
27
27
List(ErlOption),
28
28
) -> Result(
29
- tuple(
30
- tuple(Charlist, Int, Charlist),
31
- List(tuple(Charlist, Charlist)),
32
- String,
33
- ),
29
+ tuple(tuple(Charlist, Int, Charlist), List(tuple(Charlist, Charlist)), String),
34
30
Dynamic,
35
- )
36
- = "httpc" "request"
31
+ ) =
32
+ "httpc" "request"
37
33
38
34
external fn erl_request_no_body(
39
35
Method,
40
36
tuple(Charlist, List(tuple(Charlist, Charlist))),
41
37
List(ErlHttpOption),
42
38
List(ErlOption),
43
39
) -> Result(
44
- tuple(
45
- tuple(Charlist, Int, Charlist),
46
- List(tuple(Charlist, Charlist)),
47
- String,
48
- ),
40
+ tuple(tuple(Charlist, Int, Charlist), List(tuple(Charlist, Charlist)), String),
49
41
Dynamic,
50
- )
51
- = "httpc" "request"
42
+ ) =
43
+ "httpc" "request"
52
44
53
45
pub type RequestBody {
54
- Text (
55
- content_type : String ,
56
- body : String ,
57
- )
58
-
59
- None
46
+ StringBody ( content_type : String , body : String )
47
+ BitBody ( content_type : String , body : BitString )
48
+ NoBody
60
49
}
61
50
62
51
pub type Response {
63
- Response (
64
- status : Int ,
65
- headers : List ( tuple( String, String ) ) ,
66
- body: String ,
67
- )
52
+ Response ( status : Int , headers : List ( tuple( String, String ) ) , body: String )
68
53
}
69
54
70
- fn charlist_header ( header : tuple( String, String) )
71
- -> tuple( Charlist , Charlist )
72
- {
55
+ fn charlist_header ( header : tuple( String, String) ) -> tuple( Charlist , Charlist ) {
73
56
let tuple ( k, v) = header
74
57
tuple ( binary_to_list ( k ) , binary_to_list ( v ) )
75
58
}
76
59
77
- fn string_header ( header : tuple( Charlist, Charlist) )
78
- -> tuple( String , String )
79
- {
60
+ fn string_header ( header : tuple( Charlist, Charlist) ) -> tuple( String , String ) {
80
61
let tuple ( k, v) = header
81
62
tuple ( list_to_binary ( k ) , list_to_binary ( v ) )
82
63
}
@@ -93,24 +74,23 @@ pub fn request(
93
74
let erl_http_options = [ ]
94
75
let erl_options = [ BodyFormat ( Binary ) ]
95
76
96
- let response = case body {
97
- Text ( content_type : content_type , body : body ) -> {
77
+ let response = case method , body {
78
+ http . Options , _ | http . Head , _ | http . Get , _ | _ , NoBody -> {
79
+ let request = tuple ( erl_url , erl_headers )
80
+ erl_request_no_body ( method , request , erl_http_options , erl_options )
81
+ }
82
+ _ , StringBody ( content_type : content_type , body : body ) -> {
98
83
let erl_content_type = binary_to_list ( content_type )
99
84
let request = tuple ( erl_url , erl_headers , erl_content_type , body )
100
85
erl_request ( method , request , erl_http_options , erl_options )
101
86
}
102
-
103
- None -> {
104
- let request = tuple ( erl_url , erl_headers )
105
- erl_request_no_body ( method , request , erl_http_options , erl_options )
106
- }
107
87
}
108
88
109
89
case response {
110
- Error ( error ) ->
111
- Error ( error )
90
+ Error ( error ) -> Error ( error )
112
91
113
- Ok ( tuple ( tuple( _http_version, status , _status ) , headers , resp_body ) ) ->
114
- Ok ( Response ( status , list . map ( headers , string_header ) , resp_body ) )
92
+ Ok (
93
+ tuple ( tuple( _http_version, status , _status ) , headers , resp_body ) ,
94
+ ) -> Ok ( Response ( status , list . map ( headers , string_header ) , resp_body ) )
115
95
}
116
96
}
0 commit comments