Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support custom http code #287

Closed
wants to merge 4 commits into from

Conversation

dspo
Copy link
Contributor

@dspo dspo commented Jul 19, 2022

What this PR does / why we need it:

feat: support custom http code

you can define method that reference github.com/erda-project/erda-infra/proto-go/http.HttpInfo in your proto message:

import "infra/http/infra-http.proto";

service MyService {
  rpc HelloWorld(infra.http.Void) returns(HelloWorldResp) {
    option(google.api.http) = {
      get: "/api/hello-world",
    };
  }
}

message HelloWorldResp {
  string msg = 1;
  infra.http.HttpInfo httpInfo = 2 [json_name = "-"];
}

you can return custom HTTP Status Code in your handler now:

func (p *MyService) HelloWorld(ctx context.Context, void *http1.Void) (*pb.HelloWorldResp, error) {
	return &pb.HelloWorldResp{
		Msg: time.Now().Format(time.RFC3339),
		HttpInfo: &http1.HttpInfo{
			Status: http.StatusAccepted,
			Headers: []*http1.HttpHeader{
				{
					Key:    "x-custom-key-a",
					Values: []string{"v1", "v2"},
				}, {
					Key:    "x-custom-key-b",
					Values: []string{"something", "anything"},
				},
			},
		},
	}, nil
}

the cURL result:

# curl the-host/api/hello-world -v
> GET /api/hello-world HTTP/1.1
> User-Agent: curl/7.29.0
> Accept: */*
> 
< HTTP/1.1 202 Accepted
< Vary: Origin
< X-Custom-Key-A: v1
< X-Custom-Key-A: v2
< X-Custom-Key-B: something
< X-Custom-Key-B: anything
< Date: Tue, 19 Jul 2022 17:05:46 GMT
< Content-Length: 196
< Content-Type: text/plain; charset=utf-8
< 
{"success":true,"data":{"msg":"2022-07-20T01:05:46+08:00"}}

Which issue(s) this PR fixes:

Fixes #

Specified Reviewers:

/assign @your-reviewer

@dspo dspo requested review from sfwn and sixther-dc July 19, 2022 17:12
@codecov
Copy link

codecov bot commented Jul 19, 2022

Codecov Report

Merging #287 (16a105d) into master (e0758b0) will decrease coverage by 0.13%.
The diff coverage is 6.25%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #287      +/-   ##
==========================================
- Coverage   38.01%   37.87%   -0.14%     
==========================================
  Files         100      100              
  Lines        5830     5845      +15     
==========================================
- Hits         2216     2214       -2     
- Misses       3449     3466      +17     
  Partials      165      165              
Impacted Files Coverage Δ
pkg/transport/http/encoding/encoding.go 3.87% <0.00%> (-0.51%) ⬇️
...iders/mysql/v2/plugins/fields/soft_delete_stamp.go 59.15% <100.00%> (ø)
pkg/parallel/future.go 86.20% <0.00%> (-6.90%) ⬇️

@sfwn sfwn changed the title feat: support cumstom http code feat: support custom http code Jul 20, 2022
@dspo dspo marked this pull request as draft July 20, 2022 06:52
@dspo dspo closed this Mar 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

None yet

2 participants