Giter VIP home page Giter VIP logo

bananas-api's Introduction

BaNaNaS API

GitHub License

This is the HTTP API for OpenTTD's content service, called BaNaNaS. It works together with bananas-server, which serves the in-game client.

See introduction.md for more documentation about the different BaNaNaS components and how they work together.

The API is documented on SwaggerHub.

Development

This API is written in Python 3.8 with aiohttp, and makes strong use of asyncio.

Running a local server

Dependencies

  • Python3.8 or higher.
  • tusd. For example, copy the tusd binary in your ~/.local/bin.

Preparing your venv

To start it, you are advised to first create a virtualenv:

python3 -m venv .env
.env/bin/pip install -r requirements.txt

Starting a local server

You can start the HTTP server by running:

.env/bin/python -m bananas_api --web-port 8080 --tusd-port 1080 --storage local --index local --user developer --client-file clients-development.yaml

This will start the API on port 8080 for you to work with locally.

Running via docker

docker build -t openttd/bananas-api:local .
export BANANAS_COMMON=$(pwd)/../bananas-common
mkdir -p "${BANANAS_COMMON}/local_storage" "${BANANAS_COMMON}/BaNaNaS"
docker run --rm -p 127.0.0.1:8080:80 -p 127.0.0.1:1080:1080 -v "${BANANAS_COMMON}/local_storage:/code/local_storage" -v "${BANANAS_COMMON}/BaNaNaS:/code/BaNaNaS" openttd/bananas-api:local

The mount assumes that bananas-server and this repository has the same parent folder on your disk, as both servers need to read the same local storage.

Files upload (tusd)

tusd runs on its own port (1080 by default), and listens on /new-package/files. With other words: the webserver does not forward that URL to tusd. This means that for clients, you need to contact two endpoints:

  • the web-port for everything except /new-package/files.
  • the tusd-port for /new-package/files.

In production the Load Balancer redirects the URLs to the right ports, but during development this is something to keep in mind.

bananas-frontend-cli for example allows you to define the web-endpoint and the tusd-endpoint.

Regions

To unify the way authors indicate what region their content is about, we have a built-in list of supported regions. This is a combination of the UN M49 list and ISO 3166-1 / 3166-2 list.

  • The 3166-1 / 3166-2 list is easiest found in the Debian iso-codes package, after which it is located in /usr/share/iso-codes/json/iso_3166-[12].json.
  • The UN M49 can be found here.

bananas-api's People

Contributors

dependabot[bot] avatar frosch123 avatar glx22 avatar lordaro avatar pyup-bot avatar truebrain avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

bananas-api's Issues

Allow reading license/readme/changelog of packages via API (or give a link)

It would be nice to show in the frontend what the license is (if custom), or show the readme or changelog.

Technically, this is a bit tricky, as these files (after upload) are only available inside the tarball. Most likely it is a good idea to store those files separatly on S3, so it is easy to fetch them. Not a trivial issue to fix for sure.

Increase upload-token expiration time

Some content is approaching a size of 1 GB.
With some connections a upload timeout of 15 minutes is rather harsh.

It's also likely that people would start their GB upload, and go for a walk.

So I suggest to make upload-tokens expire together with the session, and not on their own.

Publishing updates fails

When uploading an update to an existing package, the front-end makes the same assumption as when editing meta data. "No name" means "name from package".
It looks like the API agrees in the validation step, since it does not complain about missing stuff in the GET /new-package/.

However, .../publish gives 500:

2020-04-25 21:48:46 ERROR Error handling request
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/aiohttp/web_protocol.py", line 418, in start
resp = await task
File "/usr/local/lib/python3.8/site-packages/aiohttp/web_app.py", line 458, in _handle
resp = await handler(request)
File "/code/bananas_api/web_routes/new.py", line 194, in new_publish
publish_session(session)
File "/code/bananas_api/new_upload/session.py", line 232, in publish_session
create_tarball(session)
File "/code/bananas_api/new_upload/session_publish.py", line 112, in create_tarball
tar_path = _safe_name(session["name"]) + "-" + _safe_name(session["version"])
KeyError: 'name'
2020-04-25 21:48:46 INFO 172.17.0.1 [25/Apr/2020:21:48:46 +0000] "POST /new-package/3a6ad88d8398ed157f8e8af1f9c5157c/publish HTTP/1.1" 500 244 "-" "python-requests/2.23.0"

