fix: PascalCase JSON fields + code:200 success + Unix timestamp ExpDate
This commit is contained in:
@@ -114,7 +114,9 @@ func (c *client) do(ctx context.Context, method, path string, body, result any,
|
|||||||
|
|
||||||
var apiResp apiResponse
|
var apiResp apiResponse
|
||||||
if err := json.Unmarshal(respBody, &apiResp); err == nil {
|
if err := json.Unmarshal(respBody, &apiResp); err == nil {
|
||||||
if resp.StatusCode >= 400 || apiResp.Code != 0 {
|
// Rainyun uses code:200 and code:0 both as success indicators
|
||||||
|
isError := resp.StatusCode >= 400 || (apiResp.Code != 0 && apiResp.Code != 200)
|
||||||
|
if isError {
|
||||||
return &APIError{
|
return &APIError{
|
||||||
StatusCode: resp.StatusCode,
|
StatusCode: resp.StatusCode,
|
||||||
Method: method,
|
Method: method,
|
||||||
|
|||||||
@@ -13,9 +13,8 @@ func listOptions() map[string]string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// listResponse is the JSON envelope returned by Rainyun list endpoints.
|
// listResponse is the JSON envelope returned by Rainyun list endpoints.
|
||||||
// The API wraps data in {"code":0,"data":{"records":[...],"totalRecords":N}}.
|
// API uses PascalCase: {"code":200,"data":{"Records":[...],"TotalRecords":N}}
|
||||||
// Our client extracts apiResp.Data ({"records":[...]}), then we unmarshal into this.
|
|
||||||
type listResponse struct {
|
type listResponse struct {
|
||||||
Records []any `json:"records"`
|
Records []any `json:"Records"`
|
||||||
TotalRecords int `json:"totalRecords"`
|
TotalRecords int `json:"TotalRecords"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,9 +13,9 @@ type ServerService struct {
|
|||||||
client *client
|
client *client
|
||||||
}
|
}
|
||||||
|
|
||||||
// rainyunServer mirrors the Rainyun RCS API JSON fields.
|
// rainyunServer mirrors the Rainyun RCS API JSON fields (PascalCase).
|
||||||
type rainyunServer struct {
|
type rainyunServer struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"ID"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
IP string `json:"ip"`
|
IP string `json:"ip"`
|
||||||
@@ -24,7 +24,7 @@ type rainyunServer struct {
|
|||||||
Disk int `json:"disk"`
|
Disk int `json:"disk"`
|
||||||
OS string `json:"os"`
|
OS string `json:"os"`
|
||||||
Region string `json:"zone_name"`
|
Region string `json:"zone_name"`
|
||||||
ExpireDate string `json:"expire_date"`
|
ExpireDate int64 `json:"ExpDate"` // Unix timestamp
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ServerService) List(ctx context.Context) ([]provider.Server, error) {
|
func (s *ServerService) List(ctx context.Context) ([]provider.Server, error) {
|
||||||
@@ -87,7 +87,7 @@ func (s *ServerService) Upgrade(ctx context.Context, id, plan string) error {
|
|||||||
|
|
||||||
// toServer maps a rainyunServer to the provider.Server model.
|
// toServer maps a rainyunServer to the provider.Server model.
|
||||||
func toServer(r rainyunServer) provider.Server {
|
func toServer(r rainyunServer) provider.Server {
|
||||||
expire, _ := time.Parse("2006-01-02", r.ExpireDate)
|
expire := time.Unix(r.ExpireDate, 0)
|
||||||
return provider.Server{
|
return provider.Server{
|
||||||
ID: fmt.Sprintf("%d", r.ID),
|
ID: fmt.Sprintf("%d", r.ID),
|
||||||
Name: r.Name,
|
Name: r.Name,
|
||||||
|
|||||||
@@ -16,9 +16,9 @@ func TestServerList(t *testing.T) {
|
|||||||
resp := map[string]any{
|
resp := map[string]any{
|
||||||
"code": 0,
|
"code": 0,
|
||||||
"data": map[string]any{
|
"data": map[string]any{
|
||||||
"records": []map[string]any{
|
"Records": []map[string]any{
|
||||||
{
|
{
|
||||||
"id": 12001,
|
"ID": 12001,
|
||||||
"name": "web-server",
|
"name": "web-server",
|
||||||
"status": "running",
|
"status": "running",
|
||||||
"ip": "1.2.3.4",
|
"ip": "1.2.3.4",
|
||||||
@@ -26,10 +26,10 @@ func TestServerList(t *testing.T) {
|
|||||||
"memory": 4096,
|
"memory": 4096,
|
||||||
"disk": 50,
|
"disk": 50,
|
||||||
"os": "Ubuntu 22.04",
|
"os": "Ubuntu 22.04",
|
||||||
"expire_date": "2026-08-15",
|
"ExpDate": 1756656000,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"totalRecords": 1,
|
"TotalRecords": 1,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
json.NewEncoder(w).Encode(resp)
|
json.NewEncoder(w).Encode(resp)
|
||||||
|
|||||||
Reference in New Issue
Block a user