rest 包

rest 包提供了一个简单的 REST Client 用于访问 API 服务。

在这个包中,主要包含三个概念:Config,Client 和 Request。Config 是 Client 的配置,用于创建 Client。而 Request 则由 Client 创建,用来表示每一个 REST 请求。

  1. // RequestExecutor implements a http client.
  2. type RequestExecutor interface {
  3. Do(req *http.Request) (*http.Response, error)
  4. }
  5. // Config is rest client config.
  6. type Config struct {
  7. // Scheme is http scheme. It can be "http" or "https".
  8. Scheme string
  9. // Host must be a host string, a host:port or a URL to a server.
  10. Host string
  11. // Executor is used to execute http requests.
  12. // If it is empty, http.DefaultClient is used.
  13. Executor RequestExecutor
  14. }
  15. // Client implements builder pattern for http client.
  16. type Client struct {
  17. ...
  18. }
  19. // NewClient creates a client.
  20. func NewClient(cfg *Config) (*Client, error)
  21. // Request creates an request with specific method and url path.
  22. // The code is only for checking if status code of response is right.
  23. func (c *Client) Request(method string, code int, url string) *Request
  24. // Request describes a http request.
  25. type Request struct {
  26. ...
  27. }
  28. // Path sets path parameter.
  29. func (r *Request) Path(name string, value interface{}) *Request
  30. // Query sets query parameter.
  31. func (r *Request) Query(name string, values ...interface{}) *Request
  32. // Header sets header parameter.
  33. func (r *Request) Header(name string, values ...interface{}) *Request
  34. // Form sets form parameter.
  35. func (r *Request) Form(name string, values ...interface{}) *Request
  36. // File sets file parameter.
  37. func (r *Request) File(name string, file interface{}) *Request
  38. // Body sets body parameter.
  39. func (r *Request) Body(contentType string, value interface{}) *Request
  40. // Meta sets header result.
  41. func (r *Request) Meta(value *map[string]string) *Request
  42. // Data sets body result. value must be a pointer.
  43. func (r *Request) Data(value interface{}) *Request
  44. // Do executes the request.
  45. func (r *Request) Do(ctx context.Context) error

Request 保存了一个请求的数据,用 Path,Query,Header,Form,File,Body 来设置请求的相关值,Meta 和 Data 来设置用于接收响应的值(都是指针)。然后 Do 用于真正发起请求,并完成 Meta 和 Data 的填充。

Request 的方法与 API Definition 的除了 Prefab 和 Error 之外的参数和返回值类型一一对应,这样可以十分方便的设置对应的值。由于 Prefab 是由服务端生成而不由客户端提交,Error 由 Do 方法返回,因此这两种类型没有对应的方法。

注:这个 Client 会被由命令 nirvana client 生成的客户端依赖,因此需要确保两者的一致性。如果用户自定义了一些新的请求和响应类型,也需要对这个客户端进行扩展。