KSP-SpaceDock / SpaceDock

Current Codebase (Python /Flask)
https://spacedock.info
Other
73 stars 33 forks source link

Ability to filter results on API for a specific game #465

Closed RedstoneWizard08 closed 1 year ago

RedstoneWizard08 commented 1 year ago

Description (What went wrong?):

I'm working on the installer for SpaceWarp over at https://github.com/SpaceWarpDev/Space-Warp-Installer and https://github.com/RedstoneWizard08/Space-Warp-Installer and I'm trying to filter mods on the API for only KSP2, but there is no option for that, and then the pagination breaks because it fetches all mods and some pages will be blank.

Reproduction Steps (What did you do?):

import axios from "axios";

import { finishBrowseResult } from "./models/browse";
import { finishFullModInfo } from "./models/modinfo/full";

export class SpaceDockAPI {
    private base: string;

    public constructor(baseUrl?: string) {
        this.base = baseUrl || SpaceDockAPI.getDefaultAPIUrl();
    }

    public static getDefaultAPIUrl() {
        return "https://spacedock.info/api";
    }

    public async getMods() {
        const response = await axios.get(`${this.base}/browse`);

        return finishBrowseResult(response.data);
    }

    public async getMod(id: string | number) {
        const response = await axios.get(`${this.base}/mod/${id}`);

        return finishFullModInfo(response.data);
    }

    // example: getModsForGame(22407) = KSP2
    public async getModsForGame(gameId: number) {
        const response = await this.getMods();
        const mods = response.result.filter((mod) => mod.game_id == gameId);

        return mods;
    }
}

Expected Behavior (What do you think should have happened instead?):

What I would like is to be able to filter on the browse API with more parameters like game_id and game_version to allow for easier use.

Environment (OS/Browser/Plugins/etc):

  1. Windows/Wry (Tauri)/Tauri/Preact/TypeScript
  2. Chrome OS/Chrome/Preact/Vite/TypeScript
  3. Linux (Ubuntu 22.04)/TypeScript

Extra Information (Screenshots/Error Messages/Javascript Console Output):

HebaruSan commented 1 year ago

The /api/browse route is here if you want to try adding the filters you need:

https://github.com/KSP-SpaceDock/SpaceDock/blob/dbd67221961376193983028c8977c51b5296204d/KerbalStuff/blueprints/api.py#L302-L343

SpaceDock is open source and pull requests are welcome. If you do, please target the alpha branch as that's where SpaceDock's dev process starts.

RedstoneWizard08 commented 1 year ago

I'll give it a try!

HebaruSan commented 1 year ago

That dev guide link also has a section on starting an instance of SpaceDock locally in Docker, which is extremely helpful for debugging (but may be overkill for a relatively small API change):

https://github.com/KSP-SpaceDock/SpaceDock/wiki/Development-Guide#running-with-docker

RedstoneWizard08 commented 1 year ago

I was already doing that lol but yea I'm gonna do dev in docker. Is there like an alpha preview deployed btw so I could use it sooner, before it goes into the stable release?

V1TA5 commented 1 year ago

https://alpha.spacedock.info/ For compleatness beta also exists for later testing: https://beta.spacedock.info/

We are following the usual software dev stages of: alpha,beta,staging,live

HebaruSan commented 1 year ago

No, but there's an alpha server with a separate database:

https://alpha.spacedock.info/

Useful for public testing but not for accessing data from the production server.

RedstoneWizard08 commented 1 year ago

Ok

RedstoneWizard08 commented 1 year ago

Done in #466!

HebaruSan commented 1 year ago

Cool! 🎉

What I would like is to be able to filter on the browse API with more parameters like game_id and game_version to allow for easier use.

Did you decide game_version wasn't needed?

RedstoneWizard08 commented 1 year ago

I should probably fix that lol

V1TA5 commented 1 year ago

We should update the api doc to reflect the new abilities.

RedstoneWizard08 commented 1 year ago

@HebaruSan I'm not going to add game_version since it's not available on the Mod's base class, and I don't want to slow down the query. I think it'll be okay though.

V1TA5 commented 1 year ago

Game_version is a different db table from game id. Thats possibly why.

RedstoneWizard08 commented 1 year ago

Yeah.

V1TA5 commented 1 year ago

I dont know how large the impact is. ATM cpu load on the server isnt a big problem. Its your chance to tailor the API to your needs :)

HebaruSan commented 1 year ago

Checking the other table is possible:

https://github.com/KSP-SpaceDock/SpaceDock/blob/dbd67221961376193983028c8977c51b5296204d/KerbalStuff/search.py#L78-L79

RedstoneWizard08 commented 1 year ago

alr im adding it

RedstoneWizard08 commented 1 year ago

It's added!

V1TA5 commented 1 year ago

Now everything has to go trough testing and we have to put it on production. Its up to @HebaruSan to tell me when its good enough to do so.