github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/messaging/v2/claims/requests.go (about)

     1  package claims
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/vnpaycloud-console/gophercloud/v2"
     7  )
     8  
     9  // CreateOptsBuilder Builder.
    10  type CreateOptsBuilder interface {
    11  	ToClaimCreateRequest() (map[string]any, string, error)
    12  }
    13  
    14  // CreateOpts params to be used with Create.
    15  type CreateOpts struct {
    16  	// Sets the TTL for the claim. When the claim expires un-deleted messages will be able to be claimed again.
    17  	TTL int `json:"ttl,omitempty"`
    18  
    19  	// Sets the Grace period for the claimed messages. The server extends the lifetime of claimed messages
    20  	// to be at least as long as the lifetime of the claim itself, plus the specified grace period.
    21  	Grace int `json:"grace,omitempty"`
    22  
    23  	// Set the limit of messages returned by create.
    24  	Limit int `q:"limit" json:"-"`
    25  }
    26  
    27  // ToClaimCreateRequest assembles a body and URL for a Create request based on
    28  // the contents of a CreateOpts.
    29  func (opts CreateOpts) ToClaimCreateRequest() (map[string]any, string, error) {
    30  	q, err := gophercloud.BuildQueryString(opts)
    31  	if err != nil {
    32  		return nil, q.String(), err
    33  	}
    34  
    35  	b, err := gophercloud.BuildRequestBody(opts, "")
    36  	if err != nil {
    37  		return b, "", err
    38  	}
    39  	return b, q.String(), err
    40  }
    41  
    42  // Create creates a Claim that claims messages on a specified queue.
    43  func Create(ctx context.Context, client *gophercloud.ServiceClient, queueName string, opts CreateOptsBuilder) (r CreateResult) {
    44  	b, q, err := opts.ToClaimCreateRequest()
    45  	if err != nil {
    46  		r.Err = err
    47  		return
    48  	}
    49  
    50  	url := createURL(client, queueName)
    51  	if q != "" {
    52  		url += q
    53  	}
    54  
    55  	resp, err := client.Post(ctx, url, b, &r.Body, &gophercloud.RequestOpts{
    56  		OkCodes: []int{201, 204},
    57  	})
    58  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    59  	return
    60  }
    61  
    62  // Get queries the specified claim for the specified queue.
    63  func Get(ctx context.Context, client *gophercloud.ServiceClient, queueName string, claimID string) (r GetResult) {
    64  	resp, err := client.Get(ctx, getURL(client, queueName, claimID), &r.Body, &gophercloud.RequestOpts{
    65  		OkCodes: []int{200},
    66  	})
    67  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    68  	return
    69  }
    70  
    71  // UpdateOptsBuilder allows extensions to add additional parameters to the
    72  // Update request.
    73  type UpdateOptsBuilder interface {
    74  	ToClaimUpdateMap() (map[string]any, error)
    75  }
    76  
    77  // UpdateOpts implements UpdateOpts.
    78  type UpdateOpts struct {
    79  	// Update the TTL for the specified Claim.
    80  	TTL int `json:"ttl,omitempty"`
    81  
    82  	// Update the grace period for Messages in a specified Claim.
    83  	Grace int `json:"grace,omitempty"`
    84  }
    85  
    86  // ToClaimUpdateMap assembles a request body based on the contents of
    87  // UpdateOpts.
    88  func (opts UpdateOpts) ToClaimUpdateMap() (map[string]any, error) {
    89  	b, err := gophercloud.BuildRequestBody(opts, "")
    90  	if err != nil {
    91  		return nil, err
    92  	}
    93  	return b, nil
    94  }
    95  
    96  // Update will update the options for a specified claim.
    97  func Update(ctx context.Context, client *gophercloud.ServiceClient, queueName string, claimID string, opts UpdateOptsBuilder) (r UpdateResult) {
    98  	b, err := opts.ToClaimUpdateMap()
    99  	if err != nil {
   100  		r.Err = err
   101  		return r
   102  	}
   103  	resp, err := client.Patch(ctx, updateURL(client, queueName, claimID), &b, nil, &gophercloud.RequestOpts{
   104  		OkCodes: []int{204},
   105  	})
   106  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   107  	return
   108  }
   109  
   110  // Delete will delete a Claim for a specified Queue.
   111  func Delete(ctx context.Context, client *gophercloud.ServiceClient, queueName string, claimID string) (r DeleteResult) {
   112  	resp, err := client.Delete(ctx, deleteURL(client, queueName, claimID), &gophercloud.RequestOpts{
   113  		OkCodes: []int{204},
   114  	})
   115  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   116  	return
   117  }