22
33use Exception ;
44use GuzzleHttp \Message \Response ;
5- use GuzzleHttp \Stream \Stream ;
65
76class AtomxClient extends ApiClient {
7+ const API_BASE = 'https://api.atomx.com/v3/ ' ;
88 protected $ apiBase = null ;
99 protected $ id = null ;
10- protected $ requiresLogin = true ;
10+ protected $ requiresToken = true ;
1111
1212 /**
1313 * @var AccountStore Store the token for the application
1414 */
15- private $ accountStore ;
15+ protected $ accountStore = null ;
1616
17- function __construct (AccountStore $ accountStore , $ idOrFields = null )
17+ /**
18+ * AtomxClient constructor.
19+ * @param AccountStore|null $accountStore
20+ * @param int|array $idOrFields
21+ * @param string $apiBase
22+ */
23+ function __construct ($ accountStore = null , $ idOrFields = null )
1824 {
19- $ this ->apiBase = $ accountStore ->getApiBase ();
25+ if ($ accountStore ) {
26+ $ this ->accountStore = $ accountStore ;
27+ $ this ->apiBase = $ accountStore ->getApiBase ();
28+ } else {
29+ $ this ->apiBase = AtomxClient::API_BASE ;
30+ }
2031
2132 parent ::__construct ();
2233
23- $ this ->accountStore = $ accountStore ;
24-
25-
2634 if (is_array ($ idOrFields ))
2735 $ this ->fields = $ idOrFields ;
2836 else if (is_numeric ($ idOrFields ))
@@ -54,7 +62,7 @@ protected function handleResponse(Response $response)
5462 return json_decode ($ response ->getBody ()->getContents (), true );
5563 }
5664
57- if ($ code == 401 ) {
65+ if ($ code == 401 && $ this -> requiresToken ) {
5866 // Unauthorized, invalidate token
5967 $ this ->accountStore ->storeToken (null );
6068 }
@@ -67,56 +75,14 @@ protected function getDefaultOptions()
6775 {
6876 $ options = parent ::getDefaultOptions ();
6977
70- if ($ this ->requiresLogin )
78+ if ($ this ->requiresToken )
7179 $ options ['headers ' ] = ['Authorization ' => 'Bearer ' . $ this ->getToken ()];
7280
7381 return $ options ;
7482 }
7583
76- public function login ()
77- {
78- $ this ->requiresLogin = false ;
79-
80- try {
81- $ response = $ this ->postUrl ('login ' , [
82- 'json ' => [
83- 'email ' => $ this ->accountStore ->getUsername (),
84- 'password ' => $ this ->accountStore ->getPassword ()
85- ]
86- ]);
87- } catch (ApiException $ e ) {
88- $ message = str_replace ($ e ->getMessage (), $ this ->accountStore ->getPassword (), '[redacted] ' );
89-
90- throw new ApiException ('Unable to login to API! Message: ' . $ message );
91- }
92-
93- $ this ->requiresLogin = true ;
94-
95- if ($ response instanceof Stream) {
96- $ response = json_decode ($ response ->getContents (), true );
97- }
98-
99- if ($ response ['success ' ] !== true )
100- throw new ApiException ('Unable to login to API! ' );
101-
102-
103- $ token = $ response ['auth_token ' ];
104-
105- $ this ->accountStore ->storeToken ($ token );
106-
107- return $ response ['user ' ];
108- }
109-
11084 private function getToken ()
11185 {
112- $ token = $ this ->accountStore ->getToken ();
113-
114- if ($ token !== null ) {
115- return $ token ;
116- }
117-
118- $ this ->login ();
119-
12086 return $ this ->accountStore ->getToken ();
12187 }
12288
0 commit comments