Skip to content

Commit

Permalink
API key
Browse files Browse the repository at this point in the history
  • Loading branch information
jm42 committed May 11, 2023
1 parent 73aaf82 commit 3263af9
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -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=
84 changes: 74 additions & 10 deletions src/loaders/api.js
Original file line number Diff line number Diff line change
@@ -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 }),
Expand All @@ -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
}
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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})`)
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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),
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/models/api.js
Original file line number Diff line number Diff line change
@@ -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 }

0 comments on commit 3263af9

Please sign in to comment.