github.com/Ingenico-ePayments/connect-sdk-go@v0.0.0-20240318153750-1f8cd329b9c9/internal/apiresource/APIResource.go (about) 1 package apiresource 2 3 import ( 4 "errors" 5 "strings" 6 7 "github.com/Ingenico-ePayments/connect-sdk-go/communicator" 8 "github.com/Ingenico-ePayments/connect-sdk-go/communicator/communication" 9 ) 10 11 // ErrNilParent occurs when the parent is nil 12 var ErrNilParent = errors.New("parent is nil") 13 14 // ErrNilCommunicator occurs when the communicator is nil 15 var ErrNilCommunicator = errors.New("communicator is nil") 16 17 // APIResource represents the base type of all Ingenico ePayments platform API resources. 18 type APIResource struct { 19 parentResource *APIResource 20 communicator *communicator.Communicator 21 clientMetaInfo string 22 pathContext map[string]string 23 errorObject error 24 } 25 26 // Communicator returns the Communicator used by the resource 27 func (ar *APIResource) Communicator() *communicator.Communicator { 28 return ar.communicator 29 } 30 31 // ClientMetaInfo returns the ClientMetaInfo used by the resource 32 func (ar *APIResource) ClientMetaInfo() string { 33 return ar.clientMetaInfo 34 } 35 36 // ClientHeaders returns the headers used by the resource 37 func (ar *APIResource) ClientHeaders() []communication.Header { 38 if len(ar.clientMetaInfo) != 0 { 39 header, _ := communication.NewHeader("X-GCS-ClientMetaInfo", ar.clientMetaInfo) 40 41 return []communication.Header{*header} 42 } 43 44 return nil 45 } 46 47 // InstantiateURIWithContext instantiates the given URI with the path context 48 func (ar *APIResource) InstantiateURIWithContext(uri string, pathContext map[string]string) (string, error) { 49 return ar.InstantiateURI(replaceAll(uri, pathContext)) 50 } 51 52 func (ar *APIResource) getError() error { 53 if ar.parentResource != nil { 54 if parentError := ar.parentResource.getError(); parentError != nil { 55 return parentError 56 } 57 if ar.errorObject != nil { 58 err := ar.errorObject 59 ar.errorObject = nil 60 61 return err 62 } 63 } 64 return nil 65 } 66 67 // InstantiateURI instantiates the given uri with the path context of the resource 68 func (ar *APIResource) InstantiateURI(uri string) (string, error) { 69 if err := ar.getError(); err != nil { 70 return "", err 71 } 72 uri = replaceAll(uri, ar.pathContext) 73 74 if ar.parentResource != nil { 75 uriParent, err := ar.parentResource.InstantiateURI(uri) 76 77 if err != nil { 78 return uriParent, err 79 } 80 81 uri = uriParent 82 } 83 84 return uri, nil 85 } 86 87 // NewAPIResourceWithParent creates an APIResource with the given parent and pathContext 88 func NewAPIResourceWithParent(parent *APIResource, pathContext map[string]string) *APIResource { 89 ar := &APIResource{parent, parent.communicator, parent.clientMetaInfo, pathContext, nil} 90 if parent == nil { 91 ar.errorObject = ErrNilParent 92 } 93 return ar 94 } 95 96 // NewAPIResource creates an APIResource with the given communicator, clientMetaInfo and pathContext 97 func NewAPIResource(communicator *communicator.Communicator, clientMetaInfo string, pathContext map[string]string) (*APIResource, error) { 98 ar := &APIResource{nil, communicator, clientMetaInfo, pathContext, nil} 99 if communicator == nil { 100 ar.errorObject = ErrNilCommunicator 101 return ar, ar.errorObject 102 } 103 return ar, nil 104 } 105 106 func replaceAll(uri string, pathContext map[string]string) string { 107 if pathContext != nil { 108 for key, value := range pathContext { 109 uri = strings.Replace(uri, "{" + key + "}", value, -1) 110 } 111 } 112 113 return uri 114 }