Skip to content

Commit 2376f8e

Browse files
committed
Merge branch 'release/0.1.1'
2 parents 71f12e4 + 9669101 commit 2376f8e

File tree

5 files changed

+52
-5
lines changed

5 files changed

+52
-5
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
}
1313
],
1414
"require": {
15-
"php": ">=5.4.1",
16-
"kriswallsmith/buzz":"dev-master"
15+
"kriswallsmith/buzz": "v0.10"
1716
},
1817
"require-dev": {
1918
"phpunit/phpunit": "3.7.*",

demo.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
require './vendor/autoload.php';
33

44
$client = new \Chatwork\Client();
5-
$client->authenticate('your api token');
5+
$client->authenticate('4596ce2716db695e1c88884e9fa3041d');
66
//var_dump($client->api('me')->show());
77
//var_dump($client->api('my')->status());
8-
//var_dump($client->api('my')->tasks());
8+
var_dump($client->api('my')->tasks());
99

1010
var_dump($client->api('rooms')->members(1)->show());
1111

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace Chatwork\Exception;
3+
4+
/**
5+
* Class InvalidApiTokenException
6+
* @package Chatwork\Exception
7+
*/
8+
class InvalidApiException extends \RuntimeException
9+
{
10+
}

lib/Chatwork/HttpClient/HttpClient.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Buzz\Client\Curl;
1212

13+
use Chatwork\HttpClient\Listener\ResponseFilterException;
1314
use Chatwork\HttpClient\Message\Request;
1415
use Chatwork\HttpClient\Message\Response;
1516

@@ -47,6 +48,7 @@ public function __construct(array $options = [], ClientInterface $client = null)
4748
$client->setTimeout($this->options['timeout']);
4849
$client->setVerifyPeer(false);
4950

51+
$this->addListener(new ResponseFilterException());
5052
$this->client = $client;
5153
}
5254

@@ -149,7 +151,7 @@ public function request($path, array $parameters = [], $httpMethod = 'GET', arra
149151
$request = $this->createRequest($httpMethod, $path);
150152
$request->addHeaders($headers);
151153
if (count($parameters) > 0) {
152-
$request->setContent(json_encode($parameters, JSON_FORCE_OBJECT));
154+
$request->setContent(http_build_query($parameters));
153155
}
154156

155157
$this->executeListeners('preSend', $request);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: polidog
5+
* Date: 2013/12/07
6+
* Time: 15:18
7+
*/
8+
9+
namespace Chatwork\HttpClient\Listener;
10+
use Buzz\Listener\ListenerInterface;
11+
use Buzz\Message\MessageInterface;
12+
use Buzz\Message\RequestInterface;
13+
use Chatwork\Exception\InvalidApiException;
14+
use Chatwork\Exception\InvalidApiTokenException;
15+
16+
class ResponseFilterException implements ListenerInterface
17+
{
18+
19+
public function preSend(RequestInterface $request)
20+
{
21+
}
22+
23+
public function postSend(RequestInterface $request, MessageInterface $response)
24+
{
25+
$content = $response->getContent();
26+
if (isset($content["errors"])) {
27+
$message = "";
28+
if (is_array($content["errors"])) {
29+
$message = implode("\n", $content["errors"]);
30+
} else if (is_string($content["errors"])) {
31+
$message = $content["errors"];
32+
}
33+
throw new InvalidApiException($message);
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)