Skip to content

Commit 5032a5d

Browse files
author
Jarkko Laitinen
committed
Initial commit of php-sdk
0 parents  commit 5032a5d

File tree

4 files changed

+290
-0
lines changed

4 files changed

+290
-0
lines changed

.gitignore

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Created by https://www.gitignore.io/api/phpstorm
2+
3+
### PhpStorm ###
4+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio
5+
6+
*.iml
7+
8+
## Directory-based project format:
9+
.idea/
10+
# if you remove the above rule, at least ignore the following:
11+
12+
# User-specific stuff:
13+
# .idea/workspace.xml
14+
# .idea/tasks.xml
15+
# .idea/dictionaries
16+
17+
# Sensitive or high-churn files:
18+
# .idea/dataSources.ids
19+
# .idea/dataSources.xml
20+
# .idea/sqlDataSources.xml
21+
# .idea/dynamic.xml
22+
# .idea/uiDesigner.xml
23+
24+
# Gradle:
25+
# .idea/gradle.xml
26+
# .idea/libraries
27+
28+
# Mongo Explorer plugin:
29+
# .idea/mongoSettings.xml
30+
31+
## File-based project format:
32+
*.ipr
33+
*.iws
34+
35+
## Plugin-specific files:
36+
37+
# IntelliJ
38+
/out/
39+
40+
# mpeltonen/sbt-idea plugin
41+
.idea_modules/
42+
43+
# JIRA plugin
44+
atlassian-ide-plugin.xml
45+
46+
# Crashlytics plugin (for Android Studio and IntelliJ)
47+
com_crashlytics_export_strings.xml
48+
crashlytics.properties
49+
crashlytics-build.properties
50+

httpful.phar

60.6 KB
Binary file not shown.

src/Api.php

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
}

src/Util.php

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
function getLink($obj, $rel = 'self') {
4+
if (is_null($obj)) {
5+
throw new Exception("Argument is null.");
6+
}
7+
8+
if (!is_object($obj)) {
9+
throw new Exception("Argument is not an object.");
10+
}
11+
12+
if (!property_exists($obj, 'Meta')) {
13+
throw new Exception("Object does not contain Meta.");
14+
}
15+
16+
$meta = $obj->Meta;
17+
$self = null;
18+
19+
foreach($meta as $m) {
20+
if($m->Rel === "self") {
21+
$self = $m;
22+
break;
23+
}
24+
}
25+
unset($m);
26+
27+
if ($rel === 'self') {
28+
if (is_object($obj) && property_exists($obj,'VersionNumber')) {
29+
$self->VersionNumber = $obj->VersionNumber;
30+
}
31+
return $self;
32+
}
33+
34+
$op = null;
35+
36+
foreach($meta as $m2) {
37+
if($m2->Rel === $rel) {
38+
$op = $m2;
39+
break;
40+
}
41+
}
42+
unset($m2);
43+
44+
$newUri = $self->Uri.$op->Uri;
45+
$link = new stdClass();
46+
$link->Method = $op->Method;
47+
$link->Rel = $op->Rel;
48+
$link->Uri = $newUri;
49+
$link->Type = $op->Type;
50+
if (is_object($obj) && property_exists($obj,'VersionNumber')) {
51+
$link->VersionNumber = $obj->VersionNumber;
52+
}
53+
54+
return $link;
55+
}
56+
57+
function createVehicle($name, $lat, $lon) {
58+
$location = new stdClass();
59+
$location->Coordinate = new stdClass();
60+
$location->Coordinate->Latitude = $lat;
61+
$location->Coordinate->Longitude = $lon;
62+
$location->Coordinate->System = "WGS84";
63+
64+
$vehicle = new stdClass();
65+
$vehicle->Name = $name;
66+
$vehicle->StartLocation = $location;
67+
$vehicle->EndLocation = $location;
68+
$vehicle->RelocationType = "None";
69+
70+
$vehicle->TimeWindows = createTimeWindow(24);
71+
$vehicle->Capacities = array(array("Amount"=>100, "Name"=>"Weight"));
72+
73+
return $vehicle;
74+
}
75+
76+
function createTask($name, $startLat, $startLon, $endLat, $endLon) {
77+
78+
$startLocation = new stdClass();
79+
$startLocation->Coordinate = new stdClass();
80+
$startLocation->Coordinate->Latitude = $startLat;
81+
$startLocation->Coordinate->Longitude = $startLon;
82+
$startLocation->Coordinate->System = "WGS84";
83+
84+
$endLocation = new stdClass();
85+
$endLocation->Coordinate = new stdClass();
86+
$endLocation->Coordinate->Latitude = $endLat;
87+
$endLocation->Coordinate->Longitude = $endLon;
88+
$endLocation->Coordinate->System = "WGS84";
89+
90+
$pickup = new stdClass();
91+
$pickup->Location = $startLocation;
92+
$pickup->TimeWindow = createTimeWindow(24);
93+
$pickup->Capacities = array(array("Amount"=>1, "Name"=>"Weight"));
94+
$pickup->Type = "Pickup";
95+
96+
$delivery = new stdClass();
97+
$delivery->Location = $endLocation;
98+
$delivery->TimeWindow = createTimeWindow(24);
99+
$delivery->Capacities = array(array("Amount"=>1, "Name"=>"Weight"));
100+
$delivery->Type = "Delivery";
101+
102+
$task = new stdClass();
103+
$task->Name = $name;
104+
$task->Info = $name;
105+
$task->RelocationType = "None";
106+
107+
$task->TaskEvents = array($pickup, $delivery);
108+
return $task;
109+
}
110+
111+
function createTimeWindow($duration) {
112+
$timeWindow = new stdClass();
113+
$now = new DateTime();
114+
$timeWindow->Start = $now->format('Y-m-d H:i:s');;
115+
$end = date_add($now, date_interval_create_from_date_string($duration.' hours'));
116+
$timeWindow->End = $end->format('Y-m-d H:i:s');
117+
118+
return $timeWindow;
119+
}
120+
121+
function pvar_dump($v) {
122+
echo '<pre>';
123+
var_dump($v);
124+
echo '</pre>';
125+
}

0 commit comments

Comments
 (0)