github.com/vmware/transport-go@v1.3.4/model/request.go (about)

     1  // Copyright 2019-2020 VMware, Inc.
     2  // SPDX-License-Identifier: BSD-2-Clause
     3  
     4  package model
     5  
     6  import (
     7  	"github.com/google/uuid"
     8  	"net/http"
     9  	"net/url"
    10  )
    11  
    12  type Request struct {
    13  	Id          *uuid.UUID  `json:"id"`
    14  	Created     int64       `json:"created"`
    15  	Version     int         `json:"version"`
    16  	Destination string      `json:"channel"`
    17  	Payload     interface{} `json:"payload"`
    18  	Request     string      `json:"request"`
    19  	// Populated if the request was sent on a "private" channel and
    20  	// indicates where to send back the Response.
    21  	// A service should check this field and if not null copy it to the
    22  	// Response.BrokerDestination field to ensure that the response will be sent
    23  	// back on the correct the "private" channel.
    24  	BrokerDestination *BrokerDestinationConfig `json:"-"`
    25  }
    26  
    27  // CreateServiceRequest is a small utility function that takes request type and payload and
    28  // returns a new model.Request instance populated with them
    29  func CreateServiceRequest(requestType string, body []byte) Request {
    30  	id := uuid.New()
    31  	return Request{
    32  		Id:      &id,
    33  		Request: requestType,
    34  		Payload: body}
    35  }
    36  
    37  // CreateServiceRequestWithValues does the same as CreateServiceRequest, except the payload is url.Values and not
    38  // A byte[] array
    39  func CreateServiceRequestWithValues(requestType string, vals url.Values) Request {
    40  	id := uuid.New()
    41  	return Request{
    42  		Id:      &id,
    43  		Request: requestType,
    44  		Payload: vals}
    45  }
    46  
    47  // CreateServiceRequestWithHttpRequest does the same as CreateServiceRequest, except the payload is a pointer to the
    48  // Incoming http.Request, so you can essentially extract what ever you want from the incoming request within your service.
    49  func CreateServiceRequestWithHttpRequest(requestType string, r *http.Request) Request {
    50  	id := uuid.New()
    51  	return Request{
    52  		Id:      &id,
    53  		Request: requestType,
    54  		Payload: r}
    55  }