podman

Форк
0
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

15
package runtime
16

17
import (
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
25
type ClientResponse interface {
26
	Code() int
27
	Message() string
28
	GetHeader(string) string
29
	GetHeaders(string) []string
30
	Body() io.ReadCloser
31
}
32

33
// A ClientResponseReaderFunc turns a function into a ClientResponseReader interface implementation
34
type ClientResponseReaderFunc func(ClientResponse, Consumer) (interface{}, error)
35

36
// ReadResponse reads the response
37
func (read ClientResponseReaderFunc) ReadResponse(resp ClientResponse, consumer Consumer) (interface{}, error) {
38
	return 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
43
type ClientResponseReader interface {
44
	ReadResponse(ClientResponse, Consumer) (interface{}, error)
45
}
46

47
// NewAPIError creates a new API error
48
func NewAPIError(opName string, payload interface{}, code int) *APIError {
49
	return &APIError{
50
		OperationName: opName,
51
		Response:      payload,
52
		Code:          code,
53
	}
54
}
55

56
// APIError wraps an error model and captures the status code
57
type APIError struct {
58
	OperationName string
59
	Response      interface{}
60
	Code          int
61
}
62

63
func (o *APIError) Error() string {
64
	var resp []byte
65
	if err, ok := o.Response.(error); ok {
66
		resp = []byte("'" + err.Error() + "'")
67
	} else {
68
		resp, _ = json.Marshal(o.Response)
69
	}
70
	return fmt.Sprintf("%s (status %d): %s", o.OperationName, o.Code, resp)
71
}
72

73
func (o *APIError) String() string {
74
	return o.Error()
75
}
76

77
// IsSuccess returns true when this elapse o k response returns a 2xx status code
78
func (o *APIError) IsSuccess() bool {
79
	return o.Code/100 == 2
80
}
81

82
// IsRedirect returns true when this elapse o k response returns a 3xx status code
83
func (o *APIError) IsRedirect() bool {
84
	return o.Code/100 == 3
85
}
86

87
// IsClientError returns true when this elapse o k response returns a 4xx status code
88
func (o *APIError) IsClientError() bool {
89
	return o.Code/100 == 4
90
}
91

92
// IsServerError returns true when this elapse o k response returns a 5xx status code
93
func (o *APIError) IsServerError() bool {
94
	return o.Code/100 == 5
95
}
96

97
// IsCode returns true when this elapse o k response returns a 4xx status code
98
func (o *APIError) IsCode(code int) bool {
99
	return 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
104
type ClientResponseStatus interface {
105
	IsSuccess() bool
106
	IsRedirect() bool
107
	IsClientError() bool
108
	IsServerError() bool
109
	IsCode(int) bool
110
}
111

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.