podman
110 строк · 3.2 Кб
1// Copyright 2015 go-swagger maintainers
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package runtime
16
17import (
18"encoding/json"
19"fmt"
20"io"
21)
22
23// A ClientResponse represents a client response
24// This bridges between responses obtained from different transports
25type ClientResponse interface {
26Code() int
27Message() string
28GetHeader(string) string
29GetHeaders(string) []string
30Body() io.ReadCloser
31}
32
33// A ClientResponseReaderFunc turns a function into a ClientResponseReader interface implementation
34type ClientResponseReaderFunc func(ClientResponse, Consumer) (interface{}, error)
35
36// ReadResponse reads the response
37func (read ClientResponseReaderFunc) ReadResponse(resp ClientResponse, consumer Consumer) (interface{}, error) {
38return read(resp, consumer)
39}
40
41// A ClientResponseReader is an interface for things want to read a response.
42// An application of this is to create structs from response values
43type ClientResponseReader interface {
44ReadResponse(ClientResponse, Consumer) (interface{}, error)
45}
46
47// NewAPIError creates a new API error
48func NewAPIError(opName string, payload interface{}, code int) *APIError {
49return &APIError{
50OperationName: opName,
51Response: payload,
52Code: code,
53}
54}
55
56// APIError wraps an error model and captures the status code
57type APIError struct {
58OperationName string
59Response interface{}
60Code int
61}
62
63func (o *APIError) Error() string {
64var resp []byte
65if err, ok := o.Response.(error); ok {
66resp = []byte("'" + err.Error() + "'")
67} else {
68resp, _ = json.Marshal(o.Response)
69}
70return fmt.Sprintf("%s (status %d): %s", o.OperationName, o.Code, resp)
71}
72
73func (o *APIError) String() string {
74return o.Error()
75}
76
77// IsSuccess returns true when this elapse o k response returns a 2xx status code
78func (o *APIError) IsSuccess() bool {
79return o.Code/100 == 2
80}
81
82// IsRedirect returns true when this elapse o k response returns a 3xx status code
83func (o *APIError) IsRedirect() bool {
84return o.Code/100 == 3
85}
86
87// IsClientError returns true when this elapse o k response returns a 4xx status code
88func (o *APIError) IsClientError() bool {
89return o.Code/100 == 4
90}
91
92// IsServerError returns true when this elapse o k response returns a 5xx status code
93func (o *APIError) IsServerError() bool {
94return o.Code/100 == 5
95}
96
97// IsCode returns true when this elapse o k response returns a 4xx status code
98func (o *APIError) IsCode(code int) bool {
99return o.Code == code
100}
101
102// A ClientResponseStatus is a common interface implemented by all responses on the generated code
103// You can use this to treat any client response based on status code
104type ClientResponseStatus interface {
105IsSuccess() bool
106IsRedirect() bool
107IsClientError() bool
108IsServerError() bool
109IsCode(int) bool
110}
111