Skip to content

Commit 2544f4a

Browse files
committed
Minor refactor
Added getcharacter API, but not being used right now
1 parent 646da24 commit 2544f4a

File tree

5 files changed

+106
-9
lines changed

5 files changed

+106
-9
lines changed

src/dcli/src/activitystoreinterface.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,7 @@ impl ActivityStoreInterface {
465465

466466
for c in characters.characters {
467467
let character_id = &c.id;
468-
self.insert_character(&c.id, &c.class_type, &member.id)
469-
.await?;
468+
self.insert_character(&c.id, &c.class_type, &member).await?;
470469
tell::progress!("{}", format!("[{}]", c.class_type).to_uppercase());
471470

472471
//these calls could be a little more general purpose by taking api ids and not db ids.
@@ -1041,7 +1040,7 @@ impl ActivityStoreInterface {
10411040

10421041
let class_type = CharacterClass::from_hash(entry.player.class_hash);
10431042

1044-
self.insert_character(&entry.character_id, &class_type, &member.id)
1043+
self.insert_character(&entry.character_id, &class_type, &member)
10451044
.await?;
10461045

10471046
self._insert_character_activity_stats(
@@ -1335,10 +1334,12 @@ impl ActivityStoreInterface {
13351334
&mut self,
13361335
character_id: &i64,
13371336
class_type: &CharacterClass,
1338-
member_id: &i64,
1337+
member: &Member,
13391338
) -> Result<(), Error> {
13401339
let mut stored_class_type = CharacterClass::Unknown;
13411340

1341+
let member_id = &member.id;
1342+
13421343
//first, check if it exists and has valid data
13431344
match sqlx::query(
13441345
r#"
@@ -1361,7 +1362,6 @@ impl ActivityStoreInterface {
13611362
if stored_class_type != CharacterClass::Unknown {
13621363
return Ok(());
13631364
}
1364-
//didn't exists or had invalid data (from API), so lets insert
13651365

13661366
sqlx::query(
13671367
r#"

src/dcli/src/apiinterface.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ use chrono::{DateTime, Utc};
3131
use indicatif::{HumanCount, ProgressBar, ProgressStyle};
3232
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
3333

34-
use crate::response::gmd::GetMembershipData;
35-
use crate::response::gpr::{CharacterActivitiesData, GetProfileResponse};
34+
use crate::response::{gpr::{CharacterActivitiesData, GetProfileResponse}, cr::GetCharacterResponse};
3635
use crate::response::pgcr::{DestinyPostGameCarnageReportData, PGCRResponse};
3736
use crate::response::sdpr::{
3837
DestinyLinkedProfilesResponse, LinkedProfilesResponse,
@@ -46,6 +45,7 @@ use crate::response::{
4645
activities::{ActivitiesResponse, Activity, MAX_ACTIVITIES_REQUEST_COUNT},
4746
sdpr::SearchDestinyPlayerPostData,
4847
};
48+
use crate::response::{character::CharacterData, gmd::GetMembershipData};
4949
use crate::response::{
5050
ggms::{GetGroupMemberResponse, GroupMemberResponse},
5151
gmd::UserMembershipData,
@@ -342,6 +342,49 @@ impl ApiInterface {
342342
Ok(response)
343343
}
344344

345+
pub async fn retrieve_character(
346+
&self,
347+
member:&Member,
348+
character_id: &i64,
349+
) -> Result<Option<CharacterData>, Error> {
350+
351+
let url = format!(
352+
"{base}/Platform/Destiny2/{platform_id}/Profile/{member_id}/Character/{character_id}/?components=200",
353+
354+
355+
base = API_BASE_URL,
356+
platform_id = member.platform.as_id(),
357+
member_id = member.id,
358+
character_id = character_id
359+
);
360+
361+
362+
363+
let profile: GetCharacterResponse = self
364+
.client
365+
.call_and_parse::<GetCharacterResponse>(&url)
366+
.await?;
367+
368+
let response = match profile.response {
369+
Some(e) => e,
370+
None => {
371+
return Err(Error::ApiRequest {
372+
description: String::from(
373+
"No response data from API Call.",
374+
),
375+
})
376+
}
377+
};
378+
379+
380+
381+
if response.character.is_none() {
382+
return Ok(None)
383+
}
384+
385+
Ok(response.character.unwrap().data)
386+
}
387+
345388
pub async fn get_player_info(
346389
&self,
347390
member_id: &i64,

src/dcli/src/response/cr.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2023 Mike Chambers
3+
* https://github.com/mikechambers/dcli
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
* this software and associated documentation files (the "Software"), to deal in
7+
* the Software without restriction, including without limitation the rights to
8+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9+
* of the Software, and to permit persons to whom the Software is furnished to do
10+
* so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
*/
22+
23+
use serde_derive::{Deserialize, Serialize};
24+
25+
use crate::enums::mode::Mode;
26+
use crate::response::character::CharacterData;
27+
use crate::response::drs::{DestinyResponseStatus, IsDestinyAPIResponse};
28+
use crate::response::utils::str_to_datetime;
29+
30+
#[derive(Serialize, Deserialize, Debug)]
31+
pub struct GetCharacterResponse {
32+
#[serde(rename = "Response")]
33+
pub response: Option<CharacterResponse>, //should this be an option?
34+
35+
#[serde(flatten)]
36+
pub status: DestinyResponseStatus,
37+
}
38+
39+
impl IsDestinyAPIResponse for GetCharacterResponse {
40+
fn get_status(&self) -> &DestinyResponseStatus {
41+
&self.status
42+
}
43+
}
44+
45+
#[derive(Serialize, Deserialize, Debug)]
46+
pub struct CharacterResponse {
47+
pub character: Option<CharacterDataFieldData>,
48+
}
49+
50+
#[derive(Serialize, Deserialize, Debug)]
51+
pub struct CharacterDataFieldData {
52+
pub data: Option<CharacterData>,
53+
}

src/dcli/src/response/gpr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl IsDestinyAPIResponse for GetProfileResponse {
4949

5050
#[derive(Serialize, Deserialize, Debug)]
5151
pub struct ProfileResponse {
52-
pub characters: Option<CharacterDataFieldData>,
52+
pub characters: Option<CharactersDataFieldData>,
5353

5454
#[serde(rename = "characterActivities")]
5555
pub character_activities: Option<CharacterActivitiesFieldData>,
@@ -74,7 +74,7 @@ pub struct CharacterActivitiesDataField {
7474
}
7575

7676
#[derive(Serialize, Deserialize, Debug)]
77-
pub struct CharacterDataFieldData {
77+
pub struct CharactersDataFieldData {
7878
pub data: HashMap<String, CharacterData>,
7979
}
8080

src/dcli/src/response/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
pub mod activities;
2424
pub mod character;
25+
pub mod cr;
2526
pub mod drs;
2627
pub mod ggms;
2728
pub mod gmd;

0 commit comments

Comments
 (0)