Use POST + body to get around URL length limit

This commit is contained in:
DominikDoom
2023-10-01 22:30:47 +02:00
parent 80fb247dbe
commit 440f109f1f
3 changed files with 26 additions and 14 deletions

View File

@@ -81,8 +81,12 @@ async function fetchAPI(url, json = true, cache = false) {
return await response.text();
}
async function postAPI(url, body) {
let response = await fetch(url, { method: "POST", body: body });
async function postAPI(url, body = null) {
let response = await fetch(url, {
method: "POST",
headers: {'Content-Type': 'application/json'},
body: body
});
if (response.status != 200) {
console.error(`Error posting to API endpoint "${url}": ` + response.status, response.statusText);
@@ -92,7 +96,7 @@ async function postAPI(url, body) {
return await response.json();
}
async function putAPI(url, body) {
async function putAPI(url, body = null) {
let response = await fetch(url, { method: "PUT", body: body });
if (response.status != 200) {
@@ -179,14 +183,16 @@ function mapUseCountArray(useCounts) {
}
// Call API endpoint to increase bias of tag in the database
async function increaseUseCount(tagName, type) {
await postAPI(`tacapi/v1/increase-use-count?tagname=${tagName}&ttype=${type}`, null);
await postAPI(`tacapi/v1/increase-use-count?tagname=${tagName}&ttype=${type}`);
}
// Get use count of tag from the database
async function getUseCount(tagName, type) {
return (await fetchAPI(`tacapi/v1/get-use-count?tagname=${tagName}&ttype=${type}`, true, false))["result"];
}
async function getUseCounts(tagNames, types) {
const rawArray = (await fetchAPI(`tacapi/v1/get-use-count-list?tags=${tagNames.join("&tags=")}&ttypes=${types.join("&ttypes=")}`))["result"]
// While semantically weird, we have to use POST here for the body, as urls are limited in length
const body = JSON.stringify({"tagNames": tagNames, "tagTypes": types});
const rawArray = (await postAPI(`tacapi/v1/get-use-count-list`, body))["result"]
return mapUseCountArray(rawArray);
}
async function getAllUseCounts() {
@@ -194,7 +200,7 @@ async function getAllUseCounts() {
return mapUseCountArray(rawArray);
}
async function resetUseCount(tagName, type) {
await putAPI(`tacapi/v1/reset-use-count?tagname=${tagName}&ttype=${type}`, null);
await putAPI(`tacapi/v1/reset-use-count?tagname=${tagName}&ttype=${type}`);
}
// Sliding window function to get possible combination groups of an array

View File

@@ -1158,8 +1158,7 @@ async function autocomplete(textArea, prompt, fixedTag = null) {
// Split our results into a list of names and types
let names = [];
let types = [];
// We need to limit size for the request url
results.slice(0, 100).forEach(r => {
results.forEach(r => {
const name = r.type === ResultType.chant ? r.aliases : r.text;
names.push(name);
types.push(r.type);
@@ -1324,7 +1323,7 @@ async function refreshTacTempFiles(api = false) {
}
if (api) {
await postAPI("tacapi/v1/refresh-temp-files", null);
await postAPI("tacapi/v1/refresh-temp-files");
await reload();
} else {
setTimeout(async () => {

View File

@@ -10,9 +10,10 @@ from pathlib import Path
import gradio as gr
import yaml
from fastapi import FastAPI, Query
from fastapi import FastAPI
from fastapi.responses import FileResponse, JSONResponse
from modules import script_callbacks, sd_hijack, shared
from pydantic import BaseModel
from scripts.model_keyword_support import (get_lora_simple_hash,
load_hash_cache, update_hash_cache,
@@ -611,10 +612,16 @@ def api_tac(_: gr.Blocks, app: FastAPI):
@app.get("/tacapi/v1/get-use-count")
async def get_use_count(tagname: str, ttype: int):
return db_request(lambda: db.get_tag_count(tagname, ttype), get=True)
@app.get("/tacapi/v1/get-use-count-list")
async def get_use_count_list(tags: list[str] | None = Query(default=None), ttypes: list[int] | None = Query(default=None)):
return db_request(lambda: list(db.get_tag_counts(tags, ttypes)), get=True)
# Small dataholder class
class UseCountListRequest(BaseModel):
tagNames: list[str]
tagTypes: list[int]
# Semantically weird to use post here, but it's required for the body on js side
@app.post("/tacapi/v1/get-use-count-list")
async def get_use_count_list(body: UseCountListRequest):
return db_request(lambda: list(db.get_tag_counts(body.tagNames, body.tagTypes)), get=True)
@app.put("/tacapi/v1/reset-use-count")
async def reset_use_count(tagname: str, ttype: int):