-
Notifications
You must be signed in to change notification settings - Fork 20
API: use APIv3 endpoint for resources #468
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
a18ca99
ddeb084
d19d740
b539256
e6e8309
737d6f7
cd037de
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 |
---|---|---|
|
@@ -129,6 +129,71 @@ export function getReadTheDocsConfig(sendUrlParam) { | |
}); | ||
} | ||
|
||
export async function getReadTheDocsConfigUsingAPIv3(sendUrlParam) { | ||
const defaultApiUrl = _getApiUrl(sendUrlParam, ADDONS_API_VERSION); | ||
const addons = await (await fetch(defaultApiUrl)).json(); | ||
|
||
const projectResponse = fetch( | ||
addons.readthedocs.urls.api.v3.projects.current, | ||
); | ||
const translationsResponse = fetch( | ||
addons.readthedocs.urls.api.v3.projects.translations, | ||
); | ||
|
||
const versionResponse = fetch( | ||
addons.readthedocs.urls.api.v3.versions.current, | ||
); | ||
// TODO: the results come paginated, so we are only seeing the first 10 results. | ||
// We need to perform more requests to get all the resulsts. | ||
// What's the correct way to do that? | ||
// | ||
// The response comes with a `next` attribute that has the URL for the next 10 results. | ||
const activeVersionsResponse = fetch( | ||
addons.readthedocs.urls.api.v3.versions.active, | ||
); | ||
const buildResponse = fetch(addons.readthedocs.urls.api.v3.builds.current); | ||
const filetreediffResponse = fetch( | ||
addons.readthedocs.urls.api.v3.filetreediff, | ||
); | ||
Comment on lines
+150
to
+152
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. I worry a little bit about one of the API calls failing and leading to weird state. Would be good to put a TODO in here probably to do a retry? 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. Yes. I need to re-write this code to handle errors as well. Currently, if an API call break, the whole code breaks completely 😅 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. @agjohnson what's the best pattern to use for this chunk of code? async/await My goal here is to make multiple requests in parallel to APIv3 while handling errors. Besides, once that all requests have responded, I want to use the respond data to form the 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. What you have is close, though yeah it does need more error handling -- especially using One thing though is that this is sort of written more like synchronous code. Normally, you'd want to take advantage of the evented code flow more, and instead of doing operations in bulk and That is, this is executing:
Where a fully evented flow is more like:
The effect can be subtle, but it does allow for early failure if one request or request response is bad. What you have here overall is good so far, though I think through restructuring to add some error handling and simplifying things, you'll probably end up with a separate async function for a single request and request encoding. |
||
|
||
const responses = await Promise.all([ | ||
projectResponse, | ||
translationsResponse, | ||
versionResponse, | ||
activeVersionsResponse, | ||
buildResponse, | ||
filetreediffResponse, | ||
]); | ||
|
||
const [project, translations, version, activeVersions, build, filetreediff] = | ||
await Promise.all(responses.map((response) => response.json())); | ||
|
||
Object.assign(addons, { | ||
builds: { | ||
current: build, | ||
}, | ||
projects: { | ||
current: project, | ||
translations: translations.results, | ||
}, | ||
versions: { | ||
active: activeVersions.results, | ||
current: version, | ||
}, | ||
}); | ||
|
||
Object.assign(addons["addons"]["filetreediff"], filetreediff); | ||
|
||
// Trigger the addons data ready CustomEvent to with the data the user is expecting. | ||
dispatchEvent( | ||
EVENT_READTHEDOCS_ADDONS_DATA_READY, | ||
document, | ||
new ReadTheDocsEventData(addons), | ||
); | ||
|
||
return addons; | ||
} | ||
|
||
function dispatchEvent(eventName, element, data) { | ||
const event = new CustomEvent(eventName, { detail: data }); | ||
element.dispatchEvent(event); | ||
|
Uh oh!
There was an error while loading. Please reload this page.