Skip to content

Commit

Permalink
Replace fetch with Obsidian requestUrl in StravaApi
Browse files Browse the repository at this point in the history
  • Loading branch information
watsonbox committed Oct 2, 2024
1 parent 9ea66b4 commit 6771f8f
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions src/StravaApi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { requestUrl } from "obsidian";
import type { AuthenticationSettings } from "./Settings";

export class StravaApi {
Expand Down Expand Up @@ -65,7 +66,8 @@ export class StravaApi {
}

private async tokenRequest(params: Record<string, string>): Promise<any> {
const response = await fetch("https://www.strava.com/oauth/token", {
const response = await requestUrl({
url: "https://www.strava.com/oauth/token",
method: "POST",
headers: {
"Content-Type": "application/json",
Expand All @@ -77,7 +79,7 @@ export class StravaApi {
}),
});

return response.json();
return response.json;
}

private updateTokens(response: any) {
Expand All @@ -91,29 +93,25 @@ export class StravaApi {
> {
await this.refreshTokenIfExpired();
const queryParams = new URLSearchParams(params as any).toString();
const response = await fetch(
`https://www.strava.com/api/v3/athlete/activities?${queryParams}`,
{
headers: {
Authorization: `Bearer ${this.settings.stravaAccessToken}`,
},
const response = await requestUrl({
url: `https://www.strava.com/api/v3/athlete/activities?${queryParams}`,
headers: {
Authorization: `Bearer ${this.settings.stravaAccessToken}`,
},
);
});

return response.json();
return response.json;
}

async getActivity(id: number): Promise<any> {
await this.refreshTokenIfExpired();
const response = await fetch(
`https://www.strava.com/api/v3/activities/${id}`,
{
headers: {
Authorization: `Bearer ${this.settings.stravaAccessToken}`,
},
const response = await requestUrl({
url: `https://www.strava.com/api/v3/activities/${id}`,
headers: {
Authorization: `Bearer ${this.settings.stravaAccessToken}`,
},
);
});

return response.json();
return response.json;
}
}

0 comments on commit 6771f8f

Please sign in to comment.