generated from SteamDeckHomebrew/Plugin-Template
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
392 additions
and
10 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,16 @@ | ||
export interface TaskQueryResponse { | ||
id: string; | ||
name: string; | ||
status: string; | ||
completed: Date; | ||
started: Date; | ||
success: boolean; | ||
} | ||
|
||
export interface ZipSubmitRequest { | ||
blob: string; | ||
description: string; | ||
privateSubmission: boolean; | ||
imageBlobs: string[]; | ||
target?: string; | ||
} |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from "./CSSThemeTypes"; | ||
export * from "./AccountData"; | ||
export * from "./BlobTypes"; | ||
export * from "./Motd"; | ||
export * from "./SubmissionTypes"; |
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,100 @@ | ||
import { refreshToken } from "../../api"; | ||
import { toast } from "../../python"; | ||
import { globalState, server } from "../pythonRoot"; | ||
|
||
function createHeadersObj(authToken: string | undefined, request: RequestInit | undefined) { | ||
const headers = new Headers(); | ||
if (request && request.headers) { | ||
for (const [key, value] of Object.entries(request.headers)) { | ||
headers.append(key, value); | ||
} | ||
} | ||
if (authToken) headers.set("Authorization", `Bearer ${authToken}`); | ||
|
||
return headers; | ||
} | ||
|
||
export async function genericApiFetch( | ||
fetchPath: string, | ||
request: RequestInit | undefined = undefined, | ||
options: { | ||
requiresAuth?: boolean; | ||
onError?: () => void; | ||
customAuthToken?: string; | ||
failSilently?: boolean; | ||
} = { | ||
requiresAuth: false, | ||
customAuthToken: undefined, | ||
onError: () => {}, | ||
failSilently: false, | ||
} | ||
) { | ||
const { | ||
requiresAuth = false, | ||
customAuthToken = undefined, | ||
onError = () => {}, | ||
failSilently = false, | ||
} = options; | ||
|
||
const { apiUrl } = globalState!.getPublicState(); | ||
function doTheFetching(authToken: string | undefined = undefined) { | ||
const headers = createHeadersObj(authToken, request); | ||
console.log("TOKEN", authToken); | ||
|
||
console.log("TEST", { | ||
method: "GET", | ||
// If a custom method is specified in request it will overwrite | ||
...request, | ||
headers: headers, | ||
}); | ||
return server! | ||
.fetchNoCors<Response>(`${apiUrl}${fetchPath}`, { | ||
method: "GET", | ||
// If a custom method is specified in request it will overwrite | ||
...request, | ||
headers: headers, | ||
}) | ||
.then((deckyRes) => { | ||
if (deckyRes.success) { | ||
return deckyRes.result; | ||
} | ||
throw new Error(`Fetch not successful!`); | ||
}) | ||
.then((res) => { | ||
if (res.status >= 200 && res.status <= 300 && res.body) { | ||
// @ts-ignore | ||
return JSON.parse(res.body || ""); | ||
} | ||
throw new Error(`Res not OK!, code ${res.status} - ${res.body}`); | ||
}) | ||
.then((json) => { | ||
console.log("JSON", json); | ||
|
||
if (json) { | ||
return json; | ||
} | ||
throw new Error(`No json returned!`); | ||
}) | ||
.catch((err) => { | ||
if (!failSilently) { | ||
console.error(`Error fetching ${fetchPath}`, err); | ||
} | ||
onError(); | ||
}); | ||
} | ||
if (requiresAuth) { | ||
if (customAuthToken) { | ||
return doTheFetching(customAuthToken); | ||
} | ||
return refreshToken(onError).then((token) => { | ||
if (token) { | ||
return doTheFetching(token); | ||
} else { | ||
toast("Error Refreshing Token!", ""); | ||
return; | ||
} | ||
}); | ||
} else { | ||
return doTheFetching(); | ||
} | ||
} |
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,2 @@ | ||
export * from "./fetchWrappers"; | ||
export * from "./fetchWrappers"; |
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,74 @@ | ||
import { TaskQueryResponse } from "../../apiTypes/SubmissionTypes"; | ||
import { genericApiFetch } from "./fetchWrappers"; | ||
|
||
async function fetchLocalZip(fileName: string) { | ||
if (!fileName.endsWith(".zip")) { | ||
throw new Error(`File must be a .zip!`); | ||
} | ||
const filesRes = await fetch(`/themes_custom/${fileName}`); | ||
if (!filesRes.ok) { | ||
throw new Error(`Couldn't fetch zip!`); | ||
} | ||
const rawBlob = await filesRes.blob(); | ||
const correctBlob = new Blob([rawBlob], { type: "application/x-zip-compressed" }); | ||
return correctBlob; | ||
} | ||
|
||
export async function uploadZipAsBlob(fileName: string): Promise<string> { | ||
if (!fileName.endsWith(".zip")) { | ||
throw new Error(`File must be a .zip!`); | ||
} | ||
const fileBlob = await fetchLocalZip(fileName); | ||
console.log("BLOB", fileBlob); | ||
const formData = new FormData(); | ||
formData.append("File", fileBlob, fileName); | ||
console.log("FORM", formData); | ||
|
||
const json = await genericApiFetch( | ||
"/blobs", | ||
{ | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "multipart/form-data", | ||
}, | ||
body: formData, | ||
}, | ||
{ requiresAuth: true } | ||
); | ||
if (json) { | ||
return json; | ||
} | ||
throw new Error(`No json returned!`); | ||
} | ||
|
||
export async function publishProfile( | ||
profileId: string, | ||
isPublic: boolean, | ||
description: string | ||
): Promise<string> { | ||
// const zipName = `${profileId}.zip`; | ||
const zipName = "round.zip"; | ||
const blobId = await uploadZipAsBlob(zipName); | ||
if (!blobId) throw new Error(`No blobId returned!`); | ||
|
||
// ALL OF THIS IS UNTESTED, BLOB IS 415'ing RN | ||
const json = await genericApiFetch( | ||
`/submissions/css_zip`, | ||
{ | ||
method: "POST", | ||
body: JSON.stringify({ | ||
blob: blobId, | ||
imageBlobs: [], | ||
description: description, | ||
privateSubmission: !isPublic, | ||
}), | ||
}, | ||
{ requiresAuth: true } | ||
); | ||
if (!json || !json.task) throw new Error(`No task returned`); | ||
return json.task; | ||
} | ||
|
||
export async function getTaskStatus(taskId: string): Promise<TaskQueryResponse> { | ||
return await genericApiFetch(`/tasks/${taskId}`, undefined, { requiresAuth: true }); | ||
} |
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
Oops, something went wrong.