github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/pkg/platform/api/inventory/inventory.go (about) 1 package inventory 2 3 import ( 4 "encoding/json" 5 "io" 6 "net/http" 7 "net/url" 8 9 "github.com/go-openapi/runtime" 10 httptransport "github.com/go-openapi/runtime/client" 11 "github.com/go-openapi/strfmt" 12 13 "github.com/ActiveState/cli/internal/locale" 14 "github.com/ActiveState/cli/pkg/platform/api" 15 "github.com/ActiveState/cli/pkg/platform/api/inventory/inventory_client/inventory_operations" 16 iop "github.com/ActiveState/cli/pkg/platform/api/inventory/inventory_client/inventory_operations" 17 "github.com/ActiveState/cli/pkg/platform/authentication" 18 ) 19 20 // persist contains the active API Client connection 21 var persist inventory_operations.ClientService 22 23 // Init will create a new API client using default settings 24 func Init(auth *authentication.Auth) (inventory_operations.ClientService, runtime.ClientTransport) { 25 var authWriter runtime.ClientAuthInfoWriter 26 if auth != nil { 27 authWriter = auth.ClientAuth() 28 } 29 return New(api.GetServiceURL(api.ServiceInventory), authWriter) 30 } 31 32 // New initializes a new api client 33 func New(serviceURL *url.URL, auth runtime.ClientAuthInfoWriter) (inventory_operations.ClientService, runtime.ClientTransport) { 34 transportRuntime := httptransport.New(serviceURL.Host, serviceURL.Path, []string{serviceURL.Scheme}) 35 transportRuntime.Transport = api.NewRoundTripper(http.DefaultTransport) 36 37 // transportRuntime.SetDebug(true) 38 39 if auth != nil { 40 transportRuntime.DefaultAuthentication = auth 41 } 42 43 return inventory_operations.New(transportRuntime, strfmt.Default), transportRuntime 44 } 45 46 // Get returns a cached version of the default api client 47 func Get(auth *authentication.Auth) inventory_operations.ClientService { 48 if persist == nil { 49 persist, _ = Init(auth) 50 } 51 return persist 52 } 53 54 type RecipesResponse struct { 55 Recipes []interface{} 56 } 57 58 func ResolveRecipes(transport runtime.ClientTransport, params *iop.ResolveRecipesParams, authInfo runtime.ClientAuthInfoWriter) (string, error) { 59 if params == nil { 60 params = iop.NewResolveRecipesParams() 61 } 62 63 result, err := transport.Submit(&runtime.ClientOperation{ 64 ID: "resolveRecipes", 65 Method: "POST", 66 PathPattern: "/v1/recipes", 67 ProducesMediaTypes: []string{"application/json"}, 68 ConsumesMediaTypes: []string{"application/json"}, 69 Schemes: []string{"http"}, 70 Params: params, 71 Reader: &RawResponder{}, 72 AuthInfo: authInfo, 73 Context: params.Context, 74 Client: params.HTTPClient, 75 }) 76 if err != nil { 77 return "", err 78 } 79 80 return string(result.([]byte)), nil 81 } 82 83 type RawResponder struct{} 84 85 func (r *RawResponder) ReadResponse(res runtime.ClientResponse, cons runtime.Consumer) (interface{}, error) { 86 defer res.Body().Close() 87 bytes, err := io.ReadAll(res.Body()) 88 if err != nil { 89 return nil, err 90 } 91 92 var umRecipe RecipesResponse 93 err = json.Unmarshal(bytes, &umRecipe) 94 if err != nil { 95 return nil, err 96 } 97 98 if len(umRecipe.Recipes) == 0 { 99 return nil, locale.NewError(locale.T("err_no_recipes")) 100 } 101 102 return json.Marshal(umRecipe.Recipes[0]) 103 }