From a7d687d278c917fce1f1aeac6de1f53952e3f78e Mon Sep 17 00:00:00 2001 From: Juan M Date: Thu, 11 May 2023 15:27:04 -0300 Subject: [PATCH 1/3] Rename cache server down by unavailable --- src/components/LastEvents.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/LastEvents.js b/src/components/LastEvents.js index d74cb1e..13741f5 100644 --- a/src/components/LastEvents.js +++ b/src/components/LastEvents.js @@ -40,7 +40,7 @@ function LastEvents({ currentPage = 1, onPageChange = (page) => {}, qty = 3, sho setLoading(false) }, (err) => { - const error = new Error('Cache server down') + const error = new Error('Unavailable') error.reason = err console.error(err) setError(error) @@ -73,7 +73,7 @@ function LastEvents({ currentPage = 1, onPageChange = (page) => {}, qty = 3, sho setLoading(false) }, (err) => { - const error = new Error('Cache server down') + const error = new Error('Unavailable') error.reason = err console.error(err) setError(error) From 3263af9b66234708cb7cdf885490d2ff0be5b04e Mon Sep 17 00:00:00 2001 From: Juan M Date: Thu, 11 May 2023 15:27:35 -0300 Subject: [PATCH 2/3] API key --- .env.template | 3 ++ src/loaders/api.js | 84 ++++++++++++++++++++++++++++++++++++++++------ src/models/api.js | 3 +- 3 files changed, 79 insertions(+), 11 deletions(-) diff --git a/.env.template b/.env.template index 7d370f3..83078a9 100644 --- a/.env.template +++ b/.env.template @@ -7,3 +7,6 @@ REACT_APP_INFURA_API_KEY= # Request API access and include POAP API key # https://documentation.poap.tech/docs/api-access REACT_APP_POAP_API_KEY= + +# Family cache server is not required +REACT_APP_FAMILY_API_KEY= diff --git a/src/loaders/api.js b/src/loaders/api.js index 5a9e517..00b5d27 100644 --- a/src/loaders/api.js +++ b/src/loaders/api.js @@ -1,11 +1,15 @@ import axios from 'axios' -import { FAMILY_API_URL } from '../models/api' +import { FAMILY_API_KEY, FAMILY_API_URL } from '../models/api' import { encodeExpiryDates, Event } from '../models/event' async function putEventAndOwners(event, owners) { + if (!FAMILY_API_KEY) { + return + } const response = await fetch(`${FAMILY_API_URL}/event/${event.id}`, { method: 'PUT', headers: { + 'x-api-key': FAMILY_API_KEY, 'content-type': 'application/json', }, body: JSON.stringify({ event, owners }), @@ -16,7 +20,14 @@ async function putEventAndOwners(event, owners) { } async function getEventAndOwners(eventId, includeMetrics = true) { - const response = await fetch(`${FAMILY_API_URL}/event/${eventId}?metrics=${encodeURIComponent(includeMetrics)}`) + if (!FAMILY_API_KEY) { + return null + } + const response = await fetch(`${FAMILY_API_URL}/event/${eventId}?metrics=${encodeURIComponent(includeMetrics)}`, { + headers: { + 'x-api-key': FAMILY_API_KEY, + }, + }) if (response.status === 404) { return null } @@ -72,9 +83,13 @@ async function getEventAndOwners(eventId, includeMetrics = true) { } async function patchEvents(events) { + if (!FAMILY_API_KEY) { + return + } const response = await fetch(`${FAMILY_API_URL}/events`, { method: 'PATCH', headers: { + 'x-api-key': FAMILY_API_KEY, 'content-type': 'application/json', }, body: JSON.stringify(events), @@ -85,9 +100,13 @@ async function patchEvents(events) { } async function putEventInCommon(eventId, inCommon) { + if (!FAMILY_API_KEY) { + return + } const response = await fetch(`${FAMILY_API_URL}/event/${eventId}/in-common`, { method: 'PUT', headers: { + 'x-api-key': FAMILY_API_KEY, 'content-type': 'application/json', }, body: JSON.stringify(inCommon), @@ -98,8 +117,14 @@ async function putEventInCommon(eventId, inCommon) { } async function getInCommonEvents(eventId, abortSignal) { + if (!FAMILY_API_KEY) { + return null + } const response = await fetch(`${FAMILY_API_URL}/event/${eventId}/in-common`, { signal: abortSignal instanceof AbortSignal ? abortSignal : null, + headers: { + 'x-api-key': FAMILY_API_KEY, + }, }) if (response.status === 404) { return null @@ -128,10 +153,16 @@ async function getInCommonEvents(eventId, abortSignal) { } async function getInCommonEventsWithProgress(eventId, abortSignal, onProgress) { + if (!FAMILY_API_KEY) { + return null + } try { const response = await axios.get(`${FAMILY_API_URL}/event/${eventId}/in-common`, { signal: abortSignal instanceof AbortSignal ? abortSignal : undefined, onDownloadProgress: onProgress, + headers: { + 'x-api-key': FAMILY_API_KEY, + }, }) if (response.status === 404) { return null @@ -168,7 +199,14 @@ async function getInCommonEventsWithProgress(eventId, abortSignal, onProgress) { } async function getLastEvents(page = 1, qty = 3) { - const response = await fetch(`${FAMILY_API_URL}/events/last?page=${encodeURIComponent(page)}&qty=${encodeURIComponent(qty)}`) + if (!FAMILY_API_KEY) { + return null + } + const response = await fetch(`${FAMILY_API_URL}/events/last?page=${encodeURIComponent(page)}&qty=${encodeURIComponent(qty)}`, { + headers: { + 'x-api-key': FAMILY_API_KEY, + }, + }) if (response.status !== 200) { throw new Error(`Last events failed to fetch (status ${response.status})`) } @@ -189,7 +227,14 @@ async function getLastEvents(page = 1, qty = 3) { } async function getEvents(eventIds) { - const response = await fetch(`${FAMILY_API_URL}/events/${eventIds.map((eventId) => encodeURIComponent(eventId)).join(',')}`) + if (!FAMILY_API_KEY) { + return null + } + const response = await fetch(`${FAMILY_API_URL}/events/${eventIds.map((eventId) => encodeURIComponent(eventId)).join(',')}`, { + headers: { + 'x-api-key': FAMILY_API_KEY, + }, + }) if (response.status === 404) { return null } @@ -216,13 +261,16 @@ async function getEvents(eventIds) { } async function getEventsOwners(eventIds, abortSignal, expiryDates) { + if (!FAMILY_API_KEY) { + return null + } const queryString = expiryDates ? encodeExpiryDates(expiryDates) : '' - const response = await fetch( - `${FAMILY_API_URL}/events/${eventIds.map((eventId) => encodeURIComponent(eventId)).join(',')}/owners${queryString ? `?${queryString}` : ''}`, - { - signal: abortSignal instanceof AbortSignal ? abortSignal : null, - } - ) + const response = await fetch(`${FAMILY_API_URL}/events/${eventIds.map((eventId) => encodeURIComponent(eventId)).join(',')}/owners${queryString ? `?${queryString}` : ''}`, { + signal: abortSignal instanceof AbortSignal ? abortSignal : null, + headers: { + 'x-api-key': FAMILY_API_KEY, + }, + }) if (response.status === 404) { return null } @@ -249,9 +297,13 @@ async function getEventsOwners(eventIds, abortSignal, expiryDates) { } async function putEventOwners(eventId, owners) { + if (!FAMILY_API_KEY) { + return + } const response = await fetch(`${FAMILY_API_URL}/event/${eventId}/owners`, { method: 'PUT', headers: { + 'x-api-key': FAMILY_API_KEY, 'content-type': 'application/json', }, body: JSON.stringify(owners), @@ -262,8 +314,14 @@ async function putEventOwners(eventId, owners) { } async function getEventMetrics(eventId, abortSignal, refresh = false) { + if (!FAMILY_API_KEY) { + throw new Error(`Event ${eventId} metrics could not be fetched, configure Family API key`) + } const response = await fetch(`${FAMILY_API_URL}/event/${eventId}/metrics?refresh=${encodeURIComponent(refresh)}`, { signal: abortSignal instanceof AbortSignal ? abortSignal : null, + headers: { + 'x-api-key': FAMILY_API_KEY, + }, }) if (response.status === 404) { return null @@ -291,8 +349,14 @@ async function getEventMetrics(eventId, abortSignal, refresh = false) { } async function getEventsMetrics(eventIds, abortSignal) { + if (!FAMILY_API_KEY) { + throw new Error(`Events (${eventIds.length}) metrics could not be fetched, configure Family API key`) + } const response = await fetch(`${FAMILY_API_URL}/events/${eventIds.map((eventId) => encodeURIComponent(eventId)).join(',')}/metrics`, { signal: abortSignal instanceof AbortSignal ? abortSignal : null, + headers: { + 'x-api-key': FAMILY_API_KEY, + }, }) if (response.status === 404) { return null diff --git a/src/models/api.js b/src/models/api.js index 153a4f1..640296b 100644 --- a/src/models/api.js +++ b/src/models/api.js @@ -1,3 +1,4 @@ const FAMILY_API_URL = process.env.REACT_APP_FAMILY_API_URL ?? 'https://api.poap.family' +const FAMILY_API_KEY = process.env.REACT_APP_FAMILY_API_KEY -export { FAMILY_API_URL } +export { FAMILY_API_URL, FAMILY_API_KEY } From 97b06b40409ac79727f622fee6a4d4302a69f3e7 Mon Sep 17 00:00:00 2001 From: Juan M Date: Thu, 11 May 2023 15:30:42 -0300 Subject: [PATCH 3/3] Version 1.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c321f21..479258a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@poap-xyz/poap-family", - "version": "1.1.10", + "version": "1.2.0", "author": { "name": "POAP", "url": "https://poap.xyz"