Repository: Nerzal/gocloak
Pull request #69 (2019-05-31 05:07:57)

Add GetServerInfo function
GetServerInfo function reurns a representation of ServerInfo.
This implementation supports only SystemInfo and MemoryInfo fields of ServerInfo.



Patch:
diff --git a/README.md b/README.md
index 32fdf80..42fe92b 100644
--- a/README.md
+++ b/README.md
@@ -91,6 +91,7 @@ type GoCloak interface {
 	RetrospectToken(accessToken string, clientID, clientSecret string, realm string) (*RetrospecTokenResult, error)
 	GetIssuer(realm string) (*IssuerResponse, error)
 	GetCerts(realm string) (*CertResponse, error)
+	GetServerInfo(accessToken string) (*ServerInfoRepesentation, error)
 	GetUserInfo(accessToken string, realm string) (*UserInfo, error)
 	SetPassword(token string, userID string, realm string, password string, temporary bool) error
 	ExecuteActionsEmail(token string, realm string, params ExecuteActionsEmail) error
diff --git a/client.go b/client.go
index a3949c6..ec5aa52 100644
--- a/client.go
+++ b/client.go
@@ -107,6 +107,19 @@ func (client *gocloak) getAdminRealmURL(realm string, path ...string) string {
 	return makeURL(path...)
 }
 
+func (client *gocloak) GetServerInfo(accessToken string) (*ServerInfoRepesentation, error) {
+	var result ServerInfoRepesentation
+	resp, err := client.getRequestWithBearerAuth(accessToken).
+		SetResult(&result).
+		Get(makeURL(client.basePath, "auth", "admin", "serverinfo"))
+
+	if err := checkForError(resp, err); err != nil {
+		return nil, err
+	}
+
+	return &result, nil
+}
+
 // GetUserInfo calls the UserInfo endpoint
 func (client *gocloak) GetUserInfo(accessToken string, realm string) (*UserInfo, error) {
 	var result UserInfo
diff --git a/gocloak.go b/gocloak.go
index 45d18ec..eada3a2 100644
--- a/gocloak.go
+++ b/gocloak.go
@@ -34,6 +34,8 @@ type GoCloak interface {
 	GetIssuer(realm string) (*IssuerResponse, error)
 	// GetCerts gets the public keys for the given realm
 	GetCerts(realm string) (*CertResponse, error)
+	// GetServerInfo returns the server info
+	GetServerInfo(accessToken string) (*ServerInfoRepesentation, error)
 	// GetUserInfo gets the user info for the given realm
 	GetUserInfo(accessToken string, realm string) (*UserInfo, error)
 
diff --git a/models.go b/models.go
index 829635b..275caba 100644
--- a/models.go
+++ b/models.go
@@ -565,3 +565,42 @@ type UserSessionRepresentation struct {
 	UserID     string            `json:"userId,omitempty"`
 	Username   string            `json:"username,omitempty"`
 }
+
+// SystemInfoRepresentation represents a system info
+type SystemInfoRepresentation struct {
+	FileEncoding   string `json:"fileEncoding"`
+	JavaHome       string `json:"javaHome"`
+	JavaRuntime    string `json:"javaRuntime,omitempty"`
+	JavaVendor     string `json:"javaVendor,omitempty"`
+	JavaVersion    string `json:"javaVersion,omitempty"`
+	JavaVM         string `json:"javaVm,omitempty"`
+	JavaVMVersion  string `json:"javaVmVersion,omitempty"`
+	OSArchitecture string `json:"osArchitecture,omitempty"`
+	OSName         string `json:"osName,omitempty"`
+	OSVersion      string `json:"osVersion,omitempty"`
+	ServerTime     string `json:"serverTime,omitempty"`
+	Uptime         string `json:"uptime,omitempty"`
+	UptimeMillis   int    `json:"uptimeMillis,omitempty"`
+	UserDir        string `json:"userDir,omitempty"`
+	UserLocale     string `json:"userLocale,omitempty"`
+	UserName       string `json:"userName,omitempty"`
+	UserTimezone   string `json:"userTimezone,omitempty"`
+	Version        string `json:"version,omitempty"`
+}
+
+// MemoryInfoRepresentation represents a memory info
+type MemoryInfoRepresentation struct {
+	Free           int    `json:"free,omitempty"`
+	FreeFormated   string `json:"freeFormated,omitempty"`
+	FreePercentage int    `json:"freePercentage,omitempty"`
+	Total          int    `json:"total,omitempty"`
+	TotalFormated  string `json:"totalFormated,omitempty"`
+	Used           int    `json:"used,omitempty"`
+	UsedFormated   string `json:"usedFormated,omitempty"`
+}
+
+// ServerInfoRepesentation represents a server info
+type ServerInfoRepesentation struct {
+	SystemInfo SystemInfoRepresentation `json:"systemInfo,omitempty"`
+	MemoryInfo MemoryInfoRepresentation `json:"memoryInfo"`
+}


Test patch:
diff --git a/client_test.go b/client_test.go
index 77a82d4..d760fc5 100644
--- a/client_test.go
+++ b/client_test.go
@@ -321,6 +321,18 @@ func TestGocloak_RestyClient(t *testing.T) {
 // API tests
 // ---------
 
+func TestGocloak_GetServerInfo(t *testing.T) {
+	t.Parallel()
+	client := NewClientWithDebug(t)
+	token := GetAdminToken(t, client)
+
+	serverInfo, err := client.GetServerInfo(
+		token.AccessToken,
+	)
+	FailIfErr(t, err, "Failed to fetch server info")
+	t.Logf("Server Info: %+v", serverInfo)
+}
+
 func TestGocloak_GetUserInfo(t *testing.T) {
 	t.Parallel()
 	cfg := GetConfig(t)
@@ -330,8 +342,8 @@ func TestGocloak_GetUserInfo(t *testing.T) {
 	userInfo, err := client.GetUserInfo(
 		token.AccessToken,
 		cfg.GoCloak.Realm)
-	t.Log(userInfo)
 	FailIfErr(t, err, "Failed to fetch userinfo")
+	t.Log(userInfo)
 }
 
 func TestGocloak_RequestPermission(t *testing.T) {