77
88namespace Magento \Ups \Model ;
99
10+ use Magento \Framework \App \Cache \Type \Config as Cache ;
1011use Magento \Framework \App \ObjectManager ;
1112use Magento \Framework \Exception \LocalizedException ;
1213use Magento \Framework \Exception \NoSuchEntityException ;
1617class UpsAuth
1718{
1819 public const TEST_AUTH_URL = 'https://wwwcie.ups.com/security/v1/oauth/token ' ;
20+ public const CACHE_KEY_PREFIX = 'ups_api_token_ ' ;
1921
2022 /**
2123 * @var AsyncClientInterface
2224 */
2325 private $ asyncHttpClient ;
2426
27+ /**
28+ * @var Cache
29+ */
30+ private $ cache ;
31+
2532 /**
2633 * @param AsyncClientInterface|null $asyncHttpClient
34+ * @param Cache $cacheManager
2735 */
28- public function __construct (AsyncClientInterface $ asyncHttpClient = null )
36+ public function __construct (AsyncClientInterface $ asyncHttpClient = null , Cache $ cacheManager )
2937 {
3038 $ this ->asyncHttpClient = $ asyncHttpClient ?? ObjectManager::getInstance ()->get (AsyncClientInterface::class);
39+ $ this ->cache = $ cacheManager ;
3140 }
3241
3342 /**
@@ -41,32 +50,39 @@ public function __construct(AsyncClientInterface $asyncHttpClient = null)
4150 */
4251 public function getAccessToken ($ clientId , $ clientSecret )
4352 {
44- $ headers = [
45- 'Content-Type ' => 'application/x-www-form-urlencoded ' ,
46- 'x-merchant-id ' => 'string ' ,
47- 'Authorization ' => 'Basic ' . base64_encode ("$ clientId: $ clientSecret " ),
48- ];
49- $ authPayload = http_build_query ([
50- 'grant_type ' => 'client_credentials ' ,
51- ]);
52- try {
53- $ asyncResponse = $ this ->asyncHttpClient ->request (new Request (
54- self ::TEST_AUTH_URL ,
55- Request::METHOD_POST ,
56- $ headers ,
57- $ authPayload
58- ));
53+
54+ $ cacheKey = self ::CACHE_KEY_PREFIX ;
55+ $ result = $ this ->cache ->load ($ cacheKey );
56+ if (!$ result ) {
57+ $ headers = [
58+ 'Content-Type ' => 'application/x-www-form-urlencoded ' ,
59+ 'x-merchant-id ' => 'string ' ,
60+ 'Authorization ' => 'Basic ' . base64_encode ("$ clientId: $ clientSecret " ),
61+ ];
62+ $ authPayload = http_build_query ([
63+ 'grant_type ' => 'client_credentials ' ,
64+ ]);
65+ try {
66+ $ asyncResponse = $ this ->asyncHttpClient ->request (new Request (
67+ self ::TEST_AUTH_URL ,
68+ Request::METHOD_POST ,
69+ $ headers ,
70+ $ authPayload
71+ ));
5972 $ responseResult = $ asyncResponse ->get ();
6073 $ responseData = $ responseResult ->getBody ();
6174 $ responseData = json_decode ($ responseData );
62- if (isset ($ responseData ->access_token )) {
63- $ result = $ responseData ->access_token ;
64- } else {
65- throw new \Magento \Framework \Exception \LocalizedException (__ ('Unable to retrieve access token. ' ));
75+ if (isset ($ responseData ->access_token )) {
76+ $ result = $ responseData ->access_token ;
77+ $ this ->cache ->save ($ result , $ cacheKey , [], $ responseData ->expires_in ?: 10000 );
78+ } else {
79+ throw new \Magento \Framework \Exception \LocalizedException (__ ('Unable to retrieve access token. ' ));
80+ }
81+ return $ result ;
82+ } catch (\Magento \Framework \HTTP \AsyncClient \HttpException $ e ) {
83+ throw new \Magento \Framework \Exception \LocalizedException (__ ('Error occurred: %1 ' , $ e ->getMessage ()));
6684 }
67- return $ result ;
68- } catch (\Magento \Framework \HTTP \AsyncClient \HttpException $ e ) {
69- throw new \Magento \Framework \Exception \LocalizedException (__ ('Error occurred: %1 ' , $ e ->getMessage ()));
7085 }
86+ return $ result ;
7187 }
7288}
0 commit comments