コネクタを利用するAPIの実装
ここでは『業務システムへの接続の構成』で定義したSOAP Webサービス呼び出しのコネクタFIF_SoapWS_xxをカスタムAPIにから呼び出す処理を追加してみます。
Package.jsonの編集
カスタムAPIからコネクタを利用する場合は、package.jsonに利用するコネクタのパスとバージョンを記述する必要があります。
package.jsonの変更後
{
"name" : "fif_incidentreport_xx",
"version" : "1.0.0",
"description" : "インシデント・レポートを取得・編集します。",
"main" : "fif_incidentreport_xx.js",
"oracleMobile" : {
"dependencies" : {
"apis" : { },
"connectors" : {
"/mobile/connector/FIF_SoapWS_xx":"1.0" ←追加
}
}
}
}
注意: 「FIF_SoapWS_xx」は作成したコネクタのAPI名と置き換えてください
エンドポイントPOST /incidents
エンドポイント「POST /incidents」では、SOAPコネクタのエンドポイント「CreateIncident」を呼び出します。
POST /incidentsの変更前
service.post('/mobile/custom/FIF_IncidentReport_xx/incidents', function(req,res) {
var result = {};
var statusCode = 201;
if (statusCode == 201){
var acceptType = req.accepts(['application/json']);
if (acceptType == 'application/json'){
result = {
"id": 20934,
"title": "Lynn's Leaking Water Heater",
"contact": {
"name": "Lynn Adams",
"street": "45 O'Connor Street",
"city": "Ottawa",
"postalcode": "a1a1a1",
"username": "johnbeta"
},
"status" : "New",
"driveTime" : 30,
"priority" : "high",
"notes" : "My notes",
"createdon": "2014-01-20 23:15:03 EDT",
"imageLink": "storage/..."
};
}
}
res.send(statusCode, result);
});
POST /incidentsの変更後
service.post('/mobile/custom/FIF_IncidentReport_xx/incidents', function(req,res) {
req.oracleMobile.connectors.FIF_SoapWS_xx.post('CreateIncident',
{Body: {CreateIncident: req.body}},
{inType: "json"}
).then(
function (success) {
res.send(success.statusCode, success.result);
},
function (failure) {
res.send(500, failure.error);
}
);
});
備考:
C:\HOL\AMCe\code\IncidentPOST.txt
をコピーすることも可能です。
エンドポイント GET /incidents/{id}
エンドポイント「GET /incidents/{id}」では、SOAPコネクタのエンドポイント「GetIncidentById」を呼び出します。
GET /incidents/{id} の変更前
service.get('/mobile/custom/FIF_IncidentReport_xx/incidents/:id', function(req,res) {
var result = {};
var statusCode = 200;
if (statusCode == 200){
var acceptType = req.accepts(['application/json']);
if (acceptType == 'application/json'){
result = {
"id": 20934,
"title": "Lynn's Leaking Water Heater",
"contact": {
"name": "Lynn Adams",
"street": "45 O'Connor Street",
"city": "Ottawa",
"postalcode": "a1a1a1",
"username": "johnbeta"
},
"status" : "New",
"driveTime" : 30,
"priority" : "high",
"notes" : "My notes",
"createdon": "2014-01-20 23:15:03 EDT",
"imageLink": "storage/..."
};
}
}
res.send(statusCode, result);
});
GET /incidents/{id} の変更後
service.get('/mobile/custom/FIF_IncidentReport_xx/incidents/:id', function(req,res) {
req.oracleMobile.connectors.FIF_SoapWS_xx.post(
'GetIncidentById',
{Body: {GetIncidentById: {'IncidentId': req.params.id}}},
{inType: "json"}
).then(
function (success) {
var responsePayload = JSON.parse(success.result);
if (responsePayload.Body.GetIncidentByIdResponse == null) {
res.send(404,
'The requested resource could not be found ' +
'but may be available again in the future');
}
res.send(success.statusCode, responsePayload);
},
function (failure) {
res.send(500, failure.error);
}
);
});
備考:
C:\HOL\AMCe\code\IncidentGETbyID.txt
をコピーすることも可能です。
エンドポイント GET /incidents
エンドポイント「GET /incidents」では、クエリー・パラメータ「contact」が指定されている場合はSOAPコネクタのエンドポイント「GetIncidentsByCustomer」を、クエリー・パラメータ「technician」が指定されている場合は、SOAPコネクタのエンドポイント「GetIncidentsByTechnician」を呼び出します。
GET /incidentsの変更前
service.get('/mobile/custom/FIF_IncidentReport_xx/incidents', function(req,res) {
var result = {};
var statusCode = 200;
if (statusCode == 200){
var acceptType = req.accepts(['application/json']);
if (acceptType == 'application/json'){
result = {
"items":
[
{
"id": 100,
"title": "Leaking Water Heater",
"createdon": "2014-07-18 17:21:55 PST",
"contact": {
"name": "Lynn Adams",
"street": "45 O'Connor Street",
"city": "Ottawa",
"postalcode": "a1a1a1",
"username": "johnbeta"
},
"status" : "New",
"priority" : "High",
"driveTime" : 30,
"imageLink": "storage/..."
},
{
"id": 101,
"title": "Smoking Oven",
"createdon": "2014-07-18 17:21:55 PST",
"contact": {
"name": "Lynn Adams",
"street": "45 O'Connor Street",
"city": "Ottawa",
"postalcode": "a1a1a1",
"username": "johnbeta"
},
"status" : "In Progress",
"priority" : "Medium",
"driveTime" : 10,
"imageLink": "storage..."
}
]
};
}
}
res.send(statusCode, result);
});
GET /incidentsの変更後
service.get('/mobile/custom/FIF_IncidentReport_xx/incidents', function(req,res) {
var payload = {};
var resource = {};
if (req.query.contact){
resource = 'GetIncidentsByCustomer';
payload = {
Body: {
GetIncidentsByCustomer: {
'UserName': req.query.contact
}
}
};
}
else if (req.query.technician){
resource = 'GetIncidentsByTechnician';
payload = {
Body: {
GetIncidentsByTechnician: {
'TechnicianEmailAddress': req.query.technician
}
}
};
}
req.oracleMobile.connectors.FIF_SoapWS_xx.post(
resource, payload, {inType: "json"}
).then(
function (success) {
responsePayload = JSON.parse(success.result);
console.log('getIncidents ' +
JSON.stringify(responsePayload));
if (req.query.contact){
if (!responsePayload.Body.GetIncidentsByCustomerResponse){
res.send(404,
'Incidents can not be found for the requested contact');
}
}
else if (req.query.technician){
if (!responsePayload.Body.GetIncidentsByTechnicianResponse){
res.send(404,
'Incidents can not be found for the requested technician');
}
}
res.send(success.statusCode, responsePayload);
},
function (failure) {
res.send(500, failure.error);
}
);
});
備考:
C:\HOL\AMCe\code\IncidentGETbyCUSTOMER.txt
をコピーすることも可能です。