Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dmnyu committed Sep 9, 2024
0 parents commit e59314c
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ml_client.config
34 changes: 34 additions & 0 deletions Entries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"encoding/json"
"fmt"
"io"
"net/http"
)

func (mlc MedialogClient) GetEntryUUIDs() ([]string, error) {
url := fmt.Sprintf("%s/entries?all_ids=true", mlc.BaseURL)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return []string{}, err
}
req.Header.Add("X-Medialog-Token", mlc.Token)
resp, err := mlc.Client.Do(req)
if err != nil {
return []string{}, err
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return []string{}, err
}

uuids := []string{}
if err := json.Unmarshal(body, &uuids); err != nil {
return uuids, err
}

return uuids, nil

}
117 changes: 117 additions & 0 deletions MedialogClient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"time"

"github.com/nyudlts/go-medialog/models"
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"`
Password string `yaml:"password"`
}

type MedialogClient struct {
Token string
BaseURL string
Client *http.Client
}

const API_ROOT = "/api/v0"

func NewClient(creds Creds, timeout int) (MedialogClient, error) {
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
}

func getCreds(config string, environment string) (Creds, error) {
credsMap := map[string]Creds{}
configBytes, err := os.ReadFile(config)
if err != nil {
return Creds{}, err
}

if err = yaml.Unmarshal(configBytes, &credsMap); err != nil {
return Creds{}, err
}

for k, v := range credsMap {
if environment == k {
return v, nil
}
}

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

func (mlClient *MedialogClient) getToken(creds Creds) {
url := fmt.Sprintf("%s/users/%s/login?password=%s", mlClient.BaseURL, creds.Username, creds.Password)
response, err := mlClient.Client.Post(url, "", nil)
if err != nil {
panic(err.Error())
}
body, err := io.ReadAll(response.Body)
if err != nil {
panic(err)
}

token := models.Token{}
if err := json.Unmarshal(body, &token); err != nil {
panic(err)
}

mlClient.Token = token.Token
}

func (mlc MedialogClient) GetRoot() (models.MedialogInfo, error) {
mlInfo := models.MedialogInfo{}
req, err := http.NewRequest("GET", mlc.BaseURL, nil)
if err != nil {
return mlInfo, err
}

resp, err := mlc.Client.Do(req)
if err != nil {
return mlInfo, err
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return mlInfo, err
}

if err := json.Unmarshal(body, &mlInfo); err != nil {
return mlInfo, err
}

return mlInfo, nil

}
34 changes: 34 additions & 0 deletions MedialogClient_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"flag"
"testing"
)

var mlc MedialogClient

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

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)
if err != nil {
t.Error(err)
}
})

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

mlInfo, err := mlc.GetRoot()
if err != nil {
t.Error(err)
}

t.Log(mlInfo)
})
}
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/nyudlts/medialog-client

go 1.22.3

require gopkg.in/yaml.v2 v2.4.0

require (
github.com/google/uuid v1.6.0 // indirect
github.com/nyudlts/go-medialog v1.0.6 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/nyudlts/go-medialog v1.0.6 h1:2r+4Oo8UkiI4Xw4qSj4ll3ISQwUGgsUCWPbMPiJenk0=
github.com/nyudlts/go-medialog v1.0.6/go.mod h1:rAuCRwnyBNxTraoobLVIGz8ARiPnUsxTRodFxghCSPY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

0 comments on commit e59314c

Please sign in to comment.