1
+ <?php
2
+
3
+ namespace NFleet ;
4
+ include ('../httpful.phar ' );
5
+
6
+ class Api
7
+ {
8
+ public $ url = '' ;
9
+ public $ username = '' ;
10
+ public $ password = '' ;
11
+ public $ currentToken = null ;
12
+ private $ ifNoneMatch = 'If-None-Match ' ;
13
+
14
+ public function __construct ($ url , $ uname , $ passw ) {
15
+ $ this ->url = $ url ;
16
+ $ this ->username = $ uname ;
17
+ $ this ->password = $ passw ;
18
+ }
19
+
20
+ public function authenticate () {
21
+ $ this ->authenticateAndGetTokenData ($ this ->username , $ this ->password );
22
+ return $ this ->currentToken ;
23
+ }
24
+
25
+ public function getRoot () {
26
+ $ response = \Httpful \Request::get ($ this ->url .'/ ' )
27
+ ->addHeader ("Authorization " , $ this ->getAuthorizationToken ())
28
+ ->addHeader ("Accept " ,"application/json " )
29
+ ->follow_redirects (false )
30
+ ->send ();
31
+ return $ response ->body ;
32
+ }
33
+
34
+ public function navigate ($ link , $ data = null , $ queryParams = null ) {
35
+ $ request = \Httpful \Request::init ()
36
+ ->addHeader ("Authorization " , $ this ->getAuthorizationToken ())
37
+ ->method ($ link ->Method )
38
+ ->uri ($ this ->url .$ link ->Uri )
39
+ ->addHeader ("Accept " ,$ link ->Type )
40
+ ->contentType ($ link ->Type );
41
+
42
+ if (($ link ->Method === "POST " || $ link ->Method === "PUT " ) && $ data !== null ) {
43
+ if (property_exists ($ link , 'VersionNumber ' )) {
44
+ $ request ->addHeader ($ this ->ifNoneMatch , $ link ->VersionNumber );
45
+ }
46
+ $ request ->body (json_encode ($ data ));
47
+ }
48
+ $ response = $ request ->send ();
49
+
50
+ $ result = null ;
51
+ if ($ link ->Method === "GET " ) {
52
+ $ result = $ response ->body ;
53
+
54
+ if (isset ($ response ->headers ['etag ' ])){
55
+ $ result ->VersionNumber = $ response ->headers ['etag ' ];
56
+ }
57
+
58
+ } elseif ($ link ->Method === "POST " || $ link ->Method === "PUT " ) {
59
+ $ result = $ this ->createResponseData ($ response ->headers ['location ' ], $ response ->headers ['Content-Type ' ]);
60
+ }
61
+
62
+ return $ result ;
63
+ }
64
+
65
+ private function getAuthorizationToken () {
66
+ return $ this ->currentToken ->TokenType .' ' .$ this ->currentToken ->AccessToken ;
67
+ }
68
+
69
+ private function createResponseData ($ location , $ contentType ) {
70
+ $ path = parse_url ($ location , PHP_URL_PATH );
71
+ $ link = new \stdClass ();
72
+ $ link ->Method = "GET " ;
73
+ $ link ->Rel = "location " ;
74
+ $ link ->Uri = $ path ;
75
+ $ link ->Type = $ contentType ;
76
+ return $ link ;
77
+ }
78
+
79
+ private function authenticateAndGetTokenData ($ key , $ secret ) {
80
+ $ authenticationUrl = $ this ->getAuthLocation ();
81
+ if ( $ authenticationUrl === null ) {
82
+ return ;
83
+ }
84
+ else {
85
+ $ tokenLocation = $ this ->authenticateAndGetTokenLocation ( $ key , $ secret , $ authenticationUrl );
86
+ $ this ->currentToken = $ this ->requestToken ($ tokenLocation );
87
+ }
88
+ }
89
+
90
+ private function requestToken ($ location ) {
91
+ $ response = \Httpful \Request::get ($ location )
92
+ ->addHeader ("Accept " ,"application/json " )
93
+ ->expects ("application/json " )
94
+ ->follow_redirects (false )
95
+ ->send ();
96
+ return $ response ->body ;
97
+ }
98
+
99
+ private function getAuthLocation () {
100
+ $ uri = $ this ->url ;
101
+ $ response = \Httpful \Request::get ($ uri )->send ();
102
+
103
+ return $ response ->headers ['location ' ];
104
+ }
105
+
106
+ private function authenticateAndGetTokenLocation ( $ key , $ secret , $ authenticationUrl ){
107
+ $ base64encoded = base64_encode ($ key .': ' .$ secret );
108
+ $ response = \Httpful \Request::post ($ authenticationUrl )
109
+ ->addHeader ('Authorization ' , 'Basic ' .$ base64encoded )
110
+ ->body ("{ 'Scope': 'data optimization' } " )
111
+ ->follow_redirects (false )->send ();
112
+
113
+ return $ response ->headers ['location ' ];
114
+ }
115
+ }
0 commit comments