From 8594b7e8f3058a8d441413a033aee311ee59bdfd Mon Sep 17 00:00:00 2001 From: Melissa Zhang Date: Fri, 10 Jul 2020 16:26:08 -0700 Subject: hypothes.is authentication is fully functional, no longer need to manually input username/api key --- src/client/apis/hypothesis/HypothesisApiUtils.ts | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'src/client/apis/hypothesis') diff --git a/src/client/apis/hypothesis/HypothesisApiUtils.ts b/src/client/apis/hypothesis/HypothesisApiUtils.ts index 9ba481c8c..83881cd89 100644 --- a/src/client/apis/hypothesis/HypothesisApiUtils.ts +++ b/src/client/apis/hypothesis/HypothesisApiUtils.ts @@ -1,6 +1,10 @@ import { StrCast } from "../../../fields/Types"; +import HypothesisAuthenticationManager from "../HypothesisAuthenticationManager"; export namespace Hypothesis { + + const getCredentials = async () => HypothesisAuthenticationManager.Instance.fetchAccessToken(); + export const fetchAnnotation = async (annotationId: string) => { const response = await fetch(`https://api.hypothes.is/api/annotations/${annotationId}`); if (response.ok) { @@ -11,12 +15,12 @@ export namespace Hypothesis { }; /** - * Searches for annotations made by @param username that - * contain @param searchKeyWord + * Searches for annotations authored by the current user that contain @param searchKeyWord */ - export const searchAnnotation = async (username: string, searchKeyWord: string) => { + export const searchAnnotation = async (searchKeyWord: string) => { + const credentials = await getCredentials(); const base = 'https://api.hypothes.is/api/search'; - const request = base + `?user=acct:${username}@hypothes.is&text=${searchKeyWord}`; + const request = base + `?user=acct:${credentials.username}@hypothes.is&text=${searchKeyWord}`; console.log("DASH Querying " + request); const response = await fetch(request); if (response.ok) { @@ -40,8 +44,8 @@ export namespace Hypothesis { }; // Find the most recent placeholder annotation created, and return its ID - export const getPlaceholderId = async (username: string, searchKeyWord: string) => { - const getResponse = await Hypothesis.searchAnnotation(username, searchKeyWord); + export const getPlaceholderId = async (searchKeyWord: string) => { + const getResponse = await Hypothesis.searchAnnotation(searchKeyWord); const id = getResponse.rows.length > 0 ? getResponse.rows[0].id : undefined; const uri = getResponse.rows.length > 0 ? getResponse.rows[0].uri : undefined; return id ? { id, uri } : undefined; @@ -49,8 +53,7 @@ export namespace Hypothesis { // Send request to Hypothes.is client to modify a placeholder annotation into a hyperlink to Dash export const dispatchLinkRequest = async (title: string, url: string, annotationId: string) => { - const apiKey = "6879-DnMTKjWjnnLPa0Php7f5Ra2kunZ_X0tMRDbTF220_q0"; - + const credentials = await getCredentials(); const oldAnnotation = await fetchAnnotation(annotationId); const oldText = StrCast(oldAnnotation.text); const newHyperlink = `[${title}\n](${url})`; @@ -58,7 +61,7 @@ export namespace Hypothesis { console.log("DASH dispatching linkRequest"); document.dispatchEvent(new CustomEvent<{ newText: string, id: string, apiKey: string }>("linkRequest", { - detail: { newText: newText, id: annotationId, apiKey: apiKey }, + detail: { newText: newText, id: annotationId, apiKey: credentials.apiKey }, bubbles: true })); }; -- cgit v1.2.3-70-g09d2