-
Notifications
You must be signed in to change notification settings - Fork 0
Use axios for fetching challenges #12
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,66 +50,66 @@ export class QueryChallengesTool { | |
accessToken, | ||
); | ||
|
||
if (!challenges.ok) { | ||
if (challenges.status < 200 || challenges.status >= 300) { | ||
this.logger.error( | ||
`Failed to fetch challenges from Topcoder API: ${challenges.statusText}`, | ||
); | ||
try { | ||
this.logger.error(await challenges.json()); | ||
this.logger.error(challenges.data); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. medium |
||
} catch (e) { | ||
this.logger.error('Failed to log challenge error'); | ||
} | ||
|
||
// Return an error response if the API call fails | ||
return { | ||
content: [ | ||
{ | ||
type: 'text', | ||
text: `Error fetching challenges: ${challenges.statusText}`, | ||
}, | ||
{ | ||
type: 'text', | ||
text: `Error fetching challenges: ${challenges.statusText}`, | ||
}, | ||
], | ||
isError: true, | ||
}; | ||
} | ||
|
||
// Parse the response as JSON | ||
const challengesData = await challenges.json(); | ||
// Axios response: data is already parsed, headers are plain object | ||
const challengesData = challenges.data; | ||
|
||
return { | ||
content: [ | ||
{ | ||
type: 'text', | ||
text: JSON.stringify({ | ||
page: Number(challenges.headers.get('x-page')) || 1, | ||
pageSize: | ||
Number(challenges.headers.get('x-per-page')) || | ||
challengesData.length || | ||
0, | ||
total: | ||
Number(challenges.headers.get('x-total')) || | ||
challengesData.length || | ||
0, | ||
nextPage: challenges.headers.get('x-next-page') | ||
? Number(challenges.headers.get('x-next-page')) | ||
: null, | ||
data: challengesData, | ||
}), | ||
}, | ||
], | ||
structuredContent: { | ||
page: Number(challenges.headers.get('x-page')) || 1, | ||
type: 'text', | ||
text: JSON.stringify({ | ||
page: Number(challenges.headers['x-page']) || 1, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. medium |
||
pageSize: | ||
Number(challenges.headers.get('x-per-page')) || | ||
challengesData.length || | ||
Number(challenges.headers['x-per-page']) || | ||
(Array.isArray(challengesData) ? challengesData.length : 0) || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. medium |
||
0, | ||
total: | ||
Number(challenges.headers.get('x-total')) || | ||
challengesData.length || | ||
Number(challenges.headers['x-total']) || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. medium |
||
(Array.isArray(challengesData) ? challengesData.length : 0) || | ||
0, | ||
nextPage: challenges.headers.get('x-next-page') | ||
? Number(challenges.headers.get('x-next-page')) | ||
nextPage: challenges.headers['x-next-page'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. medium |
||
? Number(challenges.headers['x-next-page']) | ||
: null, | ||
data: challengesData, | ||
}), | ||
}, | ||
], | ||
structuredContent: { | ||
page: Number(challenges.headers['x-page']) || 1, | ||
pageSize: | ||
Number(challenges.headers['x-per-page']) || | ||
(Array.isArray(challengesData) ? challengesData.length : 0) || | ||
0, | ||
total: | ||
Number(challenges.headers['x-total']) || | ||
(Array.isArray(challengesData) ? challengesData.length : 0) || | ||
0, | ||
nextPage: challenges.headers['x-next-page'] | ||
? Number(challenges.headers['x-next-page']) | ||
: null, | ||
data: challengesData, | ||
}, | ||
}; | ||
} catch (error) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ import { ENV_CONFIG } from 'src/config'; | |
import { Logger } from 'src/shared/global'; | ||
import { QUERY_CHALLENGES_TOOL_PARAMETERS } from 'src/mcp/tools/challenges/queryChallenges.parameters'; | ||
import { z } from 'zod'; | ||
import axios from 'axios'; | ||
|
||
const { TOPCODER_API_BASE_URL } = ENV_CONFIG; | ||
|
||
|
@@ -40,10 +41,8 @@ export class TopcoderChallengesService { | |
); | ||
|
||
try { | ||
return await fetch(stringUrl, { | ||
method: 'GET', | ||
headers, | ||
}); | ||
const response = await axios.get(stringUrl, { headers }); | ||
return response; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. medium |
||
} catch (error) { | ||
this.logger.error(`Error fetching challenges: ${JSON.stringify(error)}`, error); | ||
throw error; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
low
readability
The condition
challenges.status < 200 || challenges.status >= 300
is technically correct for checking non-success HTTP status codes, but consider using!challenges.status.toString().startsWith('2')
for improved readability and to clearly convey the intent of checking for non-2xx status codes.