本文整理匯總了Golang中github.com/micro/micro/api/proto.Response.StatusCode方法的典型用法代碼示例。如果您正苦於以下問題:Golang Response.StatusCode方法的具體用法?Golang Response.StatusCode怎麽用?Golang Response.StatusCode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/micro/micro/api/proto.Response
的用法示例。
在下文中一共展示了Response.StatusCode方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Hello
func (s *Say) Hello(ctx context.Context, req *api.Request, rsp *api.Response) error {
log.Info("Received Say.Hello API request")
name, ok := req.Get["name"]
if !ok || len(name.Values) == 0 {
return errors.BadRequest("go.micro.api.greeter", "Name cannot be blank")
}
request := client.NewRequest("go.micro.srv.greeter", "Say.Hello", &hello.Request{
Name: strings.Join(name.Values, " "),
})
response := &hello.Response{}
if err := client.Call(ctx, request, response); err != nil {
return err
}
rsp.StatusCode = 200
b, _ := json.Marshal(map[string]string{
"message": response.Msg,
})
rsp.Body = string(b)
return nil
}
示例2: Update
// Update API handler
func (es *Elastic) Update(ctx context.Context, req *api.Request, rsp *api.Response) error {
var err error
var input map[string]interface{}
var data []byte
// Unmarshal unknown JSON
if err = json.Unmarshal([]byte(req.Body), &input); err != nil {
return errors.BadRequest("go.micro.api.elastic", err.Error())
}
// Marshal unknown JSON (data)
data, err = json.Marshal(input["data"])
srvReq := client.NewRequest(
"go.micro.srv.elastic",
"Elastic.Update",
&elastic.UpdateRequest{
Index: fmt.Sprintf("%v", input["index"]),
Type: fmt.Sprintf("%v", input["type"]),
Id: fmt.Sprintf("%v", input["id"]),
Data: string(data),
},
)
srvRsp := &elastic.UpdateResponse{}
if err = client.Call(ctx, srvReq, srvRsp); err != nil {
return err
}
rsp.StatusCode = http.StatusOK
rsp.Body = `{}`
return nil
}
示例3: Query
// Query API handler
func (es *Elastic) Query(ctx context.Context, req *api.Request, rsp *api.Response) error {
var err error
var input map[string]interface{}
var query []byte
// Unmarshal unknown JSON
if err = json.Unmarshal([]byte(req.Body), &input); err != nil {
return errors.BadRequest("go.micro.api.elastic", err.Error())
}
query, err = json.Marshal(input["query"])
srvReq := client.NewRequest(
"go.micro.srv.elastic",
"Elastic.Query",
&elastic.QueryRequest{
Index: fmt.Sprintf("%v", input["index"]),
Type: fmt.Sprintf("%v", input["type"]),
Query: string(query),
},
)
srvRsp := &elastic.SearchResponse{}
if err = client.Call(ctx, srvReq, srvRsp); err != nil {
return err
}
rsp.StatusCode = http.StatusOK
rsp.Body = srvRsp.Result
return nil
}
示例4: Read
// Read API handler
func (es *Elastic) Read(ctx context.Context, req *api.Request, rsp *api.Response) error {
var err error
var readRequest *elastic.ReadRequest
if err = json.Unmarshal([]byte(req.Body), &readRequest); err != nil {
return errors.InternalServerError("go.micro.api.elastic", err.Error())
}
srvReq := client.NewRequest(
"go.micro.srv.elastic",
"Elastic.Read",
readRequest,
)
srvRsp := &elastic.ReadResponse{}
if err = client.Call(ctx, srvReq, srvRsp); err != nil {
return err
}
rsp.StatusCode = http.StatusOK
rsp.Body = srvRsp.Result
return nil
}
示例5: Call
// Example.Call is called by the API as /template/example/call with post body {"name": "foo"}
func (e *Example) Call(ctx context.Context, req *api.Request, rsp *api.Response) error {
log.Print("Received Example.Call request")
// extract the client from the context
exampleClient, ok := client.ExampleFromContext(ctx)
if !ok {
return errors.InternalServerError("go.micro.api.template.example.call", "example client not found")
}
// make request
response, err := exampleClient.Call(ctx, &example.Request{
Name: extractValue(req.Post["name"]),
})
if err != nil {
return errors.InternalServerError("go.micro.api.template.example.call", err.Error())
}
b, _ := json.Marshal(response)
rsp.StatusCode = 200
rsp.Body = string(b)
return nil
}