ex_typesense icon indicating copy to clipboard operation
ex_typesense copied to clipboard

Replace the API wrapper code with with code generated from open-api-generator

Open kianmeng opened this issue 1 year ago • 8 comments

Would it possible to replace the existing API related code in https://github.com/jaeyson/ex_typesense/tree/main/lib/ex_typesense with generated code using the OpenAPI spec, https://raw.githubusercontent.com/typesense/typesense-api-spec/master/openapi.yml, using from https://github.com/aj-foster/open-api-generator?

Not sure if we want to put the generated code within the same repository or into two separate repositories or packages.

WDYT?

kianmeng avatar Jul 05 '24 07:07 kianmeng

Hi @kianmeng, I'm cool with that. I'll circle back and let you know. In the meantime, what's pros/cons if within/separate repo? what's your preference?

jaeyson avatar Jul 05 '24 08:07 jaeyson

Up to you, whichever you find convenient.

kianmeng avatar Jul 06 '24 08:07 kianmeng

Sorry @kianmeng I'm late to reply, too many errands in past few weeks. What I did is copying exactly what's in the example openapi.yml file and ran mix api.gen. I have few noob questions 😅:

  1. When we say Replace the API wrapper code, does that mean move HttpClient (Req client) endpoint definitions to openapi.yml?
  2. What should we see inside this yml file?
  3. Can you point me on few articles regarding this? cause I'm lost 🥲

jaeyson avatar Aug 11 '24 11:08 jaeyson

  1. When we say Replace the API wrapper code, does that mean move HttpClient (Req client) endpoint definitions to openapi.yml?

Replacing the HTTPClient and some modules entirely. I'm unsure whether this is feasible so far.

  1. What should we see inside this yml file?

It is from https://raw.githubusercontent.com/typesense/typesense-api-spec/master/openapi.yml and we don't need to generate on our own.

  1. Can you point me on few articles regarding this? cause I'm lost 🥲

You can start with https://github.com/aj-foster/open-api-generator by trying to generate an Elixir Open API client first based on the OpenAPI spec file mentioned (2).

kianmeng avatar Aug 11 '24 14:08 kianmeng

Hi @kianmeng, kindly check #47. Lmk what's missing.

jaeyson avatar Aug 14 '24 07:08 jaeyson

@kianmeng I raised a discussion here. It seems that after I generate e.g. mix api.gen..., there's an implementation of the http client:

e.g.

@default_client OpenApiTypesense.Client

@doc """
Creates an analytics rule

When an analytics rule is created, we give it a name and describe the type, the source collections and the destination collection.
"""
@spec create_analytics_rule(OpenApiTypesense.AnalyticsRuleSchema.t(), keyword) ::
        {:ok, OpenApiTypesense.AnalyticsRuleSchema.t()}
        | {:error, OpenApiTypesense.ApiResponse.t()}
def create_analytics_rule(body, opts \\ []) do
  client = opts[:client] || @default_client

  client.request(%{
    args: [body: body],
    call: {OpenApiTypesense.Analytics, :create_analytics_rule},
    url: "/analytics/rules",
    body: body,
    method: :post,
    request: [{"application/json", {OpenApiTypesense.AnalyticsRuleSchema, :t}}],
    response: [
      {201, {OpenApiTypesense.AnalyticsRuleSchema, :t}},
      {400, {OpenApiTypesense.ApiResponse, :t}}
    ],
    opts: opts
  })
end

Which from my understanding client.request is hardcoded, But I don't know how to use other adapter to bend according to its needs.

jaeyson avatar Sep 20 '24 09:09 jaeyson

@jaeyson What does adapter here means?

kianmeng avatar Sep 20 '24 17:09 kianmeng

@jaeyson What does adapter here means?

I meant customizing the http client, the library author just told me what to do. All good now.

jaeyson avatar Sep 25 '24 04:09 jaeyson

@jaeyson Thanks for implementing this! 🥳

kianmeng avatar Jan 20 '25 15:01 kianmeng