• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ringcentral / ringcentral-php / 10247615018

05 Aug 2024 11:04AM UTC coverage: 77.524% (+0.1%) from 77.389%
10247615018

Pull #139

github

web-flow
Merge 085079492 into 67c9a424d
Pull Request #139: Extract Truncated error response

9 of 9 new or added lines in 1 file covered. (100.0%)

2 existing lines in 2 files now uncovered.

576 of 743 relevant lines covered (77.52%)

7.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.83
/src/Http/Client.php
1
<?php
2

3
namespace RingCentral\SDK\Http;
4

5
use Exception;
6
use GuzzleHttp\Client as GuzzleClient;
7
use GuzzleHttp\Psr7\Request;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\StreamInterface;
10

11
class Client
12
{
13

14
    /** @var GuzzleClient */
15
    private $_guzzle;
16

17
    /**
18
     * Client constructor.
19
     *
20
     * @param GuzzleClient $guzzle
21
     */
22
    public function __construct($guzzle)
23
    {
24
        $this->_guzzle = $guzzle;
32✔
25
    }
26

27
    /**
28
     * @param RequestInterface $request
29
     *
30
     * @throws ApiException If the response did not return a 2XX status code.
31
     *
32
     * @return ApiResponse
33
     */
34
    public function send(RequestInterface $request)
35
    {
36
        $apiResponse = null;
24✔
37

38
        try {
39
            $apiResponse = $this->loadResponse($request);
24✔
40

41
            if ($apiResponse->ok()) {
24✔
42
                return $apiResponse;
24✔
43
            } else {
44
                throw new Exception('Response has unsuccessful status');
1✔
45
            }
46
        } catch (Exception $e) {
1✔
47
            // The following means that request failed completely
48
            if (empty($apiResponse)) {
1✔
UNCOV
49
                $apiResponse = new ApiResponse($request);
×
50
            }
51

52
            throw new ApiException($apiResponse, $e);
1✔
53
        }
54
    }
55

56
    /**
57
     * @param RequestInterface $request
58
     * @return ApiResponse
59
     * @throws Exception
60
     */
61
    protected function loadResponse(RequestInterface $request)
62
    {
63

64
        //TODO Is it needed?
65
        if (stristr($request->getHeaderLine('Content-Type'), 'multipart/form-data')) {
24✔
66
            $request = $request->withHeader('Expect', '');
×
67
        }
68

69
        //TODO Is it needed?
70
        if ($request->getBody()->isSeekable()) {
24✔
71
            $request->getBody()->rewind();
24✔
72
        }
73

74
        $response = $this->_guzzle->send($request, ['http_errors' => false, 'exceptions' => false]);
24✔
75

76
        return new ApiResponse($request, $response);
24✔
77

78
    }
79

80
    /**
81
     * @param null|string                                $method
82
     * @param null|string                                $url
83
     * @param null|string|array                          $queryParams
84
     * @param null|string|array|resource|StreamInterface $body Message body.
85
     * @param null|array                                 $headers
86
     *
87
     * @return RequestInterface
88
     */
89
    public function createRequest($method, $url, $queryParams = [], $body = null, $headers = [])
90
    {
91

92
        $properties = $this->parseProperties($method, $url, $queryParams, $body, $headers);
31✔
93

94
        return new Request($properties['method'], $properties['url'], $properties['headers'], $properties['body']);
31✔
95

96
    }
97

98
    /**
99
     * @param RequestInterface $request
100
     * @return string[]
101
     */
102
    protected function getRequestHeaders(RequestInterface $request)
103
    {
104

105
        $headers = [];
×
106

107
        foreach (array_keys($request->getHeaders()) as $name) {
×
108
            $headers[] = $name . ': ' . $request->getHeaderLine($name);
×
109
        }
110

111
        return $headers;
×
112

113
    }
114

115
    /**
116
     * @param null|string                                $method
117
     * @param null|string                                $url
118
     * @param null|string|array                          $queryParams
119
     * @param null|string|array|resource|StreamInterface $body Message body.
120
     * @param null|array                                 $headers
121
     *
122
     * @return array
123
     */
124
    protected function parseProperties($method, $url, $queryParams = [], $body = null, $headers = [])
125
    {
126
        // URL
127
        $query = "";
31✔
128
        if (!empty($queryParams) && is_array($queryParams)) {
31✔
129
            foreach ($queryParams as $key => $value) {
2✔
130
                if (is_array($value)) {
2✔
131
                    foreach ($value as $val) {
1✔
132
                        $query .= $key . "=" . urlencode($val) . "&";
1✔
133
                    }
134
                } else {
135
                    $query .= $key . "=" . urlencode($value) . "&";
2✔
136
                }
137
            }
138
        }
139
        $query = rtrim($query, '&');
31✔
140
        if ($query != "") {
31✔
141
            $url = $url . (stristr($url, '?') ? '&' : '?') . $query;
2✔
142
        }
143
        // Headers
144

145
        $contentType = null;
31✔
146
        $accept = null;
31✔
147

148
        foreach ($headers as $k => $v) {
31✔
149

150
            if (strtolower($k) == 'content-type') {
27✔
151
                $contentType = $v;
26✔
152
            }
153

154
            if (strtolower($k) == 'accept') {
27✔
155
                $accept = $v;
1✔
156
            }
157

158
        }
159

160
        if (!$contentType) {
31✔
161
            $contentType = 'application/json';
16✔
162
            $headers['content-type'] = $contentType;
16✔
163
        }
164

165
        if ($accept) {
31✔
166
            $headers['accept'] = $accept;
1✔
167
        }
168

169
        // Body
170

171
        if ($contentType && !empty($body)) {
31✔
172

173
            switch (strtolower($contentType)) {
29✔
174
                case 'application/json':
29✔
175
                    $body = json_encode($body);
7✔
176
                    break;
7✔
177

178
                case 'application/x-www-form-urlencoded';
25✔
179
                    $body = http_build_query($body);
24✔
180
                    break;
24✔
181

182
                default:
183
                    break;
1✔
184
            }
185

186
        }
187

188
        // Create request
189

190
        return [
31✔
191
            'method' => $method,
31✔
192
            'url' => $url,
31✔
193
            'headers' => $headers,
31✔
194
            'body' => $body,
31✔
195
        ];
31✔
196

197
    }
198

199
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc