github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/server/mucp/rpc_request.go (about)

     1  // Copyright 2020 Asim Aslam
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // Original source: github.com/micro/go-micro/v3/server/mucp/rpc_request.go
    16  
    17  package mucp
    18  
    19  import (
    20  	"bytes"
    21  
    22  	"github.com/tickoalcantara12/micro/v3/service/network/transport"
    23  	"github.com/tickoalcantara12/micro/v3/util/buf"
    24  	"github.com/tickoalcantara12/micro/v3/util/codec"
    25  )
    26  
    27  type rpcRequest struct {
    28  	service     string
    29  	method      string
    30  	endpoint    string
    31  	contentType string
    32  	socket      transport.Socket
    33  	codec       codec.Codec
    34  	header      map[string]string
    35  	body        []byte
    36  	rawBody     interface{}
    37  	stream      bool
    38  	first       bool
    39  }
    40  
    41  type rpcMessage struct {
    42  	topic       string
    43  	contentType string
    44  	payload     interface{}
    45  	header      map[string]string
    46  	body        []byte
    47  	codec       codec.NewCodec
    48  }
    49  
    50  func (r *rpcRequest) Codec() codec.Reader {
    51  	return r.codec
    52  }
    53  
    54  func (r *rpcRequest) ContentType() string {
    55  	return r.contentType
    56  }
    57  
    58  func (r *rpcRequest) Service() string {
    59  	return r.service
    60  }
    61  
    62  func (r *rpcRequest) Method() string {
    63  	return r.method
    64  }
    65  
    66  func (r *rpcRequest) Endpoint() string {
    67  	return r.endpoint
    68  }
    69  
    70  func (r *rpcRequest) Header() map[string]string {
    71  	return r.header
    72  }
    73  
    74  func (r *rpcRequest) Body() interface{} {
    75  	return r.rawBody
    76  }
    77  
    78  func (r *rpcRequest) Read() ([]byte, error) {
    79  	// got a body
    80  	if r.first {
    81  		b := r.body
    82  		r.first = false
    83  		return b, nil
    84  	}
    85  
    86  	var msg transport.Message
    87  	err := r.socket.Recv(&msg)
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  	r.header = msg.Header
    92  
    93  	return msg.Body, nil
    94  }
    95  
    96  func (r *rpcRequest) Stream() bool {
    97  	return r.stream
    98  }
    99  
   100  func (r *rpcMessage) ContentType() string {
   101  	return r.contentType
   102  }
   103  
   104  func (r *rpcMessage) Topic() string {
   105  	return r.topic
   106  }
   107  
   108  func (r *rpcMessage) Payload() interface{} {
   109  	return r.payload
   110  }
   111  
   112  func (r *rpcMessage) Header() map[string]string {
   113  	return r.header
   114  }
   115  
   116  func (r *rpcMessage) Body() []byte {
   117  	return r.body
   118  }
   119  
   120  func (r *rpcMessage) Codec() codec.Reader {
   121  	b := buf.New(bytes.NewBuffer(r.body))
   122  	return r.codec(b)
   123  }