|
| 1 | +// HELPER FUNCTIONS TO USE THE OPENPROCESSING API |
| 2 | +// SEE https://documenter.getpostman.com/view/16936458/2s9YC1Xa6X#intro |
| 3 | + |
| 4 | +const openProcessingEndpoint = "https://openprocessing.org/api/"; |
| 5 | +/** |
| 6 | + * ID of the OpenProcessing Curation we pull sketches from. |
| 7 | + * Currently a placeholder (https://openprocessing.org/curation/78544/) |
| 8 | + */ |
| 9 | +const curationId = "78544"; |
| 10 | + |
| 11 | +/** |
| 12 | + * API Response from a call to the Curation Sketches endpoint |
| 13 | + * |
| 14 | + * see https://documenter.getpostman.com/view/16936458/2s9YC1Xa6X#7cd344f6-6e87-426a-969b-2b4a79701dd1 |
| 15 | + */ |
| 16 | +export type OpenProcessingCurationResponse = ReadonlyArray<{ |
| 17 | + /** Sketch ID used for constructing URLs */ |
| 18 | + visualID: string; |
| 19 | + /** Title of sketch */ |
| 20 | + title: string; |
| 21 | + /** Description of sketch */ |
| 22 | + description: string; |
| 23 | + userID: string; |
| 24 | + submittedOn: string; |
| 25 | + /** Author's name */ |
| 26 | + fullname: string; |
| 27 | +}>; |
| 28 | + |
| 29 | +/** |
| 30 | + * Get basic info for the sketches contained in a Curation |
| 31 | + * from the OpenProcessing API |
| 32 | + * |
| 33 | + * @param limit max number of sketches to return |
| 34 | + * @returns sketches |
| 35 | + */ |
| 36 | +export const getCurationSketches = async ( |
| 37 | + // TODO: Remove when we have real data |
| 38 | + limit: number = 25, |
| 39 | +): Promise<OpenProcessingCurationResponse> => { |
| 40 | + const limitParam = limit ? `limit=${limit}` : ""; |
| 41 | + const response = await fetch( |
| 42 | + `${openProcessingEndpoint}curation/${curationId}/sketches?${limitParam}`, |
| 43 | + ); |
| 44 | + const payload = await response.json(); |
| 45 | + return payload as OpenProcessingCurationResponse; |
| 46 | +}; |
| 47 | + |
| 48 | +/** |
| 49 | + * API Response from a call to the Sketch endpoint |
| 50 | + * |
| 51 | + * see https://documenter.getpostman.com/view/16936458/2s9YC1Xa6X#7cd344f6-6e87-426a-969b-2b4a79701dd1 |
| 52 | + */ |
| 53 | +export type OpenProcessingSketchResponse = { |
| 54 | + /** Sketch ID used for constructing URLs */ |
| 55 | + visualID: string; |
| 56 | + /** Title of sketch */ |
| 57 | + title: string; |
| 58 | + /** Description of sketch */ |
| 59 | + description: string; |
| 60 | + instructions: string; |
| 61 | + license: string; |
| 62 | + userID: string; |
| 63 | + submittedOn: string; |
| 64 | + createdOn: string; |
| 65 | +}; |
| 66 | + |
| 67 | +/** |
| 68 | + * Get info about a specific sketch from the OpenProcessing API |
| 69 | + * |
| 70 | + * https://documenter.getpostman.com/view/16936458/2s9YC1Xa6X#7cd344f6-6e87-426a-969b-2b4a79701dd1 |
| 71 | + * @param id |
| 72 | + * @returns |
| 73 | + */ |
| 74 | +export const getSketch = async ( |
| 75 | + id: string, |
| 76 | +): Promise<OpenProcessingSketchResponse> => { |
| 77 | + const response = await fetch(`${openProcessingEndpoint}sketch/${id}`); |
| 78 | + const payload = await response.json(); |
| 79 | + return payload as OpenProcessingSketchResponse; |
| 80 | +}; |
| 81 | + |
| 82 | +export const makeSketchLinkUrl = (id: string) => |
| 83 | + `https://openprocessing.org/sketch/${id}`; |
| 84 | + |
| 85 | +export const makeThumbnailUrl = (id: string) => |
| 86 | + `https://openprocessing-usercontent.s3.amazonaws.com/thumbnails/visualThumbnail${id}.jpg`; |
0 commit comments