github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/xnet/xhttp/types.go (about)

     1  package xhttp
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  )
     7  
     8  // Method is http request method.
     9  type Method string
    10  
    11  const (
    12  	MethodConnect Method = "CONNECT"
    13  	MethodDelete         = "DELETE"
    14  	MethodGet            = "GET"
    15  	MethodHead           = "HEAD"
    16  	MethodOptions        = "OPTIONS"
    17  	MethodPatch          = "PATCH"
    18  	MethodPost           = "POST"
    19  	MethodPut            = "PUT"
    20  	MethodTrace          = "TRACE"
    21  )
    22  
    23  // HasRequestBody indicate whether has a body in request or not.
    24  func (m Method) HasRequestBody() bool {
    25  	return m == MethodDelete ||
    26  		m == MethodPatch ||
    27  		m == MethodPost ||
    28  		m == MethodPut
    29  }
    30  
    31  // HasResponseBody indicate whether has a body in response or not.
    32  func (m Method) HasResponseBody() bool {
    33  	return m == MethodConnect ||
    34  		m == MethodDelete ||
    35  		m == MethodGet ||
    36  		m == MethodPost
    37  }
    38  
    39  // Safe indicate whether request is safe or not.
    40  func (m Method) Safe() bool {
    41  	return m == MethodGet ||
    42  		m == MethodHead ||
    43  		m == MethodOptions
    44  }
    45  
    46  // IsIdempotent indicate whether request is idempotent or not.
    47  func (m Method) IsIdempotent() bool {
    48  	return m == MethodDelete ||
    49  		m == MethodGet ||
    50  		m == MethodHead ||
    51  		m == MethodOptions ||
    52  		m == MethodPut ||
    53  		m == MethodTrace
    54  }
    55  
    56  // Cacheable indicate whether client can cache response or not.
    57  func (m Method) Cacheable() bool {
    58  	return m == MethodGet ||
    59  		m == MethodHead ||
    60  		m == MethodPost
    61  }
    62  
    63  // Status is response status with a code and a msg string.
    64  type Status struct {
    65  	code int
    66  	msg  string
    67  }
    68  
    69  // NewStatus create new status
    70  func NewStatus(code int, msg string) *Status {
    71  	return &Status{
    72  		code: code,
    73  		msg:  msg,
    74  	}
    75  }
    76  
    77  // StatusOk return ok status
    78  func StatusOk() *Status {
    79  	return &Status{code: http.StatusOK, msg: http.StatusText(http.StatusOK)}
    80  }
    81  
    82  // StatusBadRequest is bad request -- 400
    83  func StatusBadRequest() *Status {
    84  	return &Status{code: http.StatusBadRequest, msg: http.StatusText(http.StatusBadRequest)}
    85  }
    86  
    87  // StatusServerInternalError is server error -- 500
    88  func StatusServerInternalError() *Status {
    89  	return &Status{code: http.StatusInternalServerError, msg: http.StatusText(http.StatusInternalServerError)}
    90  }
    91  
    92  // Code return status code.
    93  func (s *Status) Code() int { return s.code }
    94  
    95  // Msg return status msg.
    96  func (s *Status) Msg() string { return s.msg }
    97  
    98  // String return status string.
    99  func (s *Status) String() string {
   100  	return fmt.Sprintf("Response code %d, msg is %s", s.code, s.msg)
   101  }
   102  
   103  // IsOk check whether the request is successful.
   104  func (s *Status) IsOk() bool { return s.code/100 == 2 }
   105  
   106  // IsClientError check the error occur in client side.
   107  func (s *Status) IsClientError() bool { return s.code/100 == 4 }
   108  
   109  // IsServerError check the error occur in server side.
   110  func (s *Status) IsServerError() bool { return s.code/100 == 5 }