-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: api for updating custom status and location (#78)
- Loading branch information
1 parent
7e6e6bd
commit 462950f
Showing
19 changed files
with
314 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
import { StorageClient } from 'services/_server_'; | ||
|
||
export default async function handler(_: NextApiRequest, res: NextApiResponse) { | ||
res.setHeader('Content-Type', 'application/json'); | ||
|
||
try { | ||
const client = new StorageClient(); | ||
const { token } = await client.getSpotifyCredentials(); | ||
client.disconnect(); | ||
|
||
res.statusCode = 200; | ||
res.end(JSON.stringify({ success: !!token })); | ||
} catch (e) { | ||
res.statusCode = 500; | ||
res.end(JSON.stringify({ reason: JSON.stringify(e) })); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
import { authMiddleware, StorageClient, StorageKey } from 'services/_server_'; | ||
|
||
const handler = async (req: NextApiRequest, res: NextApiResponse) => { | ||
res.setHeader('Content-Type', 'application/json'); | ||
|
||
const newLocation = req.query['loc']; | ||
const newISODate = req.query['isoDate'] as string; | ||
|
||
if (newLocation && newISODate) { | ||
try { | ||
const [hourDiff, minDiff] = [ | ||
Number(newISODate.slice(-6, -3)), | ||
Number(newISODate.slice(-2)), | ||
]; | ||
|
||
// get minutes from milliseconds | ||
const offsetMins = hourDiff * 60 + minDiff; | ||
|
||
console.log( | ||
`Setting new location to ${newLocation} and offset to ${offsetMins}` | ||
); | ||
const client = new StorageClient(); | ||
const locOk = await client.set(StorageKey.CURRENT_CITY, newLocation); | ||
const dateOk = await client.set( | ||
StorageKey.CURRENT_UTC_OFFSET_MINS, | ||
offsetMins | ||
); | ||
client.disconnect(); | ||
|
||
res.statusCode = 200; | ||
res.end( | ||
JSON.stringify({ | ||
success: !!locOk && !!dateOk, | ||
location: newLocation, | ||
offset: offsetMins, | ||
}) | ||
); | ||
} catch (e) { | ||
res.statusCode = 500; | ||
res.end(JSON.stringify({ reason: JSON.stringify(e.msg) })); | ||
} | ||
} else { | ||
res.statusCode = 400; | ||
res.end(JSON.stringify({ reason: 'No date and/or location provided!' })); | ||
} | ||
}; | ||
|
||
export default authMiddleware(handler); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
import { authMiddleware, StorageClient, StorageKey } from 'services/_server_'; | ||
|
||
const handler = async (req: NextApiRequest, res: NextApiResponse) => { | ||
res.setHeader('Content-Type', 'application/json'); | ||
|
||
const status = req.query['status']; | ||
|
||
if (status) { | ||
try { | ||
const client = new StorageClient(); | ||
const statusSetOk = await client.set(StorageKey.STATUS, status); | ||
client.disconnect(); | ||
|
||
res.statusCode = 200; | ||
res.end(JSON.stringify({ success: !!statusSetOk, status })); | ||
} catch (e) { | ||
res.statusCode = 500; | ||
res.end(JSON.stringify({ reason: JSON.stringify(e) })); | ||
} | ||
} else { | ||
res.statusCode = 400; | ||
res.end(JSON.stringify({ reason: 'No status provided!' })); | ||
} | ||
}; | ||
|
||
export default authMiddleware(handler); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './now-playing'; | ||
export * from './middleware'; | ||
export * from './storage'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
const authMiddleware = (handler) => ( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) => { | ||
if (!req.query['token'] || req.query['token'] !== process.env.API_TOKEN) { | ||
res.statusCode = 401; | ||
res.statusMessage = 'Unauthorized'; | ||
res.end(); | ||
return false; | ||
} | ||
|
||
handler(req, res); | ||
}; | ||
|
||
export { authMiddleware }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { createCanvas, Image as CanvasImage } from 'canvas'; | ||
|
||
import { getNowPlaying } from 'services/now-playing'; | ||
|
||
const SERVER_SIDE_COLOR_OPTIONS = { | ||
canvasBuilder: () => createCanvas(64, 64), | ||
imageClass: CanvasImage, | ||
}; | ||
|
||
const getNowPlayingDataServerSide = async (accessToken: string) => | ||
getNowPlaying(accessToken, SERVER_SIDE_COLOR_OPTIONS); | ||
|
||
export { getNowPlayingDataServerSide }; |
Oops, something went wrong.