Skip to content

Commit 7e57ee5

Browse files
authored
Add body and HTTP method builder functionality (#21)
1 parent ba01c39 commit 7e57ee5

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

eventsource-client/src/client.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ pub struct ClientBuilder {
6666
reconnect_opts: ReconnectOptions,
6767
read_timeout: Option<Duration>,
6868
last_event_id: String,
69+
method: String,
70+
body: Option<String>,
6971
}
7072

7173
impl ClientBuilder {
@@ -85,9 +87,23 @@ impl ClientBuilder {
8587
reconnect_opts: ReconnectOptions::default(),
8688
read_timeout: None,
8789
last_event_id: String::new(),
90+
method: String::from("GET"),
91+
body: None,
8892
})
8993
}
9094

95+
/// Set the request method used for the initial connection to the SSE endpoint.
96+
pub fn method(mut self, method: String) -> ClientBuilder {
97+
self.method = method;
98+
self
99+
}
100+
101+
/// Set the request body used for the initial connection to the SSE endpoint.
102+
pub fn body(mut self, body: String) -> ClientBuilder {
103+
self.body = Some(body);
104+
self
105+
}
106+
91107
/// Set the last event id for a stream when it is created. If it is set, it will be sent to the
92108
/// server in case it can replay missed events.
93109
pub fn last_event_id(mut self, last_event_id: String) -> ClientBuilder {
@@ -139,6 +155,8 @@ impl ClientBuilder {
139155
request_props: RequestProps {
140156
url: self.url,
141157
headers: self.headers,
158+
method: self.method,
159+
body: self.body,
142160
reconnect_opts: self.reconnect_opts,
143161
},
144162
last_event_id: self.last_event_id,
@@ -167,6 +185,8 @@ impl ClientBuilder {
167185
request_props: RequestProps {
168186
url: self.url,
169187
headers: self.headers,
188+
method: self.method,
189+
body: self.body,
170190
reconnect_opts: self.reconnect_opts,
171191
},
172192
last_event_id: self.last_event_id,
@@ -178,6 +198,8 @@ impl ClientBuilder {
178198
struct RequestProps {
179199
url: Uri,
180200
headers: HeaderMap,
201+
method: String,
202+
body: Option<String>,
181203
reconnect_opts: ReconnectOptions,
182204
}
183205

@@ -276,7 +298,9 @@ impl<C> ReconnectingRequest<C> {
276298
where
277299
C: Connect + Clone + Send + Sync + 'static,
278300
{
279-
let mut request_builder = Request::builder().uri(&self.props.url);
301+
let mut request_builder = Request::builder()
302+
.method(self.props.method.as_str())
303+
.uri(&self.props.url);
280304

281305
for (name, value) in &self.props.headers {
282306
request_builder = request_builder.header(name, value);
@@ -289,7 +313,10 @@ impl<C> ReconnectingRequest<C> {
289313
);
290314
}
291315

292-
let body = Body::empty();
316+
let body = match &self.props.body {
317+
Some(body) => Body::from(body.to_string()),
318+
None => Body::empty(),
319+
};
293320

294321
let request = request_builder
295322
.body(body)

0 commit comments

Comments
 (0)