From 147e2b5b354131e2dd8a1b9708dc2561248d3f51 Mon Sep 17 00:00:00 2001 From: Mohammed Diaa Date: Mon, 30 Oct 2023 16:14:35 +0100 Subject: [PATCH] Update fleet endpoint --- types/user.go | 9 +++++++++ util/util.go | 27 +++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/types/user.go b/types/user.go index debade6..96716f4 100644 --- a/types/user.go +++ b/types/user.go @@ -44,6 +44,15 @@ type VaultInfo struct { ModifiedDate time.Time `json:"modified_date"` } +type Fleets struct { + Next string `json:"next"` + Previous string `json:"previous"` + Page int `json:"page"` + Last int `json:"last"` + Count int `json:"count"` + Results []Fleet `json:"results"` +} + type Fleet struct { ID uuid.UUID `json:"id"` Name string `json:"name"` diff --git a/util/util.go b/util/util.go index 7d46901..f9a735b 100644 --- a/util/util.go +++ b/util/util.go @@ -100,15 +100,38 @@ func GetMe() *types.User { } func GetFleetInfo() *types.Fleet { - resp := request.Trickest.Get().DoF("fleet/%s", GetVault()) + fleet := GetFleetInfoLegacy() + if fleet != nil { + return fleet + } + resp := request.Trickest.Get().DoF("fleet/?vault=%s", GetVault()) if resp == nil || resp.Status() != http.StatusOK { request.ProcessUnexpectedResponse(resp) } + var fleets types.Fleets + err := json.Unmarshal(resp.Body(), &fleets) + if err != nil { + fmt.Println("Error unmarshalling fleet response!") + return nil + } + + if len(fleets.Results) == 0 { + fmt.Println("Error: Couldn't find any active fleets") + } + + return &fleets.Results[0] +} + +func GetFleetInfoLegacy() *types.Fleet { + resp := request.Trickest.Get().DoF("fleet/%s", GetVault()) + if resp == nil || resp.Status() != http.StatusOK { + return nil + } + var fleet types.Fleet err := json.Unmarshal(resp.Body(), &fleet) if err != nil { - fmt.Println("Error unmarshalling fleet response!") return nil }