recycleactor commited on
Commit
5e6c312
·
verified ·
1 Parent(s): 787824d

Upload 2 files

Browse files
Files changed (1) hide show
  1. src/main.rs +18 -14
src/main.rs CHANGED
@@ -25,6 +25,10 @@ type BoxErr = Box<dyn std::error::Error + Send + Sync + 'static>;
25
  mod api;
26
  use api::{APIClient, APIError, Format};
27
 
 
 
 
 
28
  #[derive(Deserialize)]
29
  struct DeezerTrackList {
30
  data: Vec<DeezerTrack>
@@ -271,13 +275,13 @@ struct UserDataReq {
271
  async fn user_data(Json(req): Json<UserDataReq>) -> impl IntoResponse {
272
  let arl = req.arl.trim().to_string();
273
  if arl.is_empty() {
274
- return (StatusCode::BAD_REQUEST, "ARL required".to_string()).into_response();
275
  }
276
 
277
  let mut client = APIClient::new_with_arl(arl);
278
  let data = match client.user_data().await {
279
  Ok(v) => v,
280
- Err(e) => return (StatusCode::BAD_GATEWAY, e.to_string()).into_response(),
281
  };
282
 
283
  let token = data["checkForm"].as_str().unwrap_or("").to_string();
@@ -296,18 +300,18 @@ struct PlaylistsReq {
296
  async fn playlists(Json(req): Json<PlaylistsReq>) -> impl IntoResponse {
297
  let arl = req.arl.trim().to_string();
298
  if arl.is_empty() {
299
- return (StatusCode::BAD_REQUEST, "ARL required".to_string()).into_response();
300
  }
301
 
302
  let mut client = APIClient::new_with_arl(arl);
303
  let ud = match client.user_data().await {
304
  Ok(v) => v,
305
- Err(e) => return (StatusCode::BAD_GATEWAY, e.to_string()).into_response(),
306
  };
307
 
308
  let uid = ud["USER"]["USER_ID"].as_i64().unwrap_or(0);
309
  if uid <= 0 {
310
- return (StatusCode::BAD_GATEWAY, "No USER_ID".to_string()).into_response();
311
  }
312
 
313
  let start = req.start.unwrap_or(0);
@@ -319,7 +323,7 @@ async fn playlists(Json(req): Json<PlaylistsReq>) -> impl IntoResponse {
319
 
320
  match res {
321
  Ok(v) => (StatusCode::OK, Json(v)).into_response(),
322
- Err(e) => (StatusCode::BAD_GATEWAY, e.to_string()).into_response(),
323
  }
324
  }
325
 
@@ -334,7 +338,7 @@ struct PlaylistTracksReq {
334
  async fn playlist_tracks(Json(req): Json<PlaylistTracksReq>) -> impl IntoResponse {
335
  let arl = req.arl.trim().to_string();
336
  if arl.is_empty() {
337
- return (StatusCode::BAD_REQUEST, "ARL required".to_string()).into_response();
338
  }
339
 
340
  let mut client = APIClient::new_with_arl(arl);
@@ -356,7 +360,7 @@ async fn playlist_tracks(Json(req): Json<PlaylistTracksReq>) -> impl IntoRespons
356
 
357
  match res {
358
  Ok(v) => (StatusCode::OK, Json(v)).into_response(),
359
- Err(e) => (StatusCode::BAD_GATEWAY, e.to_string()).into_response(),
360
  }
361
  }
362
 
@@ -409,7 +413,7 @@ async fn login(Json(req): Json<LoginReq>) -> impl IntoResponse {
409
  let pass = req.password_md5.trim().to_string();
410
 
411
  if email.is_empty() || pass.is_empty() {
412
- return (StatusCode::BAD_REQUEST, "Email and password_md5 required".to_string()).into_response();
413
  }
414
  let jar = Jar::default();
415
  let client = reqwest::Client::builder()
@@ -420,12 +424,12 @@ async fn login(Json(req): Json<LoginReq>) -> impl IntoResponse {
420
 
421
  let ud = match gw_light_call(&client, "deezer.getUserData", "null", json!({})).await {
422
  Ok(v) => v,
423
- Err(e) => return (StatusCode::BAD_GATEWAY, e).into_response(),
424
  };
425
 
426
  let token = ud["checkForm"].as_str().unwrap_or("").to_string();
427
  if token.is_empty() {
428
- return (StatusCode::BAD_GATEWAY, "no checkForm".to_string()).into_response();
429
  }
430
 
431
  let cred = match gw_light_call(
@@ -441,19 +445,19 @@ async fn login(Json(req): Json<LoginReq>) -> impl IntoResponse {
441
  .await
442
  {
443
  Ok(v) => v,
444
- Err(e) => return (StatusCode::UNAUTHORIZED, e).into_response(),
445
  };
446
 
447
  let uid = cred["USER"]["USER_ID"].as_i64().map(|n| n.to_string()).unwrap_or_default();
448
 
449
  let arl_v = match gw_light_call(&client, "user.getArl", &token, json!({})).await {
450
  Ok(v) => v,
451
- Err(e) => return (StatusCode::BAD_GATEWAY, e).into_response(),
452
  };
453
 
454
  let arl = arl_v.as_str().unwrap_or("").to_string();
455
  if arl.len() < 20 {
456
- return (StatusCode::BAD_GATEWAY, "no arl".to_string()).into_response();
457
  }
458
 
459
  (StatusCode::OK, Json(json!({ "arl": arl, "token": token, "uid": uid }))).into_response()
 
25
  mod api;
26
  use api::{APIClient, APIError, Format};
27
 
28
+ fn json_error(status: StatusCode, message: impl Into<String>) -> axum::response::Response {
29
+ (status, Json(json!({ "error": message.into() }))).into_response()
30
+ }
31
+
32
  #[derive(Deserialize)]
33
  struct DeezerTrackList {
34
  data: Vec<DeezerTrack>
 
275
  async fn user_data(Json(req): Json<UserDataReq>) -> impl IntoResponse {
276
  let arl = req.arl.trim().to_string();
277
  if arl.is_empty() {
278
+ return json_error(StatusCode::BAD_REQUEST, "ARL required");
279
  }
280
 
281
  let mut client = APIClient::new_with_arl(arl);
282
  let data = match client.user_data().await {
283
  Ok(v) => v,
284
+ Err(e) => return json_error(StatusCode::BAD_GATEWAY, e.to_string()),
285
  };
286
 
287
  let token = data["checkForm"].as_str().unwrap_or("").to_string();
 
300
  async fn playlists(Json(req): Json<PlaylistsReq>) -> impl IntoResponse {
301
  let arl = req.arl.trim().to_string();
302
  if arl.is_empty() {
303
+ return json_error(StatusCode::BAD_REQUEST, "ARL required");
304
  }
305
 
306
  let mut client = APIClient::new_with_arl(arl);
307
  let ud = match client.user_data().await {
308
  Ok(v) => v,
309
+ Err(e) => return json_error(StatusCode::BAD_GATEWAY, e.to_string()),
310
  };
311
 
312
  let uid = ud["USER"]["USER_ID"].as_i64().unwrap_or(0);
313
  if uid <= 0 {
314
+ return json_error(StatusCode::BAD_GATEWAY, "No USER_ID");
315
  }
316
 
317
  let start = req.start.unwrap_or(0);
 
323
 
324
  match res {
325
  Ok(v) => (StatusCode::OK, Json(v)).into_response(),
326
+ Err(e) => json_error(StatusCode::BAD_GATEWAY, e.to_string()),
327
  }
328
  }
329
 
 
338
  async fn playlist_tracks(Json(req): Json<PlaylistTracksReq>) -> impl IntoResponse {
339
  let arl = req.arl.trim().to_string();
340
  if arl.is_empty() {
341
+ return json_error(StatusCode::BAD_REQUEST, "ARL required");
342
  }
343
 
344
  let mut client = APIClient::new_with_arl(arl);
 
360
 
361
  match res {
362
  Ok(v) => (StatusCode::OK, Json(v)).into_response(),
363
+ Err(e) => json_error(StatusCode::BAD_GATEWAY, e.to_string()),
364
  }
365
  }
366
 
 
413
  let pass = req.password_md5.trim().to_string();
414
 
415
  if email.is_empty() || pass.is_empty() {
416
+ return json_error(StatusCode::BAD_REQUEST, "Email and password_md5 required");
417
  }
418
  let jar = Jar::default();
419
  let client = reqwest::Client::builder()
 
424
 
425
  let ud = match gw_light_call(&client, "deezer.getUserData", "null", json!({})).await {
426
  Ok(v) => v,
427
+ Err(e) => return json_error(StatusCode::BAD_GATEWAY, e),
428
  };
429
 
430
  let token = ud["checkForm"].as_str().unwrap_or("").to_string();
431
  if token.is_empty() {
432
+ return json_error(StatusCode::BAD_GATEWAY, "no checkForm");
433
  }
434
 
435
  let cred = match gw_light_call(
 
445
  .await
446
  {
447
  Ok(v) => v,
448
+ Err(e) => return json_error(StatusCode::UNAUTHORIZED, e),
449
  };
450
 
451
  let uid = cred["USER"]["USER_ID"].as_i64().map(|n| n.to_string()).unwrap_or_default();
452
 
453
  let arl_v = match gw_light_call(&client, "user.getArl", &token, json!({})).await {
454
  Ok(v) => v,
455
+ Err(e) => return json_error(StatusCode::BAD_GATEWAY, e),
456
  };
457
 
458
  let arl = arl_v.as_str().unwrap_or("").to_string();
459
  if arl.len() < 20 {
460
+ return json_error(StatusCode::BAD_GATEWAY, "no arl");
461
  }
462
 
463
  (StatusCode::OK, Json(json!({ "arl": arl, "token": token, "uid": uid }))).into_response()