forked from amirams/spotinst-functions-examples
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlyftAPI.js
139 lines (127 loc) · 3.34 KB
/
lyftAPI.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const rp = require('request-promise')
const formatter = require('./formatter.js')
const header = formatter.formatter[0]
const footer = formatter.formatter[1]
function lyftOAuth(lyftID, lyftSecret){
return({
uri: 'https://api.lyft.com/oauth/token',
method: 'POST',
headers:{
"Content-Type": "application/json"
},
auth: {
'user': lyftID,
'pass': "SANDBOX-"+ lyftSecret
},
body:{
"grant_type": "client_credentials", "scope": "rides.request"
},
json: true
})
}
function lyftRequest(authToken, hereLat, hereLon, thereLat, thereLon, origin, destination, type){
return({
uri: 'https://api.lyft.com/v1/rides',
method: 'POST',
headers:{
'Authorization': 'Bearer ' + authToken,
'Content-Type': 'application/json'},
body: {
"ride_type":type,
"origin":
{"lat":hereLat, "lng":hereLon, "address": origin},
"destination" :
{"lat":thereLat, "lng":thereLon, "destination":destination} },
json: true
})
}
module.exports.main = function main (event, context, callback) {
let lyftID = process.env['lyftID']
let lyftSecret = process.env['lyftSecret']
let spotAccount = process.env['spotAccount']
let spotToken = process.env['spotToken']
let spotEnvironment = process.env['spotEnvironment']
let getLyftVars = {
uri:'https://api.spotinst.io/functions/environment/'+spotEnvironment+'/userDocument/lyftVars',
method: "GET",
qs: {accountId: spotAccount},
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + spotToken},
json:true
}
let deleteLyftVars = {
uri:'https://api.spotinst.io/functions/environment/'+spotEnvironment+'/userDocument/lyftVars',
method: "DELETE",
qs: {accountId: spotAccount},
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + spotToken},
json:true
}
let variables
let html = header
html +=
`
<h1>Ride Requested</h1>
`
rp(getLyftVars).then((res)=>{
variables = JSON.parse(res["response"]["items"][0]["value"])
rp(deleteLyftVars).then((res)=>{
rp(lyftOAuth(lyftID,lyftSecret)).then((res)=>{
let authToken = res.access_token
rp(lyftRequest(authToken, variables["originLat"], variables["originLon"], variables["destLat"], variables["destLon"], variables["origin"], variables["destination"], variables["type"])).then((res)=>{
console.log(res)
html +=
`
<p>
From:
${variables["origin"]}
<br>
To:
${variables["destination"]}
<br>
Type:
${variables["type"]}
<br>
With:
${res["passenger"]["first_name"]}
${res["passenger"]["last_name"]}
</p>
`
html += footer
callback(null, {
statusCode: 200,
body: html,
headers: {"Content-Type": "text/html"}
});
}).catch((err)=>{
callback(null, {
statusCode: 400,
body: "Error Check Logs",
headers: {"Content-Type": "text/html"}
});
})
}).catch((err)=>{
callback(null, {
statusCode: 400,
body: "Error Check Logs",
headers: {"Content-Type": "text/html"}
});
})
}).catch((err)=>{
callback(null, {
statusCode: 400,
body: "Error Check Logs",
headers: {"Content-Type": "text/html"}
});
})
}).catch((err)=>{
console.log(err)
callback(null, {
statusCode: 400,
body: "Error Check Logs",
headers: {"Content-Type": "text/html"}
});
})
};