Documentation
¶
Index ¶
- Variables
- type APIPayload
- type APIResponse
- type ObjectOptions
- type ReadSearch
- type RestObject
- func (ro *RestObject) Create(ctx context.Context) error
- func (ro *RestObject) Delete(ctx context.Context) error
- func (ro *RestObject) Find(ctx context.Context, queryString, searchKey, searchValue, resultKey string) (APIResponse, error)
- func (ro *RestObject) Read(ctx context.Context) error
- func (ro *RestObject) ToString() string
- func (ro *RestObject) Update(ctx context.Context) error
Constants ¶
This section is empty.
Variables ¶
var ( ErrFindObject = errors.New("failed to find object") ErrFindResponse = errors.New("failed to read response from find object") )
var ErrCreateObject = errors.New("failed to create object")
var ErrInvalidObjectOptions = errors.New("invalid object options")
var ErrReadObject = errors.New("failed to read object")
var ErrUpdateObject = errors.New("failed to update object")
Functions ¶
This section is empty.
Types ¶
type APIPayload ¶
APIPayload is a map that contains arbitrary JSON-serializable data representing the payload of an API request.
type APIResponse ¶
APIResponse is a map that contains arbitrary response data.
type ObjectOptions ¶
type ObjectOptions struct { Path string PostPath string GetPath string PutPath string DeletePath string CreateMethod string ReadMethod string UpdateMethod string DeleteMethod string QueryString string ReadSearch *ReadSearch ID string IDAttribute string // Set internally Data APIPayload // Data as managed by the user UpdateData APIPayload // Update data as managed by the user DestroyData APIPayload // Destroy data as managed by the user APIResponse APIResponse // Data as available from the API APIResponseRaw string CreateResponseRaw string }
type ReadSearch ¶
type RestObject ¶
type RestObject struct { Options *ObjectOptions // contains filtered or unexported fields }
APIObject is the state holding struct for a restapi_object resource.
func New ¶
func New(client *restclient.RestClient, opts *ObjectOptions) (*RestObject, error)
New creates a new RestObject instance with the given client and options. It sets default values for the object options if they are not provided, using values from the client options. It also tries to set the object ID from the provided data if an ID is not already set in the options.
func (*RestObject) Create ¶
func (ro *RestObject) Create(ctx context.Context) error
Create uses the RestObject's client to send a POST request to create the object on the remote API. It handles setting the object's ID from the response and syncing the object's state.
func (*RestObject) Delete ¶
func (ro *RestObject) Delete(ctx context.Context) error
Delete deletes the RestObject from the API by sending a DELETE request. It returns an error if the delete request fails.
func (*RestObject) Find ¶
func (ro *RestObject) Find( ctx context.Context, queryString, searchKey, searchValue, resultKey string, ) (APIResponse, error)
Find searches the REST API for an object matching the given search criteria. It issues a GET request to the API path, optionally adding the queryString. It parses the JSON response, extracting the result array at resultKey. It loops through the array looking for an object where searchKey equals searchValue. If found, it returns that object as the APIResponse. It also extracts the ID attribute into the RestObject options.
func (*RestObject) Read ¶
func (ro *RestObject) Read(ctx context.Context) error
Read retrieves the RestObject from the API based on the configured ID and options. It handles errors and unset IDs. It can also search the response and return a matched object.
func (*RestObject) ToString ¶
func (ro *RestObject) ToString() string
ToString returns a string representation of the RestObject options.
func (*RestObject) Update ¶
func (ro *RestObject) Update(ctx context.Context) error
Update updates the RestObject by sending a PUT request to the API. It returns an error if the ID is not set, if there is an error building the request data, or if there is an error sending the request.
If write_returns_object is true, it will parse the response and update the RestObject. Otherwise it will re-read the object from the API after the update.