#contributions

Discussing the Functionality and Structure of Typesense Client Libraries

TLDR Jason explained how Typesense client libraries function and their architecture. Jason and Masahiro guide Joe in his work on the Java client, emphasizing a consistent method call structure across different languages. Joe highlights his progress and difficulties.

Powered by Struct AI
+13
7
29mo
Solved
Join the chat
Apr 22, 2021 (29 months ago)
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
07:13 PM
Essentially Typesense has a collection of REST API endpoints that you see in the sidebar here: https://typesense.org/docs/0.19.0/api/

Now, a dev could use any HTTP library in their language of choice to make HTTP calls to those API endpoints, but we're trying to make it easy for everyone to call these endpoints via method calls in their respective languages.

For eg: Instead of making an HTTP call like this to list all collections / indices and set headers:

curl -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" ""

If you use the Python library, you can just call:

client.collections.retrieve()

So the client libraries are thin wrappers around the API and each of them follow similar method calls.

Here's an example of how the Python library can be used: https://github.com/typesense/typesense-python/blob/c0592c8936540397d436d92b634cc40005c42a69/examples/collection_operations.py

Here are all the classes we typically create in each client to support a consistent method invocation pattern across clients: https://github.com/typesense/typesense-python/tree/master/typesense

Most classes are pretty simple. The most complicated piece to port over is the retry logic, where if one Typesense node in a cluster is down, the client will detect it and retry the request to another node, for high availability:

https://github.com/typesense/typesense-python/blob/c0592c8936540397d436d92b634cc40005c42a69/typesense/api_call.py#L86-L129
07:16
Jason
07:16 PM
If you're more familiar with Javascript, here's the same set of classes from the JS library: https://github.com/typesense/typesense-js/tree/master/src/Typesense

And the PHP library: https://github.com/typesense/typesense-php/tree/master/src

And the Ruby library: https://github.com/typesense/typesense-ruby/tree/master/lib/typesense
07:16
Jason
07:16 PM
So the idea is to essentially replicate the same method call structure in each language, and client library so it's consistent and easy to update in the future
+11
07:18
Jason
07:18 PM
Also, for typed languages, v-byte-cpu has created an OpenAPI spec that he used for the go library, but the spec can be used to generate types in other languages as well: https://github.com/typesense/typesense-go/blob/master/typesense/api/openapi.yml
+11
Apr 23, 2021 (29 months ago)
Masahiro
Photo of md5-366dff6b5f9b1a7d0f404fdc3261e573
Masahiro
03:02 AM
Harpreet You can check this guidance !
+11
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
06:02 AM
Joe This is a good thread to read for guidance on client library architecture ^
Apr 30, 2021 (29 months ago)
Joe
Photo of md5-d9ca032e3941589aafa8433269974f96
Joe
10:21 AM
Thanks, and yes it seems nice and performant that's for sure; I'm running it via docker in VirtualBox and port forwarding and still seems snappy; c++ will make you go gray though. The gitlab Java client has the bulk of the operations done, minus export/import, key management and possibly grouping operations on the search I think. My github issue seems to be getting worse, I can't reset password or create a new account, keep having to do a captcha on solving which set of dice add up to 14, but after solving 5, it goes to 10, after 10 to 15 etc. Very annoying hehe. It's possible that the code there though might be a start for others to look at, or hopefully at least grab bits of.