NokiMo
db0
db0

patreon


APIv2

I've been hard at work for the past 3 days, refactoring the whole Stable Horde code. It all started when I realized it was time to onboard swagger documentation for my RESTful API, because people had to be able to read up on it on their own.

As I was working on it however, I realized this would be a good opportunity to update the API itself, to fix some of the issues I had with it and some of the non-standard ways I built it due to inexperience.

However I didn't want to break the API completely for existing clients and workers, so I needed to be able to maintain two API versions side by side. This meant I needed to refactor my API code in a way that would allow me to have two live namespaces, connected to the same underlying variables.

But as I was reading up on that that, I saw that it needed to split into multiple files and then I quickly realized that my previous approach of relying on some global imports in the main file would not work, as I couldn't pass the same imports to the new modules (i.e. files) I was creating. So I needed to refactor my code further so that it could work via imports and custom packages. This especially took quite a bit of reading to wrap my head around.

But as I was working on that, it occurred that this would mean I would have to duplicate the whole refactoring for the KoboldAI Horde as well, as it also has an API which also needs documentation. However those two Hordes are almost identical in code, with only a few changes in the payloads and the kind of contributions they're tracking (pixelsteps VS tokens). So it was anguishing to think that I would have to redo the whole thing once more (with all the bugfixes that entails)

So I decided instead of refactor my code further so that the same classes and API can be plugged into both hordes, both the Stable and the KoboldAI (and any new ones I,or someone else, might come up with in the future). That in turn lead to me to read about class inheritance and how to reuse code in that fashion.

Ah but now that I will support multiple hordes, the API documentation needs to be more flexible, so that I can override the documentation of each endpoint with horde specific information, but without having to rewrite everything. So now I needed to read about the flask-restx models and field and response marshaling etc.

So I started wanting to add documentation to the my existing API, and one thing led to another and I ended up with a monster merge of 3K+ lines of changes

But it's done. After some furious bug-squashing, it seems more of less stable on the Stable Horde.

The biggest changes between APIv1 and APIv2 are:

1. 'Servers' have been renamed to 'Workers' as it is more accurate to their role

2. The API key is now sent in the header, which will allow me to use authentication for GET requests as well

3. The return codes for errors are now standardized in the {"message": "String"} format, instead of the pure string I was sending before which allows me to expand the error messages with more information as needed.

4. The documentation for both APIs can now be found in https://stablehorde.net/api/ and it automatically updated as I change my docstrings in my code.

And the swagger documentation looks dope and can be used directly from the browser! 


Related Creators