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

API: use APIv3 endpoint for resources #468

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { getReadTheDocsConfig } from "./readthedocs-config";
import {
getReadTheDocsConfig,
getReadTheDocsConfigUsingAPIv3,
} from "./readthedocs-config";
import * as notification from "./notification";
import * as analytics from "./analytics";
import * as search from "./search";
Expand Down Expand Up @@ -45,7 +48,8 @@ export function setup() {
}
}

return getReadTheDocsConfig(sendUrlParam);
// return getReadTheDocsConfig(sendUrlParam);
return getReadTheDocsConfigUsingAPIv3(sendUrlParam);
})
.then((config) => {
const loadWhenEmbedded = objectPath.get(
Expand Down
69 changes: 69 additions & 0 deletions src/readthedocs-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,75 @@ export function getReadTheDocsConfig(sendUrlParam) {
});
}

export async function getReadTheDocsConfigUsingAPIv3(sendUrlParam) {
const defaultApiUrl = _getApiUrl(sendUrlParam, ADDONS_API_VERSION);
const addonsResponse = fetch(defaultApiUrl);

// TODO: get the project/version slug from the META tags
const projectResponse = fetch("/_/api/v3/projects/test-builds/");
const translationsResponse = fetch(
"/_/api/v3/projects/test-builds/translations/",
);
const versionResponse = fetch(
"/_/api/v3/projects/test-builds/versions/full-feature/",
);
const activeVersionsResponse = fetch(
"/_/api/v3/projects/test-builds/versions/?active=true",
);
const buildResponse = fetch("/_/api/v3/projects/test-builds/builds/3111/");
// TODO: use `?base-version=` coming from `addons.options.base_version`
const filetreediffResponse = fetch(
"/_/api/v3/projects/test-builds/versions/2109/filetreediff/?base-version=latest",
);

const responses = await Promise.all([
addonsResponse,
projectResponse,
translationsResponse,
versionResponse,
activeVersionsResponse,
buildResponse,
filetreediffResponse,
]);

const [
addons,
project,
translations,
version,
activeVersions,
build,
filetreediff,
] = await Promise.all(responses.map((response) => response.json()));

// TODO: we are missing the data from the `/_/addons/` endpoint that are not resources.
// We need to perform another request for that.
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);
Expand Down