equinox

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2024 License: MIT Imports: 16 Imported by: 1

README

equinox

UsageExampleTodoAbout

Features

  • Riot APIs implemented:
    • Riot Account
    • League of Legends
    • Teamfight Tactics
    • Valorant
    • Legends of Runeterra
  • Data Dragon and Community Dragon - (Incomplete)
  • Rate limit (Internal)
  • Caching with BigCache or Redis
  • Logging with zerolog
  • Exponential backoff

equinox currently uses the proposed jsonv2 package, read more about it here.

Usage

Get the library:

go get github.com/Kyagara/equinox # or: go get github.com/Kyagara/equinox@main

Create a new instance of the equinox client:

client, err := equinox.NewClient("RIOT_API_KEY")
// or:
client, err := equinox.NewClientWithConfig(api.EquinoxConfig{})

Cache and Rate Limit can be disabled by passing nil, disabling rate limit can be useful if you have equinox passing through a proxy that handles rate limiting. See Cache and Rate limit for more details.

A default equinox client comes with the default options:

  • Key: The provided Riot API key.
  • HTTPClient: http.Client with a timeout of 15 seconds.
  • Cache: BigCache with an eviction time of 4 minutes.
  • RateLimit: Internal rate limiter with a limit usage factor of 0.99 and interval overhead of 1 second.
  • Logger: api.Logger object with zerolog.WarnLevel. Will log if rate limited or when retrying a request (before waiting).
  • Retry: api.Retry object with a limit of 3 and jitter of 500 milliseconds.

Using different endpoints:

// Contexts without a deadline will block if rate limited, waiting for the rate limited buckets to reset.
// If a deadline is set, there will be checks done before any block to see if waiting would exceed that deadline.
ctx := context.Background()

// This method uses an api.RegionalRoute. Can be accessed with a Development key.
match, err := client.LOL.MatchV5.ByID(ctx, api.AMERICAS, "BR1_2...")

// This method uses an val.PlatformRoute. May not be available in your policy.
matches, err := client.VAL.MatchV1.Recent(ctx, val.BR, "competitive")

// Interacting with DDragon.
version, err := client.DDragon.Version.Latest(ctx)
champion, err := client.CDragon.Champion.ByName(ctx, version, "Aatrox")

// Interacting with the cache.
data, err := client.Cache.Get("https://...")
err := client.Cache.Set("https://...", data)

// Using ExecuteRaw which returns []byte but skips checking cache.
l := client.Internal.Logger("LOL_StatusV4_Platform")
urlComponents := []string{"https://", lol.BR1, api.RIOT_API_BASE_URL_FORMAT, "/lol/status/v4/platform-data"}
req, err := client.Internal.Request(ctx, l, http.MethodGet, urlComponents, "", nil)
data, err := client.Internal.ExecuteRaw(ctx, req)

Example

For a slightly more advanced example, check out lol-match-crawler.

package main

import (
	"fmt"

	"github.com/Kyagara/equinox"
	"github.com/Kyagara/equinox/clients/lol"
)

func main() {
	client, err := equinox.NewClient("RIOT_API_KEY")
	if err != nil {
		fmt.Println("error creating client: ", err)
		return
	}
	// Get this week's champion rotation.
	ctx := context.Background()
	rotation, err := client.LOL.ChampionV3.Rotation(ctx, lol.BR1)
	if err != nil {
		fmt.Println("error retrieving champion rotation: ", err)
		return
	}
	fmt.Printf("%+v\n", rotation)
	// &{FreeChampionIDs:[17 43 56 62 67 79 85 90 133 145 147 157 201 203 245 518]
	// FreeChampionIDsForNewPlayers:[222 254 427 82 131 147 54 17 18 37]
	// MaxNewPlayerLevel:10}
}

Todo

  • Create a rate limit interface
  • Add Redis store for rate limit, using a lua script
  • Maybe the context usage throughout the project could be improved
  • Maybe add more options to customize the rate limiter
  • More tests for the internal client and rate limit
  • Improve DDragon/CDragon support

About

This is my first time developing and publishing an API client, any feedback is appreciated.

equinox started as a way for me to learn go, many projects helped me to understand the inner workings of an API client and go itself, here are some of them, check them out.

Projects not written in go:

A rewrite of Riven's code generation is used in equinox.

If cloning the project, make sure to have a go.work file in the root of the project, if you don't, you will get an error when trying to run go generate.

go 1.21

use (
	.
	./codegen
)

If you have any questions, feel free to open an issue or contact me.

Disclaimer

equinox isn't endorsed by Riot Games and doesn't reflect the views or opinions of Riot Games or anyone officially involved in producing or managing Riot Games properties. Riot Games, and all associated properties are trademarks or registered trademarks of Riot Games, Inc.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultConfig added in v0.14.1

func DefaultConfig(key string) (api.EquinoxConfig, error)

Returns the default equinox config with a provided key.

  • Key : The provided Riot API key.
  • HTTPClient : http.Client with a timeout of 15 seconds.
  • Cache : BigCache with an eviction time of 4 minutes.
  • RateLimit : Internal rate limiter with a limit usage factor of 0.99 and interval overhead of 1 second.
  • Logger : api.Logger object with zerolog.WarnLevel. Will log if rate limited or when retrying a request (before waiting).
  • Retry : api.Retry object with a limit of 3 and jitter of 500 milliseconds.

func DefaultLogger added in v1.0.0

func DefaultLogger() api.Logger

Returns the default logger config

  • Level : zerolog.WarnLevel
  • Pretty : false
  • TimeFieldFormat : zerolog.TimeFormatUnix
  • EnableConfigLogging : true
  • EnableTimestamp : true

func DefaultRetry added in v1.0.0

func DefaultRetry() api.Retry

Returns the default retry config

  • MaxRetries : 3
  • Jitter : 500 milliseconds

Types

type Equinox

type Equinox struct {
	Internal *internal.Client
	Cache    *cache.Cache
	DDragon  *ddragon.Client
	CDragon  *cdragon.Client
	Riot     *riot.Client
	LOL      *lol.Client
	TFT      *tft.Client
	VAL      *val.Client
	LOR      *lor.Client
}

func NewClient

func NewClient(key string) (*Equinox, error)

Creates a new equinox client with the default configuration

func NewClientWithConfig added in v0.3.0

func NewClientWithConfig(config api.EquinoxConfig) *Equinox

Creates a new equinox client using a custom configuration.

Directories

Path Synopsis
Package used to share common constants and structs.
Package used to share common constants and structs.
Cache package to provide an interface to interact with cache stores.
Cache package to provide an interface to interact with cache stores.
clients
cdragon
This package is used to interact with Community Dragon endpoints.
This package is used to interact with Community Dragon endpoints.
ddragon
This package is used to interact with Data Dragon endpoints.
This package is used to interact with Data Dragon endpoints.
lol
This package is used to interact with all LOL endpoints.
This package is used to interact with all LOL endpoints.
lor
This package is used to interact with all LOR endpoints.
This package is used to interact with all LOR endpoints.
riot
This package is used to interact with all Riot endpoints.
This package is used to interact with all Riot endpoints.
tft
This package is used to interact with all TFT endpoints.
This package is used to interact with all TFT endpoints.
val
This package is used to interact with all VAL endpoints.
This package is used to interact with all VAL endpoints.
codegen module
test
integration
This package only contains integration tests and are meant to be run manually.
This package only contains integration tests and are meant to be run manually.

Jump to

Keyboard shortcuts

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