Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "eventsource"
version = "0.5.0"
version = "0.6.0"
authors = ["Lukas Werling <[email protected]>"]
edition = "2018"

Expand All @@ -13,9 +13,9 @@ keywords = ["http"]
default = ["with-reqwest"]

# Enable the reqwest-based client.
with-reqwest = ["reqwest"]
with-reqwest = ["reqwest", "thiserror"]

[dependencies]
error-chain = "0.12.2"
thiserror = { version = "1.0.21", optional = true }
reqwest = { version = "0.10.4", features = ["blocking"], optional = true }
mime = "0.3.7"
46 changes: 18 additions & 28 deletions src/reqwest.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,24 @@
//! # Reqwest-based EventSource client

mod errors {
use error_chain::*;
error_chain! {
foreign_links {
Reqwest(reqwest::Error);
Io(::std::io::Error);
}

errors {
Http(status: reqwest::StatusCode) {
description("HTTP request failed")
display("HTTP status code: {}", status)
}
InvalidContentType(mime_type: mime::Mime) {
description("unexpected Content-Type header")
display("unexpected Content-Type: {}", mime_type)
}
NoContentType {
description("no Content-Type header in response")
display("Content-Type missing")
}
}
}
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("{0}")]
Reqwest(#[from] reqwest::Error),
#[error("{0}")]
Io(#[from] std::io::Error),
#[error("HTTP status code: {0}")]
Http(reqwest::StatusCode),
#[error("unexpected Content-Type: {0}")]
InvalidContentType(mime::Mime),
#[error("Content-Type missing")]
NoContentType,
}
pub use self::errors::*;

pub type Result<T> = std::result::Result<T, Error>;

use super::event::{parse_event_line, Event, ParseResult};
use reqwest::blocking as reqw;
use reqwest::header::{HeaderMap, HeaderValue, ACCEPT, CONTENT_TYPE};
use super::event::{parse_event_line, Event, ParseResult};
use std::io::{BufRead, BufReader};
use std::time::{Duration, Instant};

Expand Down Expand Up @@ -84,7 +74,7 @@ impl Client {
{
let status = res.status();
if !status.is_success() {
return Err(ErrorKind::Http(status.clone()).into());
return Err(Error::Http(status.clone()));
}

if let Some(content_type_hv) = res.headers().get(CONTENT_TYPE) {
Expand All @@ -98,10 +88,10 @@ impl Client {
if (content_type.type_(), content_type.subtype())
!= (mime::TEXT, mime::EVENT_STREAM)
{
return Err(ErrorKind::InvalidContentType(content_type.clone()).into());
return Err(Error::InvalidContentType(content_type.clone()));
}
} else {
return Err(ErrorKind::NoContentType.into());
return Err(Error::NoContentType);
}
}

Expand Down
6 changes: 3 additions & 3 deletions tests/reqwest.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use eventsource::reqwest::{Client, Error, ErrorKind};
use eventsource::reqwest::{Client, Error};
use reqwest::Url;
use std::time::Duration;

Expand Down Expand Up @@ -79,7 +79,7 @@ fn missing_content_type() {

let mut client = Client::new(Url::parse(&s.url("/")).unwrap());
match client.next().unwrap() {
Err(Error(ErrorKind::NoContentType, _)) => assert!(true),
Err(Error::NoContentType) => assert!(true),
_ => assert!(false, "NoContentType error expected"),
}
}
Expand All @@ -97,7 +97,7 @@ fn invalid_content_type() {

let mut client = Client::new(Url::parse(&s.url("/")).unwrap());
match client.next().unwrap() {
Err(Error(ErrorKind::InvalidContentType(_), _)) => assert!(true),
Err(Error::InvalidContentType(_)) => assert!(true),
_ => assert!(false, "InvalidContentType error expected"),
}
}
Expand Down