@@ -22,7 +22,7 @@ use chrono::NaiveDate;
2222use reqwest:: blocking:: Response ;
2323use reqwest:: Error ;
2424use 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}
0 commit comments