11<?php namespace Atomx ;
22
3+ use Atomx \Exceptions \ApiException ;
34use Exception ;
45use GuzzleHttp \Message \Response ;
5- use GuzzleHttp \Stream \Stream ;
66
77class AtomxClient extends ApiClient {
8+ const API_BASE = 'https://api.atomx.com/v3/ ' ;
89 protected $ apiBase = null ;
910 protected $ id = null ;
11+ protected $ requiresToken = true ;
1012
1113 /**
12- * @var AccountStore Store the token for the application
14+ * @var TokenStore Store the token for the application
1315 */
14- private $ accountStore ;
15- private $ shouldSendToken = true ;
16+ protected $ accountStore = null ;
1617
17- function __construct (AccountStore $ accountStore , $ idOrFields = null )
18+ /**
19+ * AtomxClient constructor.
20+ * @param TokenStore|null $accountStore
21+ * @param int|array $idOrFields
22+ * @param string $apiBase
23+ */
24+ function __construct ($ accountStore = null , $ idOrFields = null )
1825 {
19- $ this ->apiBase = $ accountStore ->getApiBase ();
26+ if ($ accountStore ) {
27+ $ this ->accountStore = $ accountStore ;
28+ $ this ->apiBase = $ accountStore ->getApiBase ();
29+ } else if ($ this ->requiresToken ) {
30+ throw new \InvalidArgumentException ("{$ this ->endpoint } endpoint requires an AccountStore for the token " );
31+ } else {
32+ $ this ->apiBase = AtomxClient::API_BASE ;
33+ }
2034
2135 parent ::__construct ();
2236
23- $ this ->accountStore = $ accountStore ;
24-
25-
2637 if (is_array ($ idOrFields ))
2738 $ this ->fields = $ idOrFields ;
2839 else if (is_numeric ($ idOrFields ))
@@ -48,62 +59,33 @@ public function setId($id)
4859
4960 protected function handleResponse (Response $ response )
5061 {
51- // TODO: Handle an invalid token/not logged in message
52- return json_decode (parent ::handleResponse ($ response ), true );
53- }
54-
55- protected function getDefaultOptions ()
56- {
57- $ options = parent ::getDefaultOptions ();
58-
59- if ($ this ->shouldSendToken )
60- $ options ['headers ' ] = ['Authorization ' => $ this ->getToken ()];
61-
62- return $ options ;
63- }
62+ $ code = $ response ->getStatusCode ();
6463
65- public function login ()
66- {
67- $ this ->shouldSendToken = false ;
68-
69- try {
70- $ response = $ this ->postUrl ('login ' , [
71- 'json ' => [
72- 'email ' => $ this ->accountStore ->getUsername (),
73- 'password ' => $ this ->accountStore ->getPassword ()
74- ]
75- ]);
76- } catch (ApiException $ e ) {
77- throw new ApiException ('Unable to login to API! ' );
64+ if ($ code == 200 ) {
65+ return json_decode ($ response ->getBody ()->getContents (), true );
7866 }
7967
80- if ($ response instanceof Stream) {
81- $ response = json_decode ($ response ->getContents (), true );
68+ if ($ code == 401 && $ this ->requiresToken ) {
69+ // Unauthorized, invalidate token
70+ $ this ->accountStore ->storeToken (null );
8271 }
8372
84- $ this ->shouldSendToken = true ;
85-
86- if ($ response ['success ' ] !== true )
87- throw new ApiException ('Unable to login to API! ' );
88-
73+ throw new ApiException ('Request failed, received the following status: ' .
74+ $ response ->getStatusCode () . ' Body: ' . $ response ->getBody ()->getContents ());
75+ }
8976
90- $ token = 'Bearer ' . $ response ['auth_token ' ];
77+ protected function getDefaultOptions ()
78+ {
79+ $ options = parent ::getDefaultOptions ();
9180
92- $ this ->accountStore ->storeToken ($ token );
81+ if ($ this ->requiresToken )
82+ $ options ['headers ' ] = ['Authorization ' => 'Bearer ' . $ this ->getToken ()];
9383
94- return $ response [ ' user ' ] ;
84+ return $ options ;
9585 }
9686
9787 private function getToken ()
9888 {
99- $ token = $ this ->accountStore ->getToken ();
100-
101- if ($ token !== null ) {
102- return $ token ;
103- }
104-
105- $ this ->login ();
106-
10789 return $ this ->accountStore ->getToken ();
10890 }
10991
0 commit comments