Skip to content

Commit

Permalink
mnz-cleint: add fakeProductKey, return jwt token as before
Browse files Browse the repository at this point in the history
  • Loading branch information
Snyssfx committed Feb 21, 2025
1 parent 17ce8a7 commit 43df308
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
21 changes: 14 additions & 7 deletions mnz-client/cmd/mnz-client/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"log"
Expand All @@ -12,6 +11,14 @@ import (
"github.com/milaboratory/small-binaries/mnz-client/internal/mnz"
)

const (
// fakeProductKey is needed for tests of our workflow.
fakeProductKey = "MIFAKEMIFAKEMIFAKE"

// fakeResponseToken is a response for fakeProductKey, it contains a number of remaining runs.
fakeResponseToken = "eyJhbGciOiJFUzI1NiIsImtpZCI6Im1pMiJ9.eyJtbnoiOnsiZGV0YWlscyI6eyJyZW1haW5pbmciOiA5OTk5OTJ9LCJ0eXBlIjoiYmFzZSJ9LCJwcm9kdWN0S2V5IjoiTUlGQUtFTUlGQUtFTUlGQUtFIiwicnVuU3BlYyI6eyJrZXkiOiJ2YWx1ZSJ9fQ==.K7pU8XE476enl-wI-rnHXnvCGLGfM0mdDS0HPdIXhnE5tuc1nKcSZMMTZSZ6USSc1_syHhDkrjsm7UvZTcQwqg"
)

func main() {
// define flags
flag.Usage = func() {
Expand Down Expand Up @@ -76,6 +83,11 @@ func main() {
log.Fatal(err)
}

if *productKey == fakeProductKey {
fmt.Println(fakeResponseToken)
return
}

// call
req := mnz.RunSpecRequest{
License: license,
Expand All @@ -90,10 +102,5 @@ func main() {
log.Fatal(err)
}

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

fmt.Println(string(bytes))
fmt.Println(result)
}
19 changes: 9 additions & 10 deletions mnz-client/internal/mnz/mnz.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ type RunSpecRequest struct {
RunSpec map[string]Arg `json:"runSpec"`
}
type RunSpecResResult struct {
JwtToken string `json:"jwtToken"`
NRemainingRuns *uint64 `json:"nRemainingRuns,omitempty"`
JwtToken string `json:"jwtToken"`
}
type RunSpecResError struct {
Code string `json:"code"`
Expand Down Expand Up @@ -117,11 +116,11 @@ func CallRunSpec(
retryWaitMin int,
retryWaitMax int,
retryMax int,
) (*RunSpecResResult, error) {
) (string, error) {
// Serialize the request to JSON
data, err := json.Marshal(runSpecRequest)
if err != nil {
return nil, fmt.Errorf("failed to serialize request: %w", err)
return "", fmt.Errorf("failed to serialize request: %w", err)
}

// Create a retryable HTTP client
Expand All @@ -134,35 +133,35 @@ func CallRunSpec(
// Create the POST request
req, err := retryablehttp.NewRequest("POST", url, bytes.NewBuffer(data))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
return "", 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 nil, fmt.Errorf("request failed: %w", err)
return "", fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()

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

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

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

return result, nil
return result.JwtToken, nil
}

func unmarshal(body []byte) (*RunSpecResResult, error) {
Expand Down

0 comments on commit 43df308

Please sign in to comment.