|
5 | 5 | class Client { |
6 | 6 | const API_ENDPOINT = "https://api.tinify.com"; |
7 | 7 |
|
| 8 | + const RETRY_COUNT = 1; |
| 9 | + const RETRY_DELAY = 500; |
| 10 | + |
8 | 11 | protected $options; |
9 | 12 |
|
10 | 13 | public static function userAgent() { |
@@ -64,73 +67,82 @@ function request($method, $url, $body = NULL) { |
64 | 67 | } |
65 | 68 | } |
66 | 69 |
|
67 | | - $request = curl_init(); |
68 | | - if ($request === false || $request === null) { |
69 | | - throw new ConnectionException( |
70 | | - "Error while connecting: curl extension is not functional or disabled." |
71 | | - ); |
72 | | - } |
| 70 | + for ($retries = self::RETRY_COUNT; $retries >= 0; $retries--) { |
| 71 | + if ($retries < self::RETRY_COUNT) { |
| 72 | + usleep(self::RETRY_DELAY * 1000); |
| 73 | + } |
73 | 74 |
|
74 | | - curl_setopt_array($request, $this->options); |
| 75 | + $request = curl_init(); |
| 76 | + if ($request === false || $request === null) { |
| 77 | + throw new ConnectionException( |
| 78 | + "Error while connecting: curl extension is not functional or disabled." |
| 79 | + ); |
| 80 | + } |
75 | 81 |
|
76 | | - $url = strtolower(substr($url, 0, 6)) == "https:" ? $url : self::API_ENDPOINT . $url; |
77 | | - curl_setopt($request, CURLOPT_URL, $url); |
78 | | - curl_setopt($request, CURLOPT_CUSTOMREQUEST, strtoupper($method)); |
| 82 | + curl_setopt_array($request, $this->options); |
79 | 83 |
|
80 | | - if (count($header) > 0) { |
81 | | - curl_setopt($request, CURLOPT_HTTPHEADER, $header); |
82 | | - } |
| 84 | + $url = strtolower(substr($url, 0, 6)) == "https:" ? $url : self::API_ENDPOINT . $url; |
| 85 | + curl_setopt($request, CURLOPT_URL, $url); |
| 86 | + curl_setopt($request, CURLOPT_CUSTOMREQUEST, strtoupper($method)); |
83 | 87 |
|
84 | | - if ($body) { |
85 | | - curl_setopt($request, CURLOPT_POSTFIELDS, $body); |
86 | | - } |
| 88 | + if (count($header) > 0) { |
| 89 | + curl_setopt($request, CURLOPT_HTTPHEADER, $header); |
| 90 | + } |
87 | 91 |
|
88 | | - $response = curl_exec($request); |
| 92 | + if ($body) { |
| 93 | + curl_setopt($request, CURLOPT_POSTFIELDS, $body); |
| 94 | + } |
89 | 95 |
|
90 | | - if (is_string($response)) { |
91 | | - $status = curl_getinfo($request, CURLINFO_HTTP_CODE); |
92 | | - $headerSize = curl_getinfo($request, CURLINFO_HEADER_SIZE); |
93 | | - curl_close($request); |
| 96 | + $response = curl_exec($request); |
94 | 97 |
|
95 | | - $headers = self::parseHeaders(substr($response, 0, $headerSize)); |
96 | | - $body = substr($response, $headerSize); |
| 98 | + if (is_string($response)) { |
| 99 | + $status = curl_getinfo($request, CURLINFO_HTTP_CODE); |
| 100 | + $headerSize = curl_getinfo($request, CURLINFO_HEADER_SIZE); |
| 101 | + curl_close($request); |
97 | 102 |
|
98 | | - if (isset($headers["compression-count"])) { |
99 | | - Tinify::setCompressionCount(intval($headers["compression-count"])); |
100 | | - } |
| 103 | + $headers = self::parseHeaders(substr($response, 0, $headerSize)); |
| 104 | + $body = substr($response, $headerSize); |
101 | 105 |
|
102 | | - $isJson = false; |
103 | | - if (isset($headers["content-type"])) { |
104 | | - /* Parse JSON response bodies. */ |
105 | | - list($contentType) = explode(";", $headers["content-type"], 2); |
106 | | - if (strtolower(trim($contentType)) == "application/json") { |
107 | | - $isJson = true; |
| 106 | + if (isset($headers["compression-count"])) { |
| 107 | + Tinify::setCompressionCount(intval($headers["compression-count"])); |
108 | 108 | } |
109 | | - } |
110 | 109 |
|
111 | | - /* 1xx and 3xx are unexpected and will be treated as error. */ |
112 | | - $isError = $status <= 199 || $status >= 300; |
113 | | - |
114 | | - if ($isJson || $isError) { |
115 | | - /* Parse JSON bodies, always interpret errors as JSON. */ |
116 | | - $body = json_decode($body); |
117 | | - if (!$body) { |
118 | | - $message = sprintf("Error while parsing response: %s (#%d)", |
119 | | - PHP_VERSION_ID >= 50500 ? json_last_error_msg() : "Error", |
120 | | - json_last_error()); |
121 | | - throw Exception::create($message, "ParseError", $status); |
| 110 | + $isJson = false; |
| 111 | + if (isset($headers["content-type"])) { |
| 112 | + /* Parse JSON response bodies. */ |
| 113 | + list($contentType) = explode(";", $headers["content-type"], 2); |
| 114 | + if (strtolower(trim($contentType)) == "application/json") { |
| 115 | + $isJson = true; |
| 116 | + } |
122 | 117 | } |
123 | | - } |
124 | 118 |
|
125 | | - if ($isError) { |
126 | | - throw Exception::create($body->message, $body->error, $status); |
127 | | - } |
| 119 | + /* 1xx and 3xx are unexpected and will be treated as error. */ |
| 120 | + $isError = $status <= 199 || $status >= 300; |
| 121 | + |
| 122 | + if ($isJson || $isError) { |
| 123 | + /* Parse JSON bodies, always interpret errors as JSON. */ |
| 124 | + $body = json_decode($body); |
| 125 | + if (!$body) { |
| 126 | + $message = sprintf("Error while parsing response: %s (#%d)", |
| 127 | + PHP_VERSION_ID >= 50500 ? json_last_error_msg() : "Error", |
| 128 | + json_last_error()); |
| 129 | + if ($retries > 0 && $status >= 500) continue; |
| 130 | + throw Exception::create($message, "ParseError", $status); |
| 131 | + } |
| 132 | + } |
128 | 133 |
|
129 | | - return (object) array("body" => $body, "headers" => $headers); |
130 | | - } else { |
131 | | - $message = sprintf("%s (#%d)", curl_error($request), curl_errno($request)); |
132 | | - curl_close($request); |
133 | | - throw new ConnectionException("Error while connecting: " . $message); |
| 134 | + if ($isError) { |
| 135 | + if ($retries > 0 && $status >= 500) continue; |
| 136 | + throw Exception::create($body->message, $body->error, $status); |
| 137 | + } |
| 138 | + |
| 139 | + return (object) array("body" => $body, "headers" => $headers); |
| 140 | + } else { |
| 141 | + $message = sprintf("%s (#%d)", curl_error($request), curl_errno($request)); |
| 142 | + curl_close($request); |
| 143 | + if ($retries > 0) continue; |
| 144 | + throw new ConnectionException("Error while connecting: " . $message); |
| 145 | + } |
134 | 146 | } |
135 | 147 | } |
136 | 148 |
|
|
0 commit comments