Skip to content

Commit

Permalink
mnz-client: print JSON with jwtToken and n remaining runs
Browse files Browse the repository at this point in the history
  • Loading branch information
Snyssfx committed Feb 19, 2025
1 parent 7697977 commit c35b1e6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
10 changes: 8 additions & 2 deletions mnz-client/cmd/mnz-client/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"log"
Expand Down Expand Up @@ -84,10 +85,15 @@ func main() {
if *dryRun {
*url = *dryRunUrl
}
jwt, err := mnz.CallRunSpec(req, *url, *retryWaitMin, *retryWaitMax, *retryMax)
result, err := mnz.CallRunSpec(req, *url, *retryWaitMin, *retryWaitMax, *retryMax)
if err != nil {
log.Fatal(err)
}

fmt.Println(jwt)
bytes, err := json.Marshal(result)
if err != nil {
log.Fatal(err)
}

fmt.Println(string(bytes))
}
33 changes: 17 additions & 16 deletions mnz-client/internal/mnz/mnz.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ type RunSpecRequest struct {
RunSpec map[string]Arg `json:"runSpec"`
}
type RunSpecResResult struct {
JwtToken string `json:"jwtToken"`
JwtToken string `json:"jwtToken"`
NRemainingRuns *uint64 `json:"nRemainingRuns,omitempty"`
}
type RunSpecResError struct {
Code string `json:"code"`
Expand Down Expand Up @@ -91,7 +92,7 @@ func PrepareArgs(args []string) (map[string]Arg, error) {
}
}
var runSpecs map[string]any
//for _, specName := range ArgSpecNames {
// for _, specName := range ArgSpecNames {
switch argType.Name {
case ArgTypeFile:
runSpecs, err = fileSpecs(splittedArgs[filepathN], ArgSpecNames)
Expand All @@ -116,12 +117,11 @@ func CallRunSpec(
retryWaitMin int,
retryWaitMax int,
retryMax int,
) (string, error) {

) (*RunSpecResResult, error) {
// Serialize the request to JSON
data, err := json.Marshal(runSpecRequest)
if err != nil {
return "", fmt.Errorf("failed to serialize request: %w", err)
return nil, fmt.Errorf("failed to serialize request: %w", err)
}

// Create a retryable HTTP client
Expand All @@ -134,45 +134,46 @@ func CallRunSpec(
// Create the POST request
req, err := retryablehttp.NewRequest("POST", url, bytes.NewBuffer(data))
if err != nil {
return "", fmt.Errorf("failed to create request: %w", err)
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", "mnz-client")

// Perform the request
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("request failed: %w", err)
return nil, fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()

// Check for non-200 HTTP status
if resp.StatusCode != 200 {
return "", fmt.Errorf("received non-200 response: %d", resp.StatusCode)
return nil, fmt.Errorf("received non-200 response: %d", resp.StatusCode)
}

// Parse the response
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response: %w", err)
return nil, fmt.Errorf("failed to read response: %w", err)
}

jwt, err := unmarshal(body)
result, err := unmarshal(body)
if err != nil {
return "", err
return nil, err
}
return jwt, nil

return result, nil
}

func unmarshal(body []byte) (string, error) {
func unmarshal(body []byte) (*RunSpecResResult, error) {
result := RunSpecRes{}
jsonErr := json.Unmarshal(body, &result)
if jsonErr != nil {
return "", jsonErr
return nil, jsonErr
}

if result.Error.Code != "" {
return "", fmt.Errorf("get API error: %s %s", result.Error.Code, result.Error.Message)
return nil, fmt.Errorf("get API error: %s %s", result.Error.Code, result.Error.Message)
}
return result.Result.JwtToken, nil
return &result.Result, nil
}
11 changes: 7 additions & 4 deletions mnz-client/internal/mnz/mnz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mnz

import (
"fmt"
"reflect"
"testing"
)

Expand All @@ -12,7 +13,7 @@ func Test_unmarshal(t *testing.T) {
tests := []struct {
name string
args args
want string
want *RunSpecResResult
wantErr error
}{
{
Expand All @@ -27,7 +28,7 @@ func Test_unmarshal(t *testing.T) {
}
`),
},
want: "",
want: nil,
wantErr: fmt.Errorf("get API error: VALIDATION_ERR bla"),
},
{
Expand All @@ -41,7 +42,9 @@ func Test_unmarshal(t *testing.T) {
}
`),
},
want: "eyJhbGciOiJSUzI1NiJ9.eyJwcm9kdWN0TmFtZSI6InRZWMiOnsiYXNkZiI6IjEyMzQ1In19.bCZuyt1",
want: &RunSpecResResult{
JwtToken: "eyJhbGciOiJSUzI1NiJ9.eyJwcm9kdWN0TmFtZSI6InRZWMiOnsiYXNkZiI6IjEyMzQ1In19.bCZuyt1",
},
wantErr: nil,
},
}
Expand All @@ -54,7 +57,7 @@ func Test_unmarshal(t *testing.T) {
t.Errorf("unmarshal() error = %v, wantErr %v", err.Error(), tt.wantErr)
return
}
if got != tt.want {
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("unmarshal() got = %v, want %v", got, tt.want)
}
})
Expand Down

0 comments on commit c35b1e6

Please sign in to comment.