Skip to content

Commit

Permalink
Add fetchMTOUpdatesFast to Prime API Client
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandy Wright committed Jul 26, 2021
1 parent 91d2b40 commit cc1648b
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cmd/prime-api-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ func main() {
prime.InitFetchMTOUpdatesFlags(fetchMTOsCommand.Flags())
root.AddCommand(fetchMTOsCommand)

fetchMTOsFastCommand := &cobra.Command{
Use: "fetch-mto-updates-fast",
Short: "An optimized fetch for all moves available to Prime",
Long: "Fetches moves that are available to Prime quickly, without all the data from fetch-mto-updates.",
RunE: prime.FetchMTOUpdatesFast,
SilenceUsage: true,
}
prime.InitFetchMTOUpdatesFastFlags(fetchMTOsFastCommand.Flags())
root.AddCommand(fetchMTOsFastCommand)

getMoveTaskOrder := &cobra.Command{
Use: "get-move-task-order",
Short: "Get an individual mto",
Expand Down
98 changes: 98 additions & 0 deletions cmd/prime-api-client/prime/fetch_mto_updates_fast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package prime

import (
"encoding/json"
"fmt"
"log"
"os"
"time"

"github.com/go-openapi/strfmt"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"

"github.com/transcom/mymove/cmd/prime-api-client/utils"

mto "github.com/transcom/mymove/pkg/gen/primeclient/move_task_order"
)

// InitFetchMTOUpdatesFastFlags declares which flags are enabled
func InitFetchMTOUpdatesFastFlags(flag *pflag.FlagSet) {
flag.String(utils.SinceFlag, "", "Timestamp for filtering moves. Returns moves updated since this time.")
flag.SortFlags = false
}

func checkFetchMTOUpdatesFastConfig(v *viper.Viper, args []string, logger *log.Logger) error {
err := utils.CheckRootConfig(v)
if err != nil {
logger.Fatal(err)
}

return nil
}

// FetchMTOUpdatesFast creates a gateway and sends the request to the endpoint
func FetchMTOUpdatesFast(cmd *cobra.Command, args []string) error {
v := viper.New()

//Create the logger
//Remove the prefix and any datetime data
logger := log.New(os.Stdout, "", log.LstdFlags)

errParseFlags := utils.ParseFlags(cmd, v, args)
if errParseFlags != nil {
return errParseFlags
}

// Check the config before talking to the CAC
err := checkFetchMTOUpdatesFastConfig(v, args, logger)
if err != nil {
logger.Fatal(err)
}

// Get the since param, if any
var params mto.FetchMTOUpdatesFastParams
since := v.GetString(utils.SinceFlag)
if since != "" {
sinceDateTime, sinceErr := strfmt.ParseDateTime(since)
if sinceErr != nil {
logger.Fatal(err)
}
params.SetSince(&sinceDateTime)
}

primeGateway, cacStore, errCreateClient := utils.CreatePrimeClient(v)
if errCreateClient != nil {
return errCreateClient
}

// Defer closing the store until after the API call has completed
if cacStore != nil {
defer func() {
if closeErr := cacStore.Close(); closeErr != nil {
logger.Fatal(closeErr)
}
}()
}

params.SetTimeout(time.Second * 30)
resp, err := primeGateway.MoveTaskOrder.FetchMTOUpdatesFast(&params)
if err != nil {
return utils.HandleGatewayError(err, logger)
}

payload := resp.GetPayload()
if payload != nil {
payload, errJSONMarshall := json.Marshal(payload)
if errJSONMarshall != nil {
logger.Fatal(errJSONMarshall)
}
fmt.Println(string(payload))
} else {
logger.Fatal(resp.Error())
}

return nil
}
2 changes: 2 additions & 0 deletions cmd/prime-api-client/utils/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const (
FilenameFlag string = "filename"
// IDFlag is the UUID of the object being retrieved
IDFlag string = "id"
// SinceFlag is the datetime for the `since` filter for fetching moves
SinceFlag string = "since"
// ETagFlag is the etag for the mto shipment being updated
ETagFlag string = "etag"
// PaymentRequestIDFlag is the payment request ID
Expand Down

0 comments on commit cc1648b

Please sign in to comment.