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

     1  package messages
     2  
     3  import (
     4  	"github.com/vnpaycloud-console/gophercloud/v2"
     5  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
     6  )
     7  
     8  // CreateResult is the response of a Create operations.
     9  type CreateResult struct {
    10  	gophercloud.Result
    11  }
    12  
    13  // MessagePage contains a single page of all clusters from a ListDetails call.
    14  type MessagePage struct {
    15  	pagination.LinkedPageBase
    16  }
    17  
    18  // DeleteResult is the result from a Delete operation. Call its ExtractErr
    19  // method to determine if the call succeeded or failed.
    20  type DeleteResult struct {
    21  	gophercloud.ErrResult
    22  }
    23  
    24  // CreateResult is the response of a Create operations.
    25  type PopResult struct {
    26  	gophercloud.Result
    27  }
    28  
    29  // GetMessagesResult is the response of a GetMessages operations.
    30  type GetMessagesResult struct {
    31  	gophercloud.Result
    32  }
    33  
    34  // GetResult is the response of a Get operations.
    35  type GetResult struct {
    36  	gophercloud.Result
    37  }
    38  
    39  // Message represents a message on a queue.
    40  type Message struct {
    41  	Body     map[string]any `json:"body"`
    42  	Age      int            `json:"age"`
    43  	Href     string         `json:"href"`
    44  	ID       string         `json:"id"`
    45  	TTL      int            `json:"ttl"`
    46  	Checksum string         `json:"checksum"`
    47  }
    48  
    49  // PopMessage represents a message returned from PopMessages.
    50  type PopMessage struct {
    51  	Body       map[string]any `json:"body"`
    52  	Age        int            `json:"age"`
    53  	ID         string         `json:"id"`
    54  	TTL        int            `json:"ttl"`
    55  	ClaimCount int            `json:"claim_count"`
    56  	ClaimID    string         `json:"claim_id"`
    57  }
    58  
    59  // ResourceList represents the result of creating a message.
    60  type ResourceList struct {
    61  	Resources []string `json:"resources"`
    62  }
    63  
    64  // Extract interprets any CreateResult as a ResourceList.
    65  func (r CreateResult) Extract() (ResourceList, error) {
    66  	var s ResourceList
    67  	err := r.ExtractInto(&s)
    68  	return s, err
    69  }
    70  
    71  // Extract interprets any PopResult as a list of PopMessage.
    72  func (r PopResult) Extract() ([]PopMessage, error) {
    73  	var s struct {
    74  		PopMessages []PopMessage `json:"messages"`
    75  	}
    76  	err := r.ExtractInto(&s)
    77  	return s.PopMessages, err
    78  }
    79  
    80  // Extract interprets any GetMessagesResult as a list of Message.
    81  func (r GetMessagesResult) Extract() ([]Message, error) {
    82  	var s struct {
    83  		Messages []Message `json:"messages"`
    84  	}
    85  	err := r.ExtractInto(&s)
    86  	return s.Messages, err
    87  }
    88  
    89  // Extract interprets any GetResult as a Message.
    90  func (r GetResult) Extract() (Message, error) {
    91  	var s Message
    92  	err := r.ExtractInto(&s)
    93  	return s, err
    94  }
    95  
    96  // ExtractMessage extracts message into a  list of Message.
    97  func ExtractMessages(r pagination.Page) ([]Message, error) {
    98  	var s struct {
    99  		Messages []Message `json:"messages"`
   100  	}
   101  	err := (r.(MessagePage)).ExtractInto(&s)
   102  	return s.Messages, err
   103  }
   104  
   105  // IsEmpty determines if a MessagePage contains any results.
   106  func (r MessagePage) IsEmpty() (bool, error) {
   107  	if r.StatusCode == 204 {
   108  		return true, nil
   109  	}
   110  
   111  	s, err := ExtractMessages(r)
   112  	return len(s) == 0, err
   113  }
   114  
   115  // NextPageURL uses the response's embedded link reference to navigate to the
   116  // next page of results.
   117  func (r MessagePage) NextPageURL() (string, error) {
   118  	var s struct {
   119  		Links []gophercloud.Link `json:"links"`
   120  	}
   121  	err := r.ExtractInto(&s)
   122  	if err != nil {
   123  		return "", err
   124  	}
   125  
   126  	next, err := gophercloud.ExtractNextURL(s.Links)
   127  	if err != nil {
   128  		return "", err
   129  	}
   130  	return nextPageURL(r.URL.String(), next)
   131  }