Restrict dependencies between content-type

Currently you can make a heightmap depend on an AI, which makes very little sense.

The list should be like this:

  • ai, ai-library: only allow ai-library
  • base-graphics, base-music, base-sounds: no dependencies allowed
  • game-script, game-script-library: only allow game-script-library
  • heightmap, newgrf: no dependencies allowed
  • scenario: only allow newgrf, ai and game-script

Currently we have AIs that depend on a scenario. There is a case for this, but not sure it is a solid one.

Error when uploading a newgrf to bananas-api running in docker

If you run the bananas-api as suggested in the readme using:

docker run --rm -p 127.0.0.1:8080:80 -p 127.0.0.1:1080:1080 -v "${BANANAS_COMMON}/local_storage:/code/local_storage" -v "${BANANAS_COMMON}/BaNaNaS:/code/BaNaNaS" openttd/bananas-api:local

Upload of a newgrf fails with the following error:

OSError: [Errno 18] Invalid cross-device link

The issue is that the temporary upload path and the local_storage path are not on the same physical device (one is inside the virtual file system of the docker container, while the other is on a volume mounted on the host).

According to the documentation I have found, shutil.move is the correct function to use if you are moving between file systems.

Different packages with same name causes OpenTTD client to mess up

It is now possible to rename your package, and as such, people are renaming multiple packages of theirs to the same name.

I have no real problems with this, as the system considers the name a piece of metadata, but it turns out the OpenTTD client has a different vision on this.

When downloading a piece of content with the same name, it appears to be overwriting the existing file. This of course can be considered a bug on its own, but there is also not really a resolution for the client.

I want to suggest to rename the packages to <uniqueid>-<name>-<version>. This is still human-readable, and always unique.

Add support for adding public keys to packages

Idea to enable bots/GH actions to upload packages on releases.

  • Owner generates a pair of public/private keys.
  • Public key is stored in authors.yaml.
  • Private key is kept by uploading bots as secret.
  • bananas-cli and api perform a handshake for authentication.

Allow setting versions to "new-games" / "savegames-only" if you like to

Currently the latest upload is always "new-games", and anything else is "savegames-only". This is not technically required by anything, it was just easier to implement.

Please create an additional to the API that allows to set any version to "new-games" and "savegames-only". Possibly it would be good if two versions are "new-games" it is enforced that their min/max compatability-versions don't overlap (as that would make no sense).

Send download URL via API

For new-games, anyone should be able to get the download URLs.
For savegames-only, only the author should be able to get the download URL.

ReadError: file could not be opened successfully

Sentry Issue: BANANAS-API-D

ReadError: file could not be opened successfully
(1 additional frame(s) were not displayed)
...
  File "aiohttp/web_app.py", line 458, in _handle
    resp = await handler(request)
  File "bananas_api/web_routes/new.py", line 92, in tusd_handler
    add_file(
  File "bananas_api/new_upload/session.py", line 236, in add_file
    session["files"].extend(extract_tarball(new_file))
  File "bananas_api/new_upload/extract.py", line 79, in extract_tarball
    with tarfile.open(file_info["internal_filename"]) as tar:
  File "tarfile.py", line 1604, in open
    raise ReadError("file could not be opened successfully")

Error handling request

Putting yourself as dependency is valid

Putting yourself as dependency is valid.

This of course is wrong, and should not be possible. This should be filtered on unique_id already, you also aren't allowed to set dependencies on an older version of your own content.

Support teams

For example, via GitHub Teams. Just any form that multiple people can create a single content, without them sharing a password.

Cache validation result of individual uploaded files

Validation of big files (I tried a NewGRF with 850 MB) takes several seconds (probably computing MD5). The validation is repeated for every PUT/GET, even when only updating meta-data like description.

Please cache the validation result of uploaded files, if they do not change.

Read dependencies from NewGRF / Scenarios automatically

This information is already inside these files, and we already extract it. Adding this would make uploads a lot easier for many users, so a huge QoL.

With this, we should also restrict manual editing of dependencies to only AI/AI Library/GS/GS Library.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.