7
7
8
8
namespace Magento \Ups \Model ;
9
9
10
+ use Magento \Framework \App \Cache \Type \Config as Cache ;
10
11
use Magento \Framework \App \ObjectManager ;
11
12
use Magento \Framework \Exception \LocalizedException ;
12
13
use Magento \Framework \Exception \NoSuchEntityException ;
16
17
class UpsAuth
17
18
{
18
19
public const TEST_AUTH_URL = 'https://wwwcie.ups.com/security/v1/oauth/token ' ;
20
+ public const CACHE_KEY_PREFIX = 'ups_api_token_ ' ;
19
21
20
22
/**
21
23
* @var AsyncClientInterface
22
24
*/
23
25
private $ asyncHttpClient ;
24
26
27
+ /**
28
+ * @var Cache
29
+ */
30
+ private $ cache ;
31
+
25
32
/**
26
33
* @param AsyncClientInterface|null $asyncHttpClient
34
+ * @param Cache $cacheManager
27
35
*/
28
- public function __construct (AsyncClientInterface $ asyncHttpClient = null )
36
+ public function __construct (AsyncClientInterface $ asyncHttpClient = null , Cache $ cacheManager )
29
37
{
30
38
$ this ->asyncHttpClient = $ asyncHttpClient ?? ObjectManager::getInstance ()->get (AsyncClientInterface::class);
39
+ $ this ->cache = $ cacheManager ;
31
40
}
32
41
33
42
/**
@@ -41,32 +50,39 @@ public function __construct(AsyncClientInterface $asyncHttpClient = null)
41
50
*/
42
51
public function getAccessToken ($ clientId , $ clientSecret )
43
52
{
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
+ ));
59
72
$ responseResult = $ asyncResponse ->get ();
60
73
$ responseData = $ responseResult ->getBody ();
61
74
$ 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 ()));
66
84
}
67
- return $ result ;
68
- } catch (\Magento \Framework \HTTP \AsyncClient \HttpException $ e ) {
69
- throw new \Magento \Framework \Exception \LocalizedException (__ ('Error occurred: %1 ' , $ e ->getMessage ()));
70
85
}
86
+ return $ result ;
71
87
}
72
88
}
0 commit comments