fix: handle status codes when not 200
This commit is contained in:
parent
4290ca9a68
commit
b8141458c7
|
@ -86,12 +86,7 @@ impl<'a> CharacterSearchBuilder<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send(&self) -> Result<Paginated<CharacterSearchItem>> {
|
pub fn send(&self) -> Result<Paginated<CharacterSearchItem>> {
|
||||||
let text = self.scraper.client
|
let text = self.scraper.text(self.as_url())?;
|
||||||
.get(self.as_url())
|
|
||||||
.send()
|
|
||||||
.map_err(Error::Net)?
|
|
||||||
.text()
|
|
||||||
.map_err(Error::Net)?;
|
|
||||||
lodestone_parser::parse_character_search(&text).map_err(Error::Parse)
|
lodestone_parser::parse_character_search(&text).map_err(Error::Parse)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -58,12 +58,7 @@ impl<'a> FreeCompanySearchBuilder<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send(&self) -> Result<Paginated<FreeCompanySearchItem>> {
|
pub fn send(&self) -> Result<Paginated<FreeCompanySearchItem>> {
|
||||||
let text = self.scraper.client
|
let text = self.scraper.text(self.as_url())?;
|
||||||
.get(self.as_url())
|
|
||||||
.send()
|
|
||||||
.map_err(Error::Net)?
|
|
||||||
.text()
|
|
||||||
.map_err(Error::Net)?;
|
|
||||||
lodestone_parser::parse_free_company_search(&text).map_err(Error::Parse)
|
lodestone_parser::parse_free_company_search(&text).map_err(Error::Parse)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,13 @@
|
||||||
|
use reqwest::StatusCode;
|
||||||
|
|
||||||
pub type Result<T> = std::result::Result<T, Error>;
|
pub type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
#[derive(Debug, Fail)]
|
#[derive(Debug, Fail)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
#[fail(display = "not found")]
|
||||||
|
NotFound,
|
||||||
|
#[fail(display = "Lodestone responded with an unexpected code: {}", _0)]
|
||||||
|
UnexpectedResponse(StatusCode),
|
||||||
#[fail(display = "network error: {}", _0)]
|
#[fail(display = "network error: {}", _0)]
|
||||||
Net(reqwest::Error),
|
Net(reqwest::Error),
|
||||||
#[fail(display = "url parse error: {}", _0)]
|
#[fail(display = "url parse error: {}", _0)]
|
||||||
|
|
29
src/lib.rs
29
src/lib.rs
|
@ -7,7 +7,7 @@ use lodestone_parser::models::{
|
||||||
free_company::FreeCompany,
|
free_company::FreeCompany,
|
||||||
};
|
};
|
||||||
|
|
||||||
use reqwest::Client;
|
use reqwest::{Client, StatusCode};
|
||||||
|
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
|
@ -41,14 +41,24 @@ impl LodestoneScraper {
|
||||||
LODESTONE_URL.join(s).map_err(Error::Url)
|
LODESTONE_URL.join(s).map_err(Error::Url)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn character(&self, id: u64) -> Result<Character> {
|
crate fn text(&self, url: Url) -> Result<String> {
|
||||||
let url = LodestoneScraper::route(&format!("character/{}", id))?;
|
let mut res = self.client
|
||||||
let text = self.client
|
|
||||||
.get(url)
|
.get(url)
|
||||||
.send()
|
.send()
|
||||||
.map_err(Error::Net)?
|
|
||||||
.text()
|
|
||||||
.map_err(Error::Net)?;
|
.map_err(Error::Net)?;
|
||||||
|
match res.status() {
|
||||||
|
StatusCode::Ok => {},
|
||||||
|
StatusCode::NotFound => return Err(Error::NotFound),
|
||||||
|
x => return Err(Error::UnexpectedResponse(x)),
|
||||||
|
}
|
||||||
|
res
|
||||||
|
.text()
|
||||||
|
.map_err(Error::Net)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn character(&self, id: u64) -> Result<Character> {
|
||||||
|
let url = LodestoneScraper::route(&format!("character/{}", id))?;
|
||||||
|
let text = self.text(url)?;
|
||||||
lodestone_parser::parse_character(id, &text).map_err(Error::Parse)
|
lodestone_parser::parse_character(id, &text).map_err(Error::Parse)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,12 +68,7 @@ impl LodestoneScraper {
|
||||||
|
|
||||||
pub fn free_company(&self, id: u64) -> Result<FreeCompany> {
|
pub fn free_company(&self, id: u64) -> Result<FreeCompany> {
|
||||||
let url = LodestoneScraper::route(&format!("freecompany/{}", id))?;
|
let url = LodestoneScraper::route(&format!("freecompany/{}", id))?;
|
||||||
let text = self.client
|
let text = self.text(url)?;
|
||||||
.get(url)
|
|
||||||
.send()
|
|
||||||
.map_err(Error::Net)?
|
|
||||||
.text()
|
|
||||||
.map_err(Error::Net)?;
|
|
||||||
lodestone_parser::parse_free_company(id, &text).map_err(Error::Parse)
|
lodestone_parser::parse_free_company(id, &text).map_err(Error::Parse)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user