Created
July 7, 2017 07:45
-
-
Save ypcode/8e702e2f368b0200115cfbe3db0b7442 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function (context, req) { | |
context.log('Sending app-only request to Hardware Requests list.'); | |
var siteUrl = "https://<yourtenant>.sharepoint.com/sites/<yoursite>"; | |
var clientId = "<your client id>"; | |
var clientSecret = "<your client secret>"; | |
// Instantiate the service in app-only | |
var service = HardwareRequestService.createAppOnly(siteUrl, clientId, clientSecret); | |
// The body is the request to create | |
var request = req.body && JSON.parse(req.body); | |
context.log(request); | |
if (!request || !request.type) { | |
context.res = { | |
headers: { | |
"Content-Type": "application/json", | |
"Access-Control-Allow-Credentials": "true", | |
"Access-Control-Allow-Origin": "https://<tenant>.sharepoint.com", | |
"Access-Control-Allow-Methods": "GET, POST, OPTIONS", | |
"Access-Control-Allow-Headers": "Content-Type, Set-Cookie", | |
"Access-Control-Max-Age": "86400" | |
}, | |
body: { status: "Properly authenticated" } | |
}; | |
context.log("No BODY"); | |
context.log(context.res); | |
context.done(); | |
return; | |
} | |
context.log("BODY available, submitting the request"); | |
// Submit the request | |
service.submitRequest(request) | |
.then(function () { | |
context.log("Request successfully submitted"); | |
context.res = { | |
headers: { | |
"Content-Type": "application/json", | |
"Access-Control-Allow-Credentials": "true", | |
"Access-Control-Allow-Origin": "https://<tenant>.sharepoint.com", | |
"Access-Control-Allow-Methods": "GET, POST, OPTIONS", | |
"Access-Control-Allow-Headers": "Content-Type, Set-Cookie", | |
"Access-Control-Max-Age": "86400" | |
}, | |
body: { status: "succeeded" } | |
}; | |
context.log(context.res); | |
context.done(); | |
}).catch(function (error) { | |
context.log(error); | |
context.log("Request cannot be submitter"); | |
context.res = { | |
headers: { | |
"Content-Type": "application/json", | |
"Access-Control-Allow-Credentials": "true", | |
"Access-Control-Allow-Origin": "https://<tenant>.sharepoint.com", | |
"Access-Control-Allow-Methods": "GET, POST, OPTIONS", | |
"Access-Control-Allow-Headers": "Content-Type, Set-Cookie", | |
"Access-Control-Max-Age": "86400" | |
}, | |
status: 400, | |
body: { status: "failed" } | |
}; | |
context.log(context.res); | |
context.done(); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment