@@ -58,6 +58,45 @@ public function __construct(array $config)
5858 $ this ->config = $ config ;
5959 }
6060
61+ /**
62+ * Get the sudo token credentials.
63+ *
64+ * @param string $userId
65+ * @return array
66+ * @throws \Chatkit\Exceptions\MissingArgumentException
67+ * @throws \Exception
68+ */
69+ public function getSudoToken ($ userId = '' ): array
70+ {
71+ return $ this ->getToken ($ userId , true );
72+ }
73+
74+ /**
75+ * Get the token credentials.
76+ *
77+ * @param string $userId
78+ * @param bool $sudo
79+ * @return array
80+ * @throws \Chatkit\Exceptions\MissingArgumentException
81+ * @throws \Exception
82+ */
83+ public function getToken ($ userId = '' , $ sudo = false ): array
84+ {
85+ if (!$ userId ) {
86+ throw new \Exception ('User id is required. ' );
87+ }
88+
89+ $ chatkit = new ChatkitSDK ([
90+ 'instance_locator ' => $ this ->config ['instance_locator ' ],
91+ 'key ' => $ this ->config ['key ' ]
92+ ]);
93+
94+ return $ chatkit ->authenticate ([
95+ 'su ' => $ sudo ,
96+ 'user_id ' => $ userId
97+ ])['body ' ];
98+ }
99+
61100 /**
62101 * Set the cache store.
63102 *
@@ -103,6 +142,8 @@ public function withUser($userId, $sudo = false): Chatkit
103142
104143 $ this ->api ['sudo ' ] = $ sudo ;
105144
145+ $ this ->httpClient = null ;
146+
106147 return $ this ;
107148 }
108149
@@ -152,30 +193,6 @@ protected function createHandlerStack(): HandlerStack
152193 return $ stack ;
153194 }
154195
155- /**
156- * Get the token credentials.
157- *
158- * @return void
159- * @throws \Chatkit\Exceptions\MissingArgumentException
160- * @throws \Exception
161- */
162- public function generateToken (): void
163- {
164- if (!array_key_exists ('userId ' , $ this ->api ) || !array_key_exists ('sudo ' , $ this ->api )) {
165- throw new \Exception ('Api settings not set ' );
166- }
167-
168- $ chatkit = new ChatkitSDK ([
169- 'instance_locator ' => $ this ->config ['instance_locator ' ],
170- 'key ' => $ this ->config ['key ' ]
171- ]);
172-
173- $ this ->api ['token ' ] = $ chatkit ->authenticate ([
174- 'su ' => $ this ->api ['sudo ' ],
175- 'user_id ' => $ this ->api ['userId ' ]
176- ])['body ' ]['access_token ' ];
177- }
178-
179196 /**
180197 * Get the Http Client.
181198 *
@@ -187,7 +204,11 @@ protected function getHttpClient(): Client
187204 {
188205 if (is_null ($ this ->httpClient )) {
189206
190- $ this ->generateToken ();
207+ if (!array_key_exists ('userId ' , $ this ->api ) || !array_key_exists ('sudo ' , $ this ->api )) {
208+ throw new \Exception ('Api settings not set ' );
209+ }
210+
211+ $ this ->api ['token ' ] = $ this ->getToken ($ this ->api ['userId ' ], $ this ->api ['sudo ' ])['access_token ' ];
191212
192213 $ this ->httpClient = new Client ([
193214 'base_uri ' => Chatkit::BASE_URI ,
0 commit comments