Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2
updates:

- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "weekly"
timezone: "Europe/Kyiv"
day: "friday"
time: "18:00"
33 changes: 33 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Build

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
strategy:
matrix:
os: [ ubuntu-latest, windows-latest, macos-latest ]
version: [ 1.22 ]
runs-on: ${{ matrix.os }}
steps:

- name: Check out
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.version }}
check-latest: true

- name: Run Make:lint
run: make lint

- name: Run Make:test
run: make test
22 changes: 22 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Publish

on:
workflow_dispatch:

jobs:
build:
strategy:
matrix:
os: [ ubuntu-latest ]
version: [ 1.22 ]
runs-on: ${{ matrix.os }}
steps:

- name: Check out
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.version }}
check-latest: true
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# OS
Thumbs.db
.DS_Store
*.pdb

# Editors
.vs/
Expand All @@ -13,6 +12,8 @@ Thumbs.db
vendor/
*.test
go.work
go.sum
go.work.sum
*.out

# Output
Expand All @@ -35,3 +36,6 @@ env/
logs/
*.log
*.log*

# Coverage
coverage.txt
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
lint: ## Lint the source code
@echo "🧹 Cleaning go.mod.."
@go mod tidy
@echo "🧹 Formatting files.."
@go fmt ./...
@echo "🧹 Vetting go.mod.."
@go vet ./...

test: ## Run tests
@echo "⏱️ Running tests.."
@go test -v -count=1 -race -shuffle=on -coverprofile=coverage.txt ./...
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
<a href="https://discord.gg/pt53Ej7rrc"><img src="https://img.shields.io/discord/1181281407813828710" alt="Discord" /></a>
<a href="https://glide.einstack.ai/"><img src="https://img.shields.io/badge/build-view-violet%20?style=flat&logo=books&label=docs&link=https%3A%2F%2Fglide.einstack.ai%2F" alt="Glide Docs" /></a>
<a href="https://artifacthub.io/packages/helm/einstack/glide"><img src="https://img.shields.io/endpoint?url=https://artifacthub.io/badge/repository/einstack" alt="ArtifactHub" /></a>
<br/>
<a href="https://github.com/einstack/glide-go/actions/workflows/build.yaml">
<img src="https://img.shields.io/github/actions/workflow/status/einstack/glide-go/build.yaml?branch=main&label=build&logo=github&style=flat-square" alt="Github Action" />
</a>
<a href="https://pkg.go.dev/github.com/einstack/glide-go"><img src="https://pkg.go.dev/badge/github.com/einstack/glide-go.svg" alt="Go Reference" /></a>
</div>

---
Expand All @@ -25,4 +30,31 @@ go get github.com/einstack/glide-go

## Usage

...
For a full example take a look at [`hello.go`](examples/hello.go).

```go
package main

import (
"context"
"log"

"github.com/einstack/glide-go"
"github.com/einstack/glide-go/lang"
)

func main() {
client, err := glide.NewClient()
if err != nil {
log.Fatal(err)
}

req := lang.NewChatRequest("Hello")
resp, err := client.Lang.Chat(ctx, "myrouter", req)
if err != nil {
log.Fatal(err)
}

log.Println("response: ", resp.Content())
}
```
132 changes: 132 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package glide

import (
"context"
"net/http"
"net/url"

"github.com/einstack/glide-go/config"
"github.com/einstack/glide-go/lang"
)

// Client is an Einstack Glide client.
type Client struct {
config *config.Config
Lang lang.Language
}

// ClientOption is a functional option type for Client.
// Also see NewClient.
type ClientOption func(*Client) error

// NewClient instantiates a new Client.
func NewClient(options ...ClientOption) (*Client, error) {
client := &Client{config: config.NewConfig()}
client.Lang = lang.NewLanguage(client.config)

for _, option := range options {
if err := option(client); err != nil {
return nil, err
}
}

return client, nil
}

// NewClientFromConfig instantiates a new Client.
func NewClientFromConfig(config *config.Config) (*Client, error) {
client := &Client{config: config}
client.Lang = lang.NewLanguage(client.config)
return client, nil
}

// WithApiKey attaches the api key.
// Use environment variable 'GLIDE_API_KEY' to override.
func WithApiKey(apiKey string) ClientOption {
return func(client *Client) error {
client.config.ApiKey = apiKey
return nil
}
}

// WithUserAgent replaces the 'User-Agent' header.
// Default value: 'Glide/0.1.0 (Go; Ver. 1.22.2)'.
// Use environment variable 'GLIDE_USER_AGENT' to override.
func WithUserAgent(userAgent string) ClientOption {
return func(client *Client) error {
client.config.UserAgent = userAgent
return nil
}
}

// WithRawBaseURL parses and replaces the base URL.
// Default value: 'http://127.0.0.1:9099/'.
// Use environment variable 'GLIDE_BASE_URL' to override.
func WithRawBaseURL(rawBaseURL string) ClientOption {
return func(client *Client) error {
baseURL, err := url.Parse(rawBaseURL)
if err != nil {
return err
}

client.config.BaseURL = baseURL
return nil
}
}

// WithBaseURL replaces the base URL.
// Also see WithRawBaseURL.
func WithBaseURL(baseURL url.URL) ClientOption {
return func(client *Client) error {
client.config.BaseURL = &baseURL
return nil
}
}

// WithHttpClient replaces the 'HTTP' client.
// Default value: 'http.DefaultClient'.
func WithHttpClient(httpClient *http.Client) ClientOption {
return func(client *Client) error {
client.config.HttpClient = httpClient
return nil
}
}

// ApiKey returns the provided API key, empty string otherwise.
func (c *Client) ApiKey() string {
return c.config.ApiKey
}

// UserAgent returns the used 'User-Agent' header value.
func (c *Client) UserAgent() string {
return c.config.UserAgent
}

// BaseURL returns the used 'base url.URL'.
func (c *Client) BaseURL() url.URL {
return *c.config.BaseURL
}

// HttpClient returns the underlying http.Client.
func (c *Client) HttpClient() *http.Client {
return c.config.HttpClient
}

// Health returns true if the service is healthy.
func (c *Client) Health(ctx context.Context) (*bool, error) {
type Health struct {
Healthy bool `json:"healthy"`
}

req, err := c.config.Build(ctx, http.MethodGet, "/v1/health/", nil)
if err != nil {
return nil, err
}

var resp *Health
if _, err := c.config.Send(req, resp); err != nil {
return nil, err
}

return &resp.Healthy, nil
}
38 changes: 38 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package glide_test

import (
"context"
"testing"

"github.com/einstack/glide-go"
"github.com/einstack/glide-go/config"
)

func TestNewClient(t *testing.T) {
if _, err := glide.NewClient(
glide.WithApiKey("testing"),
glide.WithRawBaseURL("http://127.0.0.1:9098/"),
glide.WithUserAgent("Einstack/1.0"),
); err != nil {
t.Fatal(err)
}
}

func TestNewClientFromConfig(t *testing.T) {
if _, err := glide.NewClientFromConfig(
config.NewConfig(),
); err != nil {
t.Fatal(err)
}
}

func TestClient_Health(t *testing.T) {
client, _ := glide.NewClient(
glide.WithApiKey("testing"),
)

ctx := context.Background()
if _, err := client.Health(ctx); err != nil {
t.Fatal(err)
}
}
Loading