Skip to content

Commit c683c6a

Browse files
authored
Merge pull request #16 from nanato12/feature/refactoring
Refactoring
2 parents 2e14b5b + 0ca64f8 commit c683c6a

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed

src/bot.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use chrono::NaiveDate;
2222
use reqwest::blocking::Response;
2323
use reqwest::Error;
2424
use serde_derive::Serialize;
25-
use serde_json::{json, Value};
25+
use serde_json::{json, Error as JsonError, Value};
2626

2727
/// LineBot Client
2828
#[derive(Debug)]
@@ -626,13 +626,7 @@ impl LineBot {
626626
/// ```
627627
pub fn get_profile(&self, user_id: &str) -> Result<Profile, &str> {
628628
let endpoint = format!("/profile/{userId}", userId = user_id);
629-
match self.http_client.get(&endpoint, vec![], json!({})) {
630-
Ok(res) => {
631-
let profile: Profile = serde_json::from_str(&res.text().unwrap()).unwrap();
632-
Ok(profile)
633-
}
634-
Err(_) => Err("Failed get_profile"),
635-
}
629+
self.parse_profile_response(self.http_client.get(&endpoint, vec![], json!({})))
636630
}
637631

638632
/// # Note
@@ -689,13 +683,7 @@ impl LineBot {
689683
groupId = group_id,
690684
userId = user_id
691685
);
692-
match self.http_client.get(&endpoint, vec![], json!({})) {
693-
Ok(res) => {
694-
let profile: Profile = serde_json::from_str(&res.text().unwrap()).unwrap();
695-
Ok(profile)
696-
}
697-
Err(_) => Err("Failed get_profile_from_group"),
698-
}
686+
self.parse_profile_response(self.http_client.get(&endpoint, vec![], json!({})))
699687
}
700688

701689
/// # Note
@@ -742,13 +730,7 @@ impl LineBot {
742730
roomId = room_id,
743731
userId = user_id
744732
);
745-
match self.http_client.get(&endpoint, vec![], json!({})) {
746-
Ok(res) => {
747-
let profile: Profile = serde_json::from_str(&res.text().unwrap()).unwrap();
748-
Ok(profile)
749-
}
750-
Err(_) => Err("Failed get_profile_from_room"),
751-
}
733+
self.parse_profile_response(self.http_client.get(&endpoint, vec![], json!({})))
752734
}
753735

754736
/// # Note
@@ -773,4 +755,25 @@ impl LineBot {
773755
let endpoint = format!("/user/{userId}/linkToken", userId = user_id);
774756
self.http_client.post(&endpoint, json!({}))
775757
}
758+
759+
fn parse_profile_response(
760+
&self,
761+
result: Result<Response, Error>,
762+
) -> Result<Profile, &'static str> {
763+
// GET request
764+
if let Ok(res) = result {
765+
// Get response text
766+
match res.text() {
767+
Ok(text) => {
768+
let result: Result<Profile, JsonError> = serde_json::from_str(&text);
769+
match result {
770+
Ok(profile) => return Ok(profile),
771+
Err(_) => return Err("Failed response parsing."),
772+
}
773+
}
774+
Err(_) => return Err("Failed getting response."),
775+
}
776+
}
777+
Err("Failed GET request.")
778+
}
776779
}

src/client.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ impl HttpClient {
2323
/// ```
2424
pub fn new(channel_token: &str) -> HttpClient {
2525
let mut headers = HeaderMap::new();
26-
headers.insert(
27-
AUTHORIZATION,
28-
format!("Bearer {}", channel_token).parse().unwrap(),
29-
);
26+
if let Ok(v) = format!("Bearer {}", channel_token).parse::<String>() {
27+
if let Ok(header_value) = HeaderValue::from_str(&v) {
28+
headers.insert(AUTHORIZATION, header_value);
29+
}
30+
}
3031
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
3132
HttpClient {
3233
client: Client::new(),

0 commit comments

Comments
 (0)