# API Libraries

Libraries were generated using [openapi-python-client](https://github.com/openapi-generators/openapi-python-client) for interacting with various REST APIs:

```sh
nix-shell -p ruff --run "openapi-python-client generate --path archive/scoresaber.com.swagger.json"
nix-shell -p ruff --run "openapi-python-client generate --path archive/beatleader.xyz.swagger.json"
```

To convert beatsaver, we had to convert it from openapi 2.0 to 3.0:

```sh
npm set prefix ~/.npm-global
export PATH=$HOME/.npm-global/bin:$PATH
npm install -g swagger2openapi
swagger2openapi --patch archive/beatsaver.com.swagger.json > archive/beatsaver.com.swagger_openapi3.json
```

Then we had to run some python code to fix up the mapping after the conversion:

```python
import json

# Load the OpenAPI spec
with open('archive/beatsaver.com.swagger_openapi3.json', 'r') as f:
    spec = json.load(f)

# Fix tags in the main spec
if 'tags' in spec:
    spec['tags'] = [tag['name'] if isinstance(tag, dict) else tag for tag in spec['tags']]

# Fix tags in each path
for path in spec['paths'].values():
    for method in path.values():
        if 'tags' in method:
            method['tags'] = [tag['name'] if isinstance(tag, dict) else tag for tag in method['tags']]

# Save the fixed spec
with open('archive/beatsaver.com.swagger_openapi3_fixed.json', 'w') as f:
    json.dump(spec, f, indent=2)
```

And we ran this one too:

```python
import json

# Load the OpenAPI spec
with open('archive/beatsaver.com.swagger_openapi3_fixed.json', 'r') as f:
    spec = json.load(f)

# Fix tags in the main spec
if 'tags' in spec:
    spec['tags'] = [tag['name'] if isinstance(tag, dict) else tag for tag in spec['tags']]

# Fix tags and content types in each path
for path in spec['paths'].values():
    for method in path.values():
        if 'tags' in method:
            method['tags'] = [tag['name'] if isinstance(tag, dict) else tag for tag in method['tags']]
        if 'responses' in method:
            for response in method['responses'].values():
                if 'content' in response:
                    if '*/*' in response['content']:
                        content = response['content']['*/*']
                        response['content'] = {'application/json': content}

# Save the fixed spec
with open('archive/beatsaver.com.swagger_openapi3_fixed2.json', 'w') as f:
    json.dump(spec, f, indent=2)
```

```sh
nix-shell -p ruff --run "openapi-python-client generate --path archive/beatsaver.com.swagger_openapi3_fixed2.json"
```

## Resources

* Beat Leader [swagger](https://api.beatleader.xyz/swagger/index.html), [GitHub](https://github.com/BeatLeader)
* Score Saber [swagger](https://docs.scoresaber.com/), [Github](https://github.com/ScoreSaber) (backend remains closed-source)
* Beat Saver [swagger](https://api.beatsaver.com/docs/), [GitHub](https://github.com/beatmaps-io/beatsaver-main)