tdclient

package module
v0.0.0-...-b57e018 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2024 License: MIT Imports: 6 Imported by: 0

README

tdclient

A golang client for tibiadata's API

Actions on main Coverage Status Go Report Card LICENSE GoDoc

Disclaimer

This is a work in progress client library, it does not contain yet all the routes that TibiaData supports.

Usage

Check example/simple/main.go for a working example.

package main

import (
	"log/slog"

    "github.com/arxdsilva/tdclient"
)

func main() {
    client := tdclient.New()
    ctx := context.Background()
    world, err := client.GetWorld(ctx,"Premia")
    if err != nil {
        // handle error
    }

    worlds, err := client.GetWorlds(ctx)
    if err != nil {
        // handle error
    }

    fmt.Println("world: ", world)
    fmt.Println("worlds:", worlds)

    // you can also configure the client with options:
    tdclient.New(
        // change the env to query from
        tdclient.WithEnv("dev"),
        // bring your own slog logger
        tdclient.WithLogger(customSlogger),
        // set the debug level
        tdclient.WithLogLevel(slog.LevelDebug),
        // add your custom http client
        tdclient.WithHTTPClient(http.DefaultClient), 
        // customise the http client max timeout duration
        tdclient.WithTimeout(time.Hour),
    )
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	api.API
}

func New

func New(opts ...ClientOption) *Client

New creates a new instance of the Client. It accepts optional ClientOption arguments to customize the configuration. Returns a pointer to the newly created Client.

type ClientOption

type ClientOption func(*Config) *Config

func WithEnv

func WithEnv(env string) ClientOption

WithEnv sets the environment for the client configuration. It takes an `env` string as a parameter and returns a `ClientOption` function. The `ClientOption` function modifies the `Config` object by setting the `Env` field. If the `env` is not equal to "api", the `Env` field is set to "dev". Otherwise, the `Env` field is set to the value of `env`.

func WithHTTPClient

func WithHTTPClient(cl *http.Client) ClientOption

WithHTTPClient is a ClientOption function that sets the HTTP client for the configuration. It takes a pointer to an http.Client and returns a function that sets the client in the Config struct.

func WithLogLevel

func WithLogLevel(lv slog.Level) ClientOption

WithLogLevel sets the log level for the client. It takes a slog.Level as input and returns a ClientOption. The ClientOption modifies the Config object by setting the log level.

func WithLogger

func WithLogger(lg *slog.Logger) ClientOption

WithLogger sets the logger for the client. It takes a pointer to a slog.Logger and returns a ClientOption function. The ClientOption function sets the logger in the provided Config and returns the updated Config.

func WithTimeout

func WithTimeout(timeout time.Duration) ClientOption

WithTimeout sets the timeout duration for the HTTP client in the configuration. It returns a ClientOption function that modifies the given Config object.

type Config

type Config struct {
	Env     string
	Version string
	Host    string
	// contains filtered or unexported fields
}

Directories

Path Synopsis
api
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL