Skip to content

Commit

Permalink
Read server version. (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkb-code authored Aug 27, 2024
1 parent 5e4de51 commit ba14fcc
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
2 changes: 2 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Client struct {
Relations *clientRelations
Fields *clientFields
Settings *clientSettings
Server *clientServer

instance, token string
logger *slog.Logger
Expand Down Expand Up @@ -76,6 +77,7 @@ func NewClient(instance string, token string, opts ...ClientOption) *Client {
client.Fields = &clientFields{client: client}
client.Relations = &clientRelations{client: client}
client.Settings = &clientSettings{client: client}
client.Server = &clientServer{client: client}

return client
}
Expand Down
42 changes: 42 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package directus

import (
"context"
"fmt"
"net/http"

"github.com/perimeterx/marshmallow"
)

type clientServer struct {
client *Client
}

type Info struct {
Version string `json:"version"`

Unknown map[string]any `json:"-"`
}

func (server *Info) UnmarshalJSON(data []byte) error {
values, err := marshmallow.Unmarshal(data, server, marshmallow.WithExcludeKnownFieldsFromMap(true))
if err != nil {
return err
}
server.Unknown = values
return nil
}

func (cr *clientServer) Info(ctx context.Context) (*Info, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, cr.client.urlf("/server/info"), nil)
if err != nil {
return nil, fmt.Errorf("directus: cannot prepare request: %v", err)
}
reply := struct {
Data *Info `json:"data"`
}{}
if err := cr.client.sendRequest(req, &reply); err != nil {
return nil, err
}
return reply.Data, nil
}
57 changes: 57 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package directus

import (
"context"
"encoding/json"
"testing"

"github.com/stretchr/testify/require"
)

func TestServerInfoUnmarshall(t *testing.T) {
data := []byte(`
{
"project": {
"project_name": "Directus",
"project_descriptor": null,
"project_logo": null,
"project_color": "#6644FF",
"default_appearance": "auto",
"default_theme_light": null,
"default_theme_dark": null,
"theme_light_overrides": null,
"theme_dark_overrides": null,
"default_language": "es-ES",
"public_foreground": null,
"public_favicon": null,
"public_note": null,
"custom_css": null,
"public_registration": false,
"public_registration_verify_email": true,
"public_background": null
},
"rateLimit": false,
"rateLimitGlobal": false,
"extensions": {
"limit": null
},
"queryLimit": {
"default": 100,
"max": -1
},
"websocket": false,
"version": "11.0.2"
}`,
)

var server Info
require.NoError(t, json.Unmarshal(data, &server))
require.EqualValues(t, server.Version, "11.0.2")
}

func TestServerInfo(t *testing.T) {
cli := initClient(t)
s, err := cli.Server.Info(context.Background())
require.NoError(t, err)
require.NotNil(t, s)
}

0 comments on commit ba14fcc

Please sign in to comment.