Skip to content

Commit

Permalink
Enable using v2 of the API from the frontend via the 'api_v2' feature…
Browse files Browse the repository at this point in the history
… flag
  • Loading branch information
jfreda committed Oct 20, 2023
1 parent bb2643c commit 14ae928
Show file tree
Hide file tree
Showing 15 changed files with 203 additions and 112 deletions.
8 changes: 8 additions & 0 deletions configs/config.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ email {
from_address = "[email protected]"
}

// FeatureFlags contain available feature flags.
feature_flags {
// api_v2 enables v2 of the API.
flag "api_v2" {
enabled = false
}
}

// google_workspace configures Hermes to work with Google Workspace.
google_workspace {
// create_doc_shortcuts enables creating a shortcut in the shortcuts_folder
Expand Down
4 changes: 3 additions & 1 deletion web/app/authenticators/torii.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// @ts-ignore -- TODO: Add Types
import Torii from "ember-simple-auth/authenticators/torii";
import { inject as service } from "@ember/service";
import ConfigService from "hermes/services/config";
import FetchService from "hermes/services/fetch";

export default class ToriiAuthenticator extends Torii {
@service("config") declare configSvc: ConfigService;
@service("fetch") declare fetchSvc: FetchService;

// Appears unused, but necessary for the session service
Expand All @@ -16,7 +18,7 @@ export default class ToriiAuthenticator extends Torii {
* in the session being invalidated or remaining unauthenticated.
*/
return this.fetchSvc
.fetch("/api/v1/me", {
.fetch(`/api/${this.configSvc.config.api_version}/me`, {
method: "HEAD",
headers: {
"Hermes-Google-Access-Token": data.access_token,
Expand Down
13 changes: 9 additions & 4 deletions web/app/components/document/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { inject as service } from "@ember/service";
import { dropTask } from "ember-concurrency";
import { HermesDocument } from "hermes/types/document";
import { AuthenticatedUser } from "hermes/services/authenticated-user";
import ConfigService from "hermes/services/config";
import FetchService from "hermes/services/fetch";
import RouterService from "@ember/routing/router-service";
import FlashMessageService from "ember-cli-flash/services/flash-messages";
Expand All @@ -21,6 +22,7 @@ interface DocumentIndexComponentSignature {

export default class DocumentIndexComponent extends Component<DocumentIndexComponentSignature> {
@service declare authenticatedUser: AuthenticatedUser;
@service("config") declare configSvc: ConfigService;
@service("fetch") declare fetchSvc: FetchService;
@service declare router: RouterService;
@service declare flashMessages: FlashMessageService;
Expand All @@ -35,10 +37,13 @@ export default class DocumentIndexComponent extends Component<DocumentIndexCompo

protected deleteDraft = dropTask(async (docID: string) => {
try {
let fetchResponse = await this.fetchSvc.fetch("/api/v1/drafts/" + docID, {
method: "DELETE",
headers: { "Content-Type": "application/json" },
});
let fetchResponse = await this.fetchSvc.fetch(
`/api/${this.configSvc.config.api_version}/drafts/` + docID,
{
method: "DELETE",
headers: { "Content-Type": "application/json" },
},
);

if (!fetchResponse?.ok) {
this.showError(fetchResponse?.statusText);
Expand Down
85 changes: 54 additions & 31 deletions web/app/components/document/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,13 +544,16 @@ export default class DocumentSidebarComponent extends Component<DocumentSidebarC

shareButton.classList.add("out");

void this.fetchSvc.fetch(`/api/v1/drafts/${this.docID}/shareable`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
isShareable: false,
}),
});
void this.fetchSvc.fetch(
`/api/${this.configSvc.config.api_version}/drafts/${this.docID}/shareable`,
{
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
isShareable: false,
}),
},
);

// Give time for the link icon to animate out
await timeout(Ember.testing ? 0 : 300);
Expand All @@ -563,13 +566,16 @@ export default class DocumentSidebarComponent extends Component<DocumentSidebarC
this.newDraftVisibilityIcon = DraftVisibilityIcon.Shareable;
this._docIsShareable = true;

await this.fetchSvc.fetch(`/api/v1/drafts/${this.docID}/shareable`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
isShareable: true,
}),
});
await this.fetchSvc.fetch(
`/api/${this.configSvc.config.api_version}/drafts/${this.docID}/shareable`,
{
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
isShareable: true,
}),
},
);

// Kick off the timer for the "link created" state.
void this.showCreateLinkSuccessMessage.perform();
Expand Down Expand Up @@ -653,11 +659,14 @@ export default class DocumentSidebarComponent extends Component<DocumentSidebarC
const endpoint = this.isDraft ? "drafts" : "documents";

try {
await this.fetchSvc.fetch(`/api/v1/${endpoint}/${this.docID}`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(fields),
});
await this.fetchSvc.fetch(
`/api/${this.configSvc.config.api_version}/${endpoint}/${this.docID}`,
{
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(fields),
},
);
} catch (error: unknown) {
this.maybeShowFlashError(error as Error, "Unable to save document");
throw error;
Expand All @@ -673,9 +682,12 @@ export default class DocumentSidebarComponent extends Component<DocumentSidebarC
approvers: this.approvers?.compact().mapBy("email"),
});

await this.fetchSvc.fetch(`/api/v1/reviews/${this.docID}`, {
method: "POST",
});
await this.fetchSvc.fetch(
`/api/${this.configSvc.config.api_version}/reviews/${this.docID}`,
{
method: "POST",
},
);

this.router.transitionTo({
queryParams: { draft: false },
Expand Down Expand Up @@ -788,8 +800,13 @@ export default class DocumentSidebarComponent extends Component<DocumentSidebarC
*/
private getDraftPermissions = task(async () => {
try {
let apiVersion = "v1";
if (this.configSvc.config.feature_flags["api_v2"]) {
apiVersion = "v2";
}

const response = await this.fetchSvc
.fetch(`/api/v1/drafts/${this.docID}/shareable`)
.fetch(`/api/${apiVersion}/drafts/${this.docID}/shareable`)
.then((response) => response?.json());
if (response?.isShareable) {
this._docIsShareable = true;
Expand All @@ -799,10 +816,13 @@ export default class DocumentSidebarComponent extends Component<DocumentSidebarC

approve = task(async () => {
try {
await this.fetchSvc.fetch(`/api/v1/approvals/${this.docID}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
});
await this.fetchSvc.fetch(
`/api/${this.configSvc.config.api_version}/approvals/${this.docID}`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
},
);
this.showFlashSuccess("Done!", "Document approved");
} catch (error: unknown) {
this.maybeShowFlashError(error as Error, "Unable to approve");
Expand All @@ -814,10 +834,13 @@ export default class DocumentSidebarComponent extends Component<DocumentSidebarC

requestChanges = task(async () => {
try {
await this.fetchSvc.fetch(`/api/v1/approvals/${this.docID}`, {
method: "DELETE",
headers: { "Content-Type": "application/json" },
});
await this.fetchSvc.fetch(
`/api/${this.configSvc.config.api_version}/approvals/${this.docID}`,
{
method: "DELETE",
headers: { "Content-Type": "application/json" },
},
);
// Add a notification for the user
let msg = "Requested changes for document";
// FRDs are a special case that can be approved or not approved.
Expand Down
12 changes: 6 additions & 6 deletions web/app/components/document/sidebar/related-resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@ export default class DocumentSidebarRelatedResourcesComponent extends Component<
try {
const resources = await this.fetchSvc
.fetch(
`/api/v1/${this.args.documentIsDraft ? "drafts" : "documents"}/${
this.args.objectID
}/related-resources`,
`/api/${this.configSvc.config.api_version}/${
this.args.documentIsDraft ? "drafts" : "documents"
}/${this.args.objectID}/related-resources`,
)
.then((response) => response?.json());

Expand Down Expand Up @@ -315,9 +315,9 @@ export default class DocumentSidebarRelatedResourcesComponent extends Component<

try {
await this.fetchSvc.fetch(
`/api/v1/${this.args.documentIsDraft ? "drafts" : "documents"}/${
this.args.objectID
}/related-resources`,
`/api/${this.configSvc.config.api_version}/${
this.args.documentIsDraft ? "drafts" : "documents"
}/${this.args.objectID}/related-resources`,
{
method: "PUT",
body: JSON.stringify(this.formattedRelatedResources),
Expand Down
19 changes: 12 additions & 7 deletions web/app/components/inputs/people-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { tracked } from "@glimmer/tracking";
import { inject as service } from "@ember/service";
import { restartableTask, timeout } from "ember-concurrency";
import { action } from "@ember/object";
import ConfigService from "hermes/services/config";
import FetchService from "hermes/services/fetch";
import { HermesUser } from "hermes/types/document";
import Ember from "ember";
Expand All @@ -27,6 +28,7 @@ const MAX_RETRIES = 3;
const INITIAL_RETRY_DELAY = Ember.testing ? 0 : 500;

export default class InputsPeopleSelectComponent extends Component<InputsPeopleSelectComponentSignature> {
@service("config") declare configSvc: ConfigService;
@service("fetch") declare fetchSvc: FetchService;

/**
Expand Down Expand Up @@ -65,13 +67,16 @@ export default class InputsPeopleSelectComponent extends Component<InputsPeopleS
let retryDelay = INITIAL_RETRY_DELAY;

try {
let response = await this.fetchSvc.fetch("/api/v1/people", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
query: query,
}),
});
let response = await this.fetchSvc.fetch(
`/api/${this.configSvc.config.api_version}/people`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
query: query,
}),
},
);

const peopleJson = await response?.json();

Expand Down
6 changes: 4 additions & 2 deletions web/app/components/new/doc-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { task, timeout } from "ember-concurrency";
import { inject as service } from "@ember/service";
import { tracked } from "@glimmer/tracking";
import { action } from "@ember/object";
import ConfigService from "hermes/services/config";
import Ember from "ember";
import FetchService from "hermes/services/fetch";
import AuthenticatedUserService from "hermes/services/authenticated-user";
Expand All @@ -28,7 +29,7 @@ const FORM_ERRORS: DocFormErrors = {
contributors: null,
};

const AWAIT_DOC_DELAY = Ember.testing ? 0 : 2000;
const AWAIT_DOC_DELAY = Ember.testing ? 0 : 1000;
const AWAIT_DOC_CREATED_MODAL_DELAY = Ember.testing ? 0 : 1500;

interface NewDocFormComponentSignature {
Expand All @@ -38,6 +39,7 @@ interface NewDocFormComponentSignature {
}

export default class NewDocFormComponent extends Component<NewDocFormComponentSignature> {
@service("config") declare configSvc: ConfigService;
@service("fetch") declare fetchSvc: FetchService;
@service declare authenticatedUser: AuthenticatedUserService;
@service declare flashMessages: FlashService;
Expand Down Expand Up @@ -195,7 +197,7 @@ export default class NewDocFormComponent extends Component<NewDocFormComponentSi

try {
const doc = await this.fetchSvc
.fetch("/api/v1/drafts", {
.fetch(`/api/${this.configSvc.config.api_version}/drafts`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
Expand Down
4 changes: 2 additions & 2 deletions web/app/routes/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default class ApplicationRoute extends Route {
JSON.stringify({
url: transitionTo,
expiresOn: Date.now() + 60 * 5000, // 5 minutes
})
}),
);
}

Expand All @@ -68,7 +68,7 @@ export default class ApplicationRoute extends Route {
this.flags.initialize();

await this.fetchSvc
.fetch("/api/v1/web/config")
.fetch(`/api/${this.config.config.api_version}/web/config`)
.then((response) => response?.json())
.then((json) => {
this.config.setConfig(json);
Expand Down
Loading

0 comments on commit 14ae928

Please sign in to comment.