1
1
use env_logger;
2
2
use log:: info;
3
- use fplus_database;
3
+ use fplus_database:: database ;
4
4
pub ( crate ) mod router;
5
5
use reqwest:: Client ;
6
6
use serde:: Deserialize ;
63
63
fn call ( & self , req : ServiceRequest ) -> Self :: Future {
64
64
let query_string = req. query_string ( ) ;
65
65
let query: Result < web:: Query < RepoQuery > , _ > = web:: Query :: from_query ( query_string) ;
66
+ let RepoQuery { owner, repo } = match query {
67
+ Ok ( q) => q. into_inner ( ) ,
68
+ Err ( _) => {
69
+ return Box :: pin ( async {
70
+ return Err ( actix_web:: error:: ErrorBadRequest ( "Wrong query string format" ) ) ;
71
+ } ) ;
72
+ }
73
+ } ;
66
74
67
75
let auth_header_value = req. headers ( ) . get ( "Authorization" )
68
76
. and_then ( |hv| hv. to_str ( ) . ok ( ) )
@@ -71,47 +79,45 @@ where
71
79
let fut = self . service . call ( req) ;
72
80
73
81
Box :: pin ( async move {
74
- // if let Some(token) = auth_header_value {
75
- // // Make the asynchronous HTTP request here
76
- // let client = Client::new();
77
- // let user_info_result = client.get("https://api.github.com/user")
78
- // .header("Authorization", format!("Bearer {}", token))
79
- // .header("User-Agent", "Actix-web")
80
- // .send()
81
- // .await;
82
+ if let Some ( token) = auth_header_value {
83
+ // Make the asynchronous HTTP request here
84
+ let client = Client :: new ( ) ;
85
+ let user_info_result = client. get ( "https://api.github.com/user" )
86
+ . header ( "Authorization" , format ! ( "Bearer {}" , token) )
87
+ . header ( "User-Agent" , "Actix-web" )
88
+ . send ( )
89
+ . await ;
82
90
83
- // match user_info_result {
84
- // Ok(response) => {
85
- // if response.status().is_success() {
86
- // let user_info = response
87
- // .json::<serde_json::Value>()
88
- // .await
89
- // .expect("Failed to parse JSON");
90
-
91
- // if let Some(login) = user_info.get("login").and_then(|v| v.as_str()) {
92
- // println!("Login: {}", login);
93
- // } else {
94
- // println!("Login information not found.");
95
- // }
96
- // } else {
97
- // println!("Failed to get GitHub user info");
98
- // }
99
- // },
100
- // Err(e) => println!("Request error: {:?}", e),
101
- // }
102
- // }
103
-
104
- // match database::get_allocator(&owner, &repo).await {
105
- // Ok(allocator) => {
106
- // match allocator {
107
- // Some(allocator) => HttpResponse::Ok().json(allocator),
108
- // None => HttpResponse::NotFound().finish(),
109
- // }
110
- // },
111
- // Err(e) => {
112
- // HttpResponse::InternalServerError().body(e.to_string())
113
- // }
114
- // }
91
+ match user_info_result {
92
+ Ok ( response) => {
93
+ //Raise an actix test error
94
+ if response. status ( ) . is_success ( ) {
95
+ let user_info = response
96
+ . json :: < serde_json:: Value > ( )
97
+ . await
98
+ . expect ( "Failed to parse JSON" ) ;
99
+
100
+ if let Some ( login) = user_info. get ( "login" ) . and_then ( |v| v. as_str ( ) ) {
101
+ println ! ( "Login: {}" , login) ;
102
+ } else {
103
+ println ! ( "Login information not found." ) ;
104
+ }
105
+ } else {
106
+ println ! ( "Failed to get GitHub user info" ) ;
107
+ }
108
+ } ,
109
+ Err ( e) => println ! ( "Request error: {:?}" , e) ,
110
+ }
111
+ }
112
+
113
+ match database:: get_allocator ( & owner, & repo) . await {
114
+ Ok ( allocator) => {
115
+ println ! ( "Allocator: {:?}" , allocator) ;
116
+ } ,
117
+ Err ( e) => {
118
+ println ! ( "Failed to get allocator: {:?}" , e) ;
119
+ }
120
+ }
115
121
116
122
let res = fut. await ?;
117
123
println ! ( "Hi from response" ) ;
0 commit comments