Julia API design

I want to try to reimplement pyxivapi in Julia, which is client for XIVAPI REST API. But I don’t know how to write the Julia APIs that looks like Julia library. Julia feels very different than other programming languages that I used. Is there a guide on how to design API for Julia?

Several more concrete questions:

Lets say I want to rewrite this code in Julia:

client = pyxivapi.XIVAPIClient(api_key="your_key_here")
item = client.index_by_id(
        index="Item", 
        content_id=23575, 
        columns=["ID", "Name", "Icon", "ItemUICategory.Name"], 
        language="de"
)

First question:
Can I write the index_by_id in client.index_by_id style or index_by_id(client,...) better? For the second style, do client go first in the arguments, or it go in the last?

Second question:
There is a language optional argument in that function. XIVAPI accepts jp,en,de,fr. I can implement it in Julia in several ways:

  1. Keep it that way, use string comparison language = "de"

  2. Use : instead, Plots.jl is like that language = :de

  3. Define an enum and set language argument type as that enum

        @enum Language begin
              jp
              en
              de
              fr
        end
    
        index_by_id(language::Language = en)
    
  4. Make a Language abstract type, then make those languages as concrete type

    abstract type Language end
    
    struct Jp <: Language end
    struct En <: Language end
    struct De <: Language end
    struct Fr <: Language end
    
    function index_by_id(lang::Type{T}) where T <: Language 
            ...
    end
    

Which one looks more Julian? Which one is faster? And which one can autocomplete in REPL or VS Code?

1 Like

index_by_id(client,...) is what you want. I think the enum solution for language is probably best, but that’s less clear The symbol solution looks OK too.

5 Likes