Skip to content

Commit

Permalink
Merge pull request #2 from freakytoad1/main
Browse files Browse the repository at this point in the history
Client Options & GH Actions
  • Loading branch information
dweymouth authored Dec 21, 2023
2 parents 07699d7 + 33efedc commit e3a3849
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 9 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...

- name: Run golangci-lint
uses: golangci/[email protected]
42 changes: 33 additions & 9 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"bytes"
"context"
"crypto/md5"
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -38,7 +38,7 @@ type Client struct {
}

// NewClient creates a jellyfin Client using the url provided.
func NewClient(urlStr, clientName, clientVersion string) (*Client, error) {
func NewClient(urlStr, clientName, clientVersion string, opts ...ClientOptionFunc) (*Client, error) {
// validate the baseurl
if urlStr == "" {
return nil, errors.New("url must be provided")
Expand All @@ -52,14 +52,38 @@ func NewClient(urlStr, clientName, clientVersion string) (*Client, error) {
return nil, err
}

return &Client{
cli := &Client{
HTTPClient: &http.Client{
Timeout: DefaultTimeOut,
},
baseURL: baseURL,
ClientName: clientName,
ClientVersion: clientVersion,
}, nil
}

// perform any options provided
for _, option := range opts {
option(cli)
}

return cli, nil
}

// ClientOptionFunc can be used to customize a new jellyfin API client.
type ClientOptionFunc func(*Client)

// Http timeout override.
func WithTimeout(timeout time.Duration) ClientOptionFunc {
return func(c *Client) {
c.HTTPClient.Timeout = timeout
}
}

// Http client override.
func WithHTTPClient(httpClient *http.Client) ClientOptionFunc {
return func(c *Client) {
c.HTTPClient = httpClient
}
}

// BaseURL return a copy of the baseURL.
Expand Down Expand Up @@ -206,16 +230,16 @@ func deviceName() string {
return hostname
}

const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"
const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

// randomKey returns a string at the length desired of random mixed-case letters.
func randomKey(length int) string {
r := rand.Reader
data := make([]byte, length)
r.Read(data)

for i, b := range data {
data[i] = letters[b%byte(len(letters))]
for i := range data {
data[i] = letters[rand.Intn(len(letters))]
}

return string(data)
}

Expand Down

0 comments on commit e3a3849

Please sign in to comment.