Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure profileId is set when testing API token #77

Merged
merged 7 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion TarkovMonitor/Blazor/Pages/Settings/Settings.razor
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,18 @@
}
}

public string ProfileId
Owaan marked this conversation as resolved.
Show resolved Hide resolved
{
get
{
return eft.CurrentProfile.Id;
}
set
{
eft.CurrentProfile.Id = value;
}
}

void TokenShowClick()
{
if (tokenShow)
Expand All @@ -442,7 +454,7 @@
} else {
try
{
var tokenResponse = await TarkovTracker.TestToken(TarkovTrackerToken);
var tokenResponse = await TarkovTracker.TestToken(ProfileId, TarkovTrackerToken);
if (!tokenResponse.permissions.Contains("WP"))
{
messageLog.AddMessage("You do not have write permissions with this token.", "warning");
Expand Down
26 changes: 16 additions & 10 deletions TarkovMonitor/MainBlazorUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ public MainBlazorUI()
{
if (Properties.Settings.Default.tarkovTrackerToken != "")
{
TarkovTracker.SetToken(e.Profile.Id, Properties.Settings.Default.tarkovTrackerToken);
try {
TarkovTracker.SetToken(e.Profile.Id, Properties.Settings.Default.tarkovTrackerToken);
} catch (Exception ex) {
messageLog.AddMessage($"Error setting token from previously saved settings {ex.Message}", "exception");
}

Properties.Settings.Default.tarkovTrackerToken = "";
Properties.Settings.Default.Save();
}
Expand Down Expand Up @@ -460,24 +465,24 @@ private async Task UpdateHideoutStations()

private async Task InitializeProgress()
{
if (TarkovTracker.GetToken(eft.CurrentProfile.Id) == "")
{
messageLog.AddMessage("To automatically track task progress, set your Tarkov Tracker token in Settings");
return;
}
messageLog.AddMessage($"Using {eft.CurrentProfile.Type} profile");
try
{
await TarkovTracker.SetProfile(eft.CurrentProfile.Id);
return;
}
catch (Exception ex)
{
messageLog.AddMessage($"Error getting Tarkov Tracker progress: {ex.Message}");
messageLog.AddMessage($"Profile does not exist: {ex.Message}");
return;
}
messageLog.AddMessage($"Using {eft.CurrentProfile.Type} profile");
if (TarkovTracker.GetToken(eft.CurrentProfile.Id) == "")
{
messageLog.AddMessage("To automatically track task progress, set your Tarkov Tracker token in Settings");
return;
}
try
{
var tokenResponse = await TarkovTracker.TestToken(TarkovTracker.GetToken(eft.CurrentProfile.Id));
var tokenResponse = await TarkovTracker.TestToken(eft.CurrentProfile.Id, TarkovTracker.GetToken(eft.CurrentProfile.Id));
if (!tokenResponse.permissions.Contains("WP"))
{
messageLog.AddMessage("Your Tarkov Tracker token is missing the required write permissions");
Expand All @@ -486,6 +491,7 @@ private async Task InitializeProgress()
catch (Exception ex)
{
messageLog.AddMessage($"Error updating progress: {ex.Message}");
return;
}
}

Expand Down
18 changes: 13 additions & 5 deletions TarkovMonitor/TarkovTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal interface ITarkovTrackerAPI

[Get("/token")]
[Headers("Authorization: Bearer {token}")]
Task<TokenResponse> TestToken(string token);
Owaan marked this conversation as resolved.
Show resolved Hide resolved
Task<TokenResponse> TestToken(string profileId, string token);

[Get("/progress")]
[Headers("Authorization: Bearer")]
Expand All @@ -35,6 +35,10 @@ internal interface ITarkovTrackerAPI
{
AuthorizationHeaderValueGetter = (rq, cr) => {
return Task.Run<string>(() => {
if (tokens == null || !tokens.ContainsKey(currentProfile)) {
throw new Exception("No PVE or PVP profile could be found, launch tarkov first");
}

return tokens[currentProfile];
});
},
Expand Down Expand Up @@ -68,7 +72,7 @@ public static void SetToken(string profileId, string token)
{
if (profileId == "")
{
return;
throw new Exception("No PVP or PVE profile initialised, please launch Escape from Tarkov first");
}
tokens[profileId] = token;
Properties.Settings.Default.tarkovTrackerTokens = JsonSerializer.Serialize(tokens);
Expand All @@ -77,6 +81,10 @@ public static void SetToken(string profileId, string token)

public static async Task<ProgressResponse> SetProfile(string profileId)
{
if (profileId == "") {
throw new Exception("Can't set PVP or PVE profile, please launch Escape from Tarkov and then restart this application");
}

if (currentProfile == profileId)
{
return Progress;
Expand All @@ -94,7 +102,7 @@ public static async Task<ProgressResponse> SetProfile(string profileId)
Progress = new();
return Progress;
}
await TestToken(newToken);
await TestToken(profileId, newToken);
Owaan marked this conversation as resolved.
Show resolved Hide resolved
return Progress;
}

Expand Down Expand Up @@ -285,11 +293,11 @@ public static async Task<ProgressResponse> GetProgress()
}
}

public static async Task<TokenResponse> TestToken(string apiToken)
public static async Task<TokenResponse> TestToken(string profileId, string apiToken)
Owaan marked this conversation as resolved.
Show resolved Hide resolved
{
try
{
var response = await api.TestToken(apiToken);
var response = await api.TestToken(profileId, apiToken);
if (response.permissions.Contains("WP"))
{
ValidToken = true;
Expand Down