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

http rule's response_body support to stream rpc #1208

Merged
merged 3 commits into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/internal/clients/responsebody/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ go_library(
"model_protobuf_any.go",
"model_response_response_type.go",
"model_runtime_error.go",
"model_runtime_stream_error.go",
"model_stream_result_of_examplepb_response_body_out.go",
"response.go",
],
importpath = "github.com/grpc-ecosystem/grpc-gateway/examples/internal/clients/responsebody",
Expand Down
75 changes: 75 additions & 0 deletions examples/internal/clients/responsebody/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,32 @@ paths:
description: "An unexpected error response"
schema:
$ref: "#/definitions/runtimeError"
/responsebody/stream/{data}:
get:
tags:
- "ResponseBodyService"
operationId: "ResponseBodyService_GetResponseBodyStream"
parameters:
- name: "data"
in: "path"
required: true
type: "string"
x-exportParamName: "Data"
responses:
200:
description: "(streaming responses)"
schema:
type: "object"
properties:
result:
$ref: "#/definitions/examplepbResponseBodyOutResponse"
error:
$ref: "#/definitions/runtimeStreamError"
title: "Stream result of examplepbResponseBodyOut"
default:
description: "An unexpected error response"
schema:
$ref: "#/definitions/runtimeError"
/responsebody/{data}:
get:
tags:
Expand Down Expand Up @@ -175,6 +201,9 @@ definitions:
\ custom JSON in addition to the `@type`\nfield. Example (for message [google.protobuf.Duration][]):\n\
\n {\n \"@type\": \"type.googleapis.com/google.protobuf.Duration\",\n\
\ \"value\": \"1.212s\"\n }"
example:
value: "value"
type_url: "type_url"
runtimeError:
type: "object"
properties:
Expand All @@ -189,3 +218,49 @@ definitions:
type: "array"
items:
$ref: "#/definitions/protobufAny"
runtimeStreamError:
type: "object"
properties:
grpc_code:
type: "integer"
format: "int32"
http_code:
type: "integer"
format: "int32"
message:
type: "string"
http_status:
type: "string"
details:
type: "array"
items:
$ref: "#/definitions/protobufAny"
example:
http_code: 6
http_status: "http_status"
details:
- value: "value"
type_url: "type_url"
- value: "value"
type_url: "type_url"
message: "message"
grpc_code: 0
Stream result of examplepbResponseBodyOut:
properties:
result:
$ref: "#/definitions/examplepbResponseBodyOutResponse"
error:
$ref: "#/definitions/runtimeStreamError"
example:
result:
data: "data"
error:
http_code: 6
http_status: "http_status"
details:
- value: "value"
type_url: "type_url"
- value: "value"
type_url: "type_url"
message: "message"
grpc_code: 0
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,105 @@ func (a *ResponseBodyServiceApiService) ResponseBodyServiceGetResponseBody(ctx c
return localVarReturnValue, localVarHttpResponse, nil
}

/*
ResponseBodyServiceApiService
* @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
* @param data

@return StreamResultOfExamplepbResponseBodyOut
*/
func (a *ResponseBodyServiceApiService) ResponseBodyServiceGetResponseBodyStream(ctx context.Context, data string) (StreamResultOfExamplepbResponseBodyOut, *http.Response, error) {
var (
localVarHttpMethod = strings.ToUpper("Get")
localVarPostBody interface{}
localVarFileName string
localVarFileBytes []byte
localVarReturnValue StreamResultOfExamplepbResponseBodyOut
)

// create path and map variables
localVarPath := a.client.cfg.BasePath + "/responsebody/stream/{data}"
localVarPath = strings.Replace(localVarPath, "{"+"data"+"}", fmt.Sprintf("%v", data), -1)

localVarHeaderParams := make(map[string]string)
localVarQueryParams := url.Values{}
localVarFormParams := url.Values{}

// to determine the Content-Type header
localVarHttpContentTypes := []string{"application/json"}

// set Content-Type header
localVarHttpContentType := selectHeaderContentType(localVarHttpContentTypes)
if localVarHttpContentType != "" {
localVarHeaderParams["Content-Type"] = localVarHttpContentType
}

// to determine the Accept header
localVarHttpHeaderAccepts := []string{"application/json"}

// set Accept header
localVarHttpHeaderAccept := selectHeaderAccept(localVarHttpHeaderAccepts)
if localVarHttpHeaderAccept != "" {
localVarHeaderParams["Accept"] = localVarHttpHeaderAccept
}
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
if err != nil {
return localVarReturnValue, nil, err
}

localVarHttpResponse, err := a.client.callAPI(r)
if err != nil || localVarHttpResponse == nil {
return localVarReturnValue, localVarHttpResponse, err
}

localVarBody, err := ioutil.ReadAll(localVarHttpResponse.Body)
localVarHttpResponse.Body.Close()
if err != nil {
return localVarReturnValue, localVarHttpResponse, err
}

if localVarHttpResponse.StatusCode < 300 {
// If we succeed, return the data, otherwise pass on to decode error.
err = a.client.decode(&localVarReturnValue, localVarBody, localVarHttpResponse.Header.Get("Content-Type"));
if err == nil {
return localVarReturnValue, localVarHttpResponse, err
}
}

if localVarHttpResponse.StatusCode >= 300 {
newErr := GenericSwaggerError{
body: localVarBody,
error: localVarHttpResponse.Status,
}

if localVarHttpResponse.StatusCode == 200 {
var v StreamResultOfExamplepbResponseBodyOut
err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"));
if err != nil {
newErr.error = err.Error()
return localVarReturnValue, localVarHttpResponse, newErr
}
newErr.model = v
return localVarReturnValue, localVarHttpResponse, newErr
}

if localVarHttpResponse.StatusCode == 0 {
var v RuntimeError
err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"));
if err != nil {
newErr.error = err.Error()
return localVarReturnValue, localVarHttpResponse, newErr
}
newErr.model = v
return localVarReturnValue, localVarHttpResponse, newErr
}

return localVarReturnValue, localVarHttpResponse, newErr
}

return localVarReturnValue, localVarHttpResponse, nil
}

/*
ResponseBodyServiceApiService
* @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All URIs are relative to *https://localhost*
Method | HTTP request | Description
------------- | ------------- | -------------
[**ResponseBodyServiceGetResponseBody**](ResponseBodyServiceApi.md#ResponseBodyServiceGetResponseBody) | **Get** /responsebody/{data} |
[**ResponseBodyServiceGetResponseBodyStream**](ResponseBodyServiceApi.md#ResponseBodyServiceGetResponseBodyStream) | **Get** /responsebody/stream/{data} |
[**ResponseBodyServiceListResponseBodies**](ResponseBodyServiceApi.md#ResponseBodyServiceListResponseBodies) | **Get** /responsebodies/{data} |
[**ResponseBodyServiceListResponseStrings**](ResponseBodyServiceApi.md#ResponseBodyServiceListResponseStrings) | **Get** /responsestrings/{data} |

Expand Down Expand Up @@ -35,6 +36,32 @@ No authorization required

[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)

# **ResponseBodyServiceGetResponseBodyStream**
> StreamResultOfExamplepbResponseBodyOut ResponseBodyServiceGetResponseBodyStream(ctx, data)


### Required Parameters

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**ctx** | **context.Context** | context for authentication, logging, cancellation, deadlines, tracing, etc.
**data** | **string**| |

### Return type

[**StreamResultOfExamplepbResponseBodyOut**](Stream result of examplepbResponseBodyOut.md)

### Authorization

No authorization required

### HTTP request headers

- **Content-Type**: application/json
- **Accept**: application/json

[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)

# **ResponseBodyServiceListResponseBodies**
> []ExamplepbRepeatedResponseBodyOutResponse ResponseBodyServiceListResponseBodies(ctx, data)

Expand Down
14 changes: 14 additions & 0 deletions examples/internal/clients/responsebody/docs/RuntimeStreamError.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# RuntimeStreamError

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**GrpcCode** | **int32** | | [optional] [default to null]
**HttpCode** | **int32** | | [optional] [default to null]
**Message** | **string** | | [optional] [default to null]
**HttpStatus** | **string** | | [optional] [default to null]
**Details** | [**[]ProtobufAny**](protobufAny.md) | | [optional] [default to null]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# StreamResultOfExamplepbResponseBodyOut

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Result** | [***ExamplepbResponseBodyOutResponse**](examplepbResponseBodyOutResponse.md) | | [optional] [default to null]
**Error_** | [***RuntimeStreamError**](runtimeStreamError.md) | | [optional] [default to null]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* examples/internal/proto/examplepb/response_body_service.proto
*
* No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
*
* API version: version not set
* Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
*/

package responsebody

type RuntimeStreamError struct {
GrpcCode int32 `json:"grpc_code,omitempty"`
HttpCode int32 `json:"http_code,omitempty"`
Message string `json:"message,omitempty"`
HttpStatus string `json:"http_status,omitempty"`
Details []ProtobufAny `json:"details,omitempty"`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* examples/internal/proto/examplepb/response_body_service.proto
*
* No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
*
* API version: version not set
* Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
*/

package responsebody

type StreamResultOfExamplepbResponseBodyOut struct {
Result *ExamplepbResponseBodyOutResponse `json:"result,omitempty"`
Error_ *RuntimeStreamError `json:"error,omitempty"`
}
Loading