PluginProbe ʕ •ᴥ•ʔ
ShareThis Dashboard for Google Analytics / trunk
ShareThis Dashboard for Google Analytics vtrunk
3.3.2 trunk 1.0.7 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.5 2.3.5 2.3.6 2.3.7 2.3.8 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.0.0 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.3.0 3.3.1
googleanalytics / lib / analytics-admin / vendor / google / gax / src / ApiStatus.php
googleanalytics / lib / analytics-admin / vendor / google / gax / src Last commit date
Middleware 3 years ago ResourceTemplate 3 years ago Testing 3 years ago Transport 3 years ago AgentHeader.php 3 years ago ApiException.php 3 years ago ApiStatus.php 3 years ago ArrayTrait.php 3 years ago BidiStream.php 3 years ago Call.php 3 years ago ClientStream.php 3 years ago CredentialsWrapper.php 3 years ago FixedSizeCollection.php 3 years ago GPBLabel.php 3 years ago GPBType.php 3 years ago GapicClientTrait.php 3 years ago GrpcSupportTrait.php 3 years ago OperationResponse.php 3 years ago Page.php 3 years ago PageStreamingDescriptor.php 3 years ago PagedListResponse.php 3 years ago PathTemplate.php 3 years ago PollingTrait.php 3 years ago RequestBuilder.php 3 years ago RequestParamsHeaderDescriptor.php 3 years ago RetrySettings.php 3 years ago Serializer.php 3 years ago ServerStream.php 3 years ago ServerStreamingCallInterface.php 3 years ago ServiceAddressTrait.php 3 years ago UriTrait.php 3 years ago ValidationException.php 3 years ago ValidationTrait.php 3 years ago Version.php 3 years ago
ApiStatus.php
172 lines
1 <?php
2 /*
3 * Copyright 2017 Google LLC
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 namespace Google\ApiCore;
33
34 use Google\Rpc\Code;
35
36 class ApiStatus
37 {
38 const OK = 'OK';
39 const CANCELLED = 'CANCELLED';
40 const UNKNOWN = 'UNKNOWN';
41 const INVALID_ARGUMENT = 'INVALID_ARGUMENT';
42 const DEADLINE_EXCEEDED = 'DEADLINE_EXCEEDED';
43 const NOT_FOUND = 'NOT_FOUND';
44 const ALREADY_EXISTS = 'ALREADY_EXISTS';
45 const PERMISSION_DENIED = 'PERMISSION_DENIED';
46 const RESOURCE_EXHAUSTED = 'RESOURCE_EXHAUSTED';
47 const FAILED_PRECONDITION = 'FAILED_PRECONDITION';
48 const ABORTED = 'ABORTED';
49 const OUT_OF_RANGE = 'OUT_OF_RANGE';
50 const UNIMPLEMENTED = 'UNIMPLEMENTED';
51 const INTERNAL = 'INTERNAL';
52 const UNAVAILABLE = 'UNAVAILABLE';
53 const DATA_LOSS = 'DATA_LOSS';
54 const UNAUTHENTICATED = 'UNAUTHENTICATED';
55
56 const UNRECOGNIZED_STATUS = 'UNRECOGNIZED_STATUS';
57 const UNRECOGNIZED_CODE = -1;
58
59 private static $apiStatusToCodeMap = [
60 ApiStatus::OK => Code::OK,
61 ApiStatus::CANCELLED => Code::CANCELLED,
62 ApiStatus::UNKNOWN => Code::UNKNOWN,
63 ApiStatus::INVALID_ARGUMENT => Code::INVALID_ARGUMENT,
64 ApiStatus::DEADLINE_EXCEEDED => Code::DEADLINE_EXCEEDED,
65 ApiStatus::NOT_FOUND => Code::NOT_FOUND,
66 ApiStatus::ALREADY_EXISTS => Code::ALREADY_EXISTS,
67 ApiStatus::PERMISSION_DENIED => Code::PERMISSION_DENIED,
68 ApiStatus::RESOURCE_EXHAUSTED => Code::RESOURCE_EXHAUSTED,
69 ApiStatus::FAILED_PRECONDITION => Code::FAILED_PRECONDITION,
70 ApiStatus::ABORTED => Code::ABORTED,
71 ApiStatus::OUT_OF_RANGE => Code::OUT_OF_RANGE,
72 ApiStatus::UNIMPLEMENTED => Code::UNIMPLEMENTED,
73 ApiStatus::INTERNAL => Code::INTERNAL,
74 ApiStatus::UNAVAILABLE => Code::UNAVAILABLE,
75 ApiStatus::DATA_LOSS => Code::DATA_LOSS,
76 ApiStatus::UNAUTHENTICATED => Code::UNAUTHENTICATED,
77 ];
78 private static $codeToApiStatusMap = [
79 Code::OK => ApiStatus::OK,
80 Code::CANCELLED => ApiStatus::CANCELLED,
81 Code::UNKNOWN => ApiStatus::UNKNOWN,
82 Code::INVALID_ARGUMENT => ApiStatus::INVALID_ARGUMENT,
83 Code::DEADLINE_EXCEEDED => ApiStatus::DEADLINE_EXCEEDED,
84 Code::NOT_FOUND => ApiStatus::NOT_FOUND,
85 Code::ALREADY_EXISTS => ApiStatus::ALREADY_EXISTS,
86 Code::PERMISSION_DENIED => ApiStatus::PERMISSION_DENIED,
87 Code::RESOURCE_EXHAUSTED => ApiStatus::RESOURCE_EXHAUSTED,
88 Code::FAILED_PRECONDITION => ApiStatus::FAILED_PRECONDITION,
89 Code::ABORTED => ApiStatus::ABORTED,
90 Code::OUT_OF_RANGE => ApiStatus::OUT_OF_RANGE,
91 Code::UNIMPLEMENTED => ApiStatus::UNIMPLEMENTED,
92 Code::INTERNAL => ApiStatus::INTERNAL,
93 Code::UNAVAILABLE => ApiStatus::UNAVAILABLE,
94 Code::DATA_LOSS => ApiStatus::DATA_LOSS,
95 Code::UNAUTHENTICATED => ApiStatus::UNAUTHENTICATED,
96 ];
97 private static $httpStatusCodeToRpcCodeMap = [
98 400 => Code::INVALID_ARGUMENT,
99 401 => Code::UNAUTHENTICATED,
100 403 => Code::PERMISSION_DENIED,
101 404 => Code::NOT_FOUND,
102 409 => Code::ABORTED,
103 416 => Code::OUT_OF_RANGE,
104 429 => Code::RESOURCE_EXHAUSTED,
105 499 => Code::CANCELLED,
106 501 => Code::UNIMPLEMENTED,
107 503 => Code::UNAVAILABLE,
108 504 => Code::DEADLINE_EXCEEDED,
109 ];
110
111 /**
112 * @param string $status
113 * @return bool
114 */
115 public static function isValidStatus(string $status)
116 {
117 return array_key_exists($status, self::$apiStatusToCodeMap);
118 }
119
120 /**
121 * @param int $code
122 * @return string
123 */
124 public static function statusFromRpcCode(int $code)
125 {
126 if (array_key_exists($code, self::$codeToApiStatusMap)) {
127 return self::$codeToApiStatusMap[$code];
128 }
129 return ApiStatus::UNRECOGNIZED_STATUS;
130 }
131
132 /**
133 * @param string $status
134 * @return int
135 */
136 public static function rpcCodeFromStatus(string $status)
137 {
138 if (array_key_exists($status, self::$apiStatusToCodeMap)) {
139 return self::$apiStatusToCodeMap[$status];
140 }
141 return ApiStatus::UNRECOGNIZED_CODE;
142 }
143
144 /**
145 * Maps HTTP status codes to Google\Rpc\Code codes.
146 * Some codes are left out because they map to multiple gRPC codes (e.g. 500).
147 *
148 * @param int $httpStatusCode
149 * @return int
150 */
151 public static function rpcCodeFromHttpStatusCode(int $httpStatusCode)
152 {
153 if (array_key_exists($httpStatusCode, self::$httpStatusCodeToRpcCodeMap)) {
154 return self::$httpStatusCodeToRpcCodeMap[$httpStatusCode];
155 }
156 // All 2xx
157 if ($httpStatusCode >= 200 && $httpStatusCode < 300) {
158 return Code::OK;
159 }
160 // All 4xx
161 if ($httpStatusCode >= 400 && $httpStatusCode < 500) {
162 return Code::FAILED_PRECONDITION;
163 }
164 // All 5xx
165 if ($httpStatusCode >= 500 && $httpStatusCode < 600) {
166 return Code::INTERNAL;
167 }
168 // Everything else (We cannot change this to Code::UNKNOWN because it would break BC)
169 return ApiStatus::UNRECOGNIZED_CODE;
170 }
171 }
172