mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-26 15:48:32 -06:00
made a request to get assignments
This commit is contained in:
@@ -1,9 +1,14 @@
|
|||||||
|
import { canvasAssignmentService } from "@/services/canvas/canvasAssignmentService";
|
||||||
import Image from "next/image";
|
import Image from "next/image";
|
||||||
|
|
||||||
export default function Home() {
|
export default async function Home() {
|
||||||
|
const assignments = await canvasAssignmentService.getAll(960410);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main className="flex min-h-screen flex-col items-center justify-between p-24">
|
<main className="flex min-h-screen flex-col items-center justify-between p-24">
|
||||||
|
{assignments.map((assignment) => (
|
||||||
|
<div key={assignment.id}>{assignment.name}</div>
|
||||||
|
))}
|
||||||
</main>
|
</main>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
18
nextjs/src/services/canvas/canvasAssignmentService.ts
Normal file
18
nextjs/src/services/canvas/canvasAssignmentService.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import { CanvasAssignment } from "@/models/canvas/assignments/canvasAssignment";
|
||||||
|
import { canvasServiceUtils } from "./canvasServiceUtils";
|
||||||
|
|
||||||
|
export const canvasAssignmentService = {
|
||||||
|
async getAll(courseId: number): Promise<CanvasAssignment[]> {
|
||||||
|
const url = `courses/${courseId}/assignments`;
|
||||||
|
const assignments = await canvasServiceUtils.paginatedRequest<
|
||||||
|
CanvasAssignment[]
|
||||||
|
>({ url });
|
||||||
|
return assignments.flatMap((assignments) =>
|
||||||
|
assignments.map((a) => ({
|
||||||
|
...a,
|
||||||
|
due_at: a.due_at ? new Date(a.due_at).toLocaleString() : undefined, // timezones?
|
||||||
|
lock_at: a.lock_at ? new Date(a.lock_at).toLocaleString() : undefined, // timezones?
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
},
|
||||||
|
};
|
||||||
59
nextjs/src/services/canvas/canvasServiceUtils.ts
Normal file
59
nextjs/src/services/canvas/canvasServiceUtils.ts
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
// services/canvasServiceUtils.ts
|
||||||
|
|
||||||
|
import { webRequestor } from "./webRequestor";
|
||||||
|
|
||||||
|
const BASE_URL = "https://snow.instructure.com/api/v1/";
|
||||||
|
|
||||||
|
const getNextUrl = (headers: Headers): string | undefined => {
|
||||||
|
const linkHeader = headers.get("Link");
|
||||||
|
if (!linkHeader) return undefined;
|
||||||
|
|
||||||
|
const links = linkHeader.split(",").map((link) => link.trim());
|
||||||
|
const nextLink = links.find((link) => link.includes('rel="next"'));
|
||||||
|
|
||||||
|
if (!nextLink) return undefined;
|
||||||
|
|
||||||
|
const nextUrl = nextLink.split(";")[0].trim().slice(1, -1);
|
||||||
|
return nextUrl.replace(BASE_URL, "").trim();
|
||||||
|
};
|
||||||
|
|
||||||
|
export const canvasServiceUtils = {
|
||||||
|
async paginatedRequest<T>(request: { url: string }): Promise<T[]> {
|
||||||
|
var requestCount = 1;
|
||||||
|
const url = new URL(request.url!, BASE_URL);
|
||||||
|
url.searchParams.set("per_page", "100");
|
||||||
|
|
||||||
|
const [firstData, firstResponse] = await webRequestor.get<T>(
|
||||||
|
url.toString()
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!firstResponse.ok) {
|
||||||
|
console.error(
|
||||||
|
"error in response",
|
||||||
|
firstResponse.statusText,
|
||||||
|
firstResponse.body
|
||||||
|
);
|
||||||
|
throw new Error("error in response");
|
||||||
|
}
|
||||||
|
|
||||||
|
var returnData: T[] = firstData ? [firstData] : [];
|
||||||
|
var nextUrl = getNextUrl(firstResponse.headers);
|
||||||
|
|
||||||
|
while (nextUrl) {
|
||||||
|
requestCount += 1;
|
||||||
|
const [data, response] = await webRequestor.get<T>(nextUrl);
|
||||||
|
if (data) {
|
||||||
|
returnData = [...returnData, data];
|
||||||
|
}
|
||||||
|
nextUrl = getNextUrl(response.headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (requestCount > 1) {
|
||||||
|
console.log(
|
||||||
|
`Requesting ${typeof returnData} took ${requestCount} requests`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return returnData;
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
type FetchOptions = Omit<RequestInit, "method">;
|
type FetchOptions = Omit<RequestInit, "method">;
|
||||||
|
|
||||||
const token = process.env.CANVAS_TOKEN;
|
const token = process.env.CANVAS_TOKEN;
|
||||||
@@ -20,7 +19,7 @@ const isRateLimited = async (response: Response): Promise<boolean> => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const deserialize = async <T>(response: Response): Promise<T | null> => {
|
const deserialize = async <T>(response: Response): Promise<T | undefined> => {
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
console.error(`Error with response to ${response.url} ${response.status}`);
|
console.error(`Error with response to ${response.url} ${response.status}`);
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@@ -112,11 +111,11 @@ const recursiveDeleteAsync = async (
|
|||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
export const WebRequestor = {
|
export const webRequestor = {
|
||||||
getManyAsync: async <T>(
|
getMany: async <T>(
|
||||||
url: string,
|
url: string,
|
||||||
options: FetchOptions = {}
|
options: FetchOptions = {}
|
||||||
): Promise<[T[] | null, Response]> => {
|
): Promise<[T[] | undefined, Response]> => {
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
...options,
|
...options,
|
||||||
method: "GET",
|
method: "GET",
|
||||||
@@ -128,10 +127,10 @@ export const WebRequestor = {
|
|||||||
return [await deserialize<T[]>(response), response];
|
return [await deserialize<T[]>(response), response];
|
||||||
},
|
},
|
||||||
|
|
||||||
getAsync: async <T>(
|
get: async <T>(
|
||||||
url: string,
|
url: string,
|
||||||
options: FetchOptions = {}
|
options: FetchOptions = {}
|
||||||
): Promise<[T | null, Response]> => {
|
): Promise<[T | undefined, Response]> => {
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
...options,
|
...options,
|
||||||
method: "GET",
|
method: "GET",
|
||||||
@@ -143,25 +142,19 @@ export const WebRequestor = {
|
|||||||
return [await deserialize<T>(response), response];
|
return [await deserialize<T>(response), response];
|
||||||
},
|
},
|
||||||
|
|
||||||
postAsync: async (
|
post: async (url: string, options: FetchOptions = {}): Promise<Response> => {
|
||||||
url: string,
|
|
||||||
options: FetchOptions = {}
|
|
||||||
): Promise<Response> => {
|
|
||||||
return await rateLimitAwarePostAsync(url, options);
|
return await rateLimitAwarePostAsync(url, options);
|
||||||
},
|
},
|
||||||
|
|
||||||
postAsyncWithDeserialize: async <T>(
|
postWithDeserialize: async <T>(
|
||||||
url: string,
|
url: string,
|
||||||
options: FetchOptions = {}
|
options: FetchOptions = {}
|
||||||
): Promise<[T | null, Response]> => {
|
): Promise<[T | undefined, Response]> => {
|
||||||
const response = await rateLimitAwarePostAsync(url, options);
|
const response = await rateLimitAwarePostAsync(url, options);
|
||||||
return [await deserialize<T>(response), response];
|
return [await deserialize<T>(response), response];
|
||||||
},
|
},
|
||||||
|
|
||||||
putAsync: async (
|
put: async (url: string, options: FetchOptions = {}): Promise<Response> => {
|
||||||
url: string,
|
|
||||||
options: FetchOptions = {}
|
|
||||||
): Promise<Response> => {
|
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
...options,
|
...options,
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
@@ -174,10 +167,10 @@ export const WebRequestor = {
|
|||||||
return response;
|
return response;
|
||||||
},
|
},
|
||||||
|
|
||||||
putAsyncWithDeserialize: async <T>(
|
putWithDeserialize: async <T>(
|
||||||
url: string,
|
url: string,
|
||||||
options: FetchOptions = {}
|
options: FetchOptions = {}
|
||||||
): Promise<[T | null, Response]> => {
|
): Promise<[T | undefined, Response]> => {
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
...options,
|
...options,
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
@@ -190,7 +183,7 @@ export const WebRequestor = {
|
|||||||
return [await deserialize<T>(response), response];
|
return [await deserialize<T>(response), response];
|
||||||
},
|
},
|
||||||
|
|
||||||
deleteAsync: async (
|
delete: async (
|
||||||
url: string,
|
url: string,
|
||||||
options: FetchOptions = {}
|
options: FetchOptions = {}
|
||||||
): Promise<Response> => {
|
): Promise<Response> => {
|
||||||
Reference in New Issue
Block a user