jaeyson / ex_typesense

Typesense client for Elixir with support for importing your Ecto schemas.
https://hexdocs.pm/ex_typesense
MIT License
30 stars 11 forks source link

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

Open kianmeng opened 4 months ago

kianmeng commented 4 months ago

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?

jaeyson commented 4 months ago

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?

kianmeng commented 4 months ago

Up to you, whichever you find convenient.

jaeyson commented 3 months ago

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 🥲
kianmeng commented 3 months ago
  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).

jaeyson commented 3 months ago

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

jaeyson commented 1 month ago

@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.

kianmeng commented 1 month ago

@jaeyson What does adapter here means?

jaeyson commented 1 month ago

@jaeyson What does adapter here means?

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