Skip to content

Commit

Permalink
Adding example, changing package to medialog
Browse files Browse the repository at this point in the history
  • Loading branch information
dmnyu committed Sep 10, 2024
1 parent 14613e7 commit 5713ae6
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Entries.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package medialog

import (
"encoding/json"
Expand Down
29 changes: 12 additions & 17 deletions MedialogClient.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package main
package medialog

import (
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
Expand All @@ -13,16 +12,6 @@ import (
yaml "gopkg.in/yaml.v2"
)

var (
config string
environment string
)

func init() {
flag.StringVar(&config, "config", "", "")
flag.StringVar(&environment, "environment", "", "")
}

type Creds struct {
URL string `yaml:"url"`
Username string `yaml:"username"`
Expand All @@ -37,19 +26,25 @@ type MedialogClient struct {

const API_ROOT = "/api/v0"

func NewClient(creds Creds, timeout int) (MedialogClient, error) {
func NewClient(config string, environment string, timeout int) (*MedialogClient, error) {

mlClient := MedialogClient{}
creds, err := getCreds(config, environment)
if err != nil {
return &mlClient, err
}

tr := &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: time.Duration(timeout) * time.Second,
DisableCompression: true,
}

mlClient := MedialogClient{}
mlClient.Client = &http.Client{Transport: tr}
mlClient.BaseURL = creds.URL + API_ROOT
mlClient.getToken(creds)

return mlClient, nil
return &mlClient, nil
}

func getCreds(config string, environment string) (Creds, error) {
Expand All @@ -69,7 +64,7 @@ func getCreds(config string, environment string) (Creds, error) {
}
}

return Creds{}, fmt.Errorf("Credentials file did not contain %s\n", environment)
return Creds{}, fmt.Errorf("credentials file did not contain %s\n", environment)
}

func (mlClient *MedialogClient) getToken(creds Creds) {
Expand All @@ -91,7 +86,7 @@ func (mlClient *MedialogClient) getToken(creds Creds) {
mlClient.Token = token.Token
}

func (mlc MedialogClient) GetRoot() (models.MedialogInfo, error) {
func (mlc MedialogClient) GetHostInfo() (models.MedialogInfo, error) {
mlInfo := models.MedialogInfo{}
req, err := http.NewRequest("GET", mlc.BaseURL, nil)
if err != nil {
Expand Down
25 changes: 15 additions & 10 deletions MedialogClient_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package medialog

import (
"flag"
Expand All @@ -8,26 +8,31 @@ import (
"github.com/stretchr/testify/assert"
)

var mlc MedialogClient
var (
config string
environment string
)

func init() {
flag.StringVar(&config, "config", "", "")
flag.StringVar(&environment, "environment", "", "")
}

func TestClient(t *testing.T) {
flag.Parse()

var mlc *MedialogClient
t.Run("test create client", func(t *testing.T) {
creds, err := getCreds(config, environment)
if err != nil {
t.Error(err)
}

mlc, err = NewClient(creds, 20)
var err error
mlc, err = NewClient(20)
if err != nil {
t.Error(err)
}
})

t.Run("test get medialog info", func(t *testing.T) {

mlInfo, err := mlc.GetRoot()
mlInfo, err := mlc.GetHostInfo()
if err != nil {
t.Error(err)
}
Expand All @@ -53,6 +58,6 @@ func TestClient(t *testing.T) {
if err != nil {
t.Error(err)
}
t.Logf("%v", entry)
assert.Greater(t, entry.MediaID, uint(0))
})
}
45 changes: 45 additions & 0 deletions examples/GetHostInfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"os"

"github.com/nyudlts/medialog-client"
)

var (
config string
environment string
timeout int
)

func init() {
flag.StringVar(&config, "config", "", "")
flag.StringVar(&environment, "environment", "", "")
flag.IntVar(&timeout, "timeout", 20, "")
}

func main() {
flag.Parse()

mlc, err := medialog.NewClient(config, environment, 20)
if err != nil {
panic(err)
}

medialogHostInfo, err := mlc.GetHostInfo()
if err != nil {
panic(err)
}

hostJSON, err := json.MarshalIndent(medialogHostInfo, "", " ")
if err != nil {
panic(err)
}

fmt.Printf("%s\n", hostJSON)

os.Exit(0)
}
5 changes: 5 additions & 0 deletions ml_client.config-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"local": {
"username": "username",
"password": "password",
"url": "http://localhost:8080"
}

0 comments on commit 5713ae6

Please sign in to comment.