open-meteo / sdk

Open-Meteo schema files
MIT License
24 stars 15 forks source link

feat: Add support for AIProxy in Swift #120

Closed lzell closed 1 month ago

lzell commented 1 month ago

WeatherApiResponse has a public static method fetch(url:session:) that customers use to interact with the Open-Meteo API. This patch opens a second public static method, fetch(request:session:) that takes a URLRequest as the first argument.

Why? By passing in a URLRequest, the developer is able to customize headers, which is useful if the request is first routing through a proxy. There is an AIProxy customer that would like to use your commercial API. In order for your service to be compatible with AIProxy, the end developer needs to be able to specify request headers (e.g. a DeviceCheck header, and a header that contains part of a split-key encryption scheme). These headers allow AIProxy to defend against actors that would try to steal a secret API key, or abuse a service endpoint.

I have found that the least invasive way to modify your SDK is to open this new public method. I'll open this PR as a draft to address any concerns that you may have, and I'm happy to answer questions!

patrick-zippenfenig commented 1 month ago

Looks good. Do you use any other libraries like async-http-client? Now with better async support in Swift, I could add support for AsyncSequences as well.

lzell commented 1 month ago

Thanks for approving! I don't personally use async-http-client, but it looks nice.

For my needs, I find the async/await versions of Foundation's URLSessionDataTask to be sufficient, since the requests are originating from an app and they are relatively infrequent. If I were doing some heavier lifting, I'd consider dropping in that client (the nonblocking I/O aspects look most handy for dealing with lots of requests with a small memory footprint).

For my purposes, users of AIProxy are making a small number of requests from an app. The requests route from the app -> aiproxy -> the final destination, in this case Open-Meteo! AIProxy sits in the middle and applies DeviceCheck, banning, and rate limiting functionality so that the app developer gets a predictable bill. Hope that helps! Happy to chat more too, on here or twitter or lou@aiproxy.pro

In some cases, I try to bake AIProxy support directly into the client sdk. In this case, since you expose almost everything I need already, I think just opening this new interface is enough for me. If you are curious what a deeper integration looks like, you can see an example here: https://github.com/jamesrochabrun/SwiftAnthropic/pull/26

I'm also thinking of extracting the common parts of that PR into a little lib on its own so that folks can add AIProxy support to their own SDKs with ease

lzell commented 1 month ago

@patrick-zippenfenig FYI I force-pushed a new commit that uses my GPG signature to satisfy your workflow

lzell commented 1 month ago

Hi @patrick-zippenfenig, do you mind taking another look? I'm still seeing this:

Screenshot 2024-08-02 at 08 29 14

github-actions[bot] commented 1 month ago

:tada: This issue has been resolved in version 1.13.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

lzell commented 1 month ago

Thank you @patrick-zippenfenig !

patrick-zippenfenig commented 1 month ago

Thanks for your contribution!