Skip to content

Commit 15778db

Browse files
committed
Merge pull request #741 from apinf/feature/publish-api-backend-on-update
Feature/publish api backend on update. Closes #713
2 parents 8f4ccc1 + 0ab4a34 commit 15778db

File tree

4 files changed

+94
-3
lines changed

4 files changed

+94
-3
lines changed

client/views/api_backends/add-backend-workflow/wizard.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,18 @@ Template.addApiBackendWizard.helpers({
103103

104104
// Set frontend host to API Umbrella host value
105105
apiBackend.frontend_host = apiUmbrellaHost;
106-
106+
107107
// Create the API Backend on API Umbrella
108108
Meteor.call('createApiBackendOnApiUmbrella', apiBackend, function(error, apiUmbrellaWebResponse) {
109109
if (apiUmbrellaWebResponse.http_status === 200) {
110+
// Get API Backend from API Umbrella response
111+
var apiBackend = apiUmbrellaWebResponse.result.data.api;
112+
110113
// Insert the API Backend
111114
var apiBackendId = ApiBackends.insert(apiBackend);
112115

113116
// Get the ID of the newly created API Backend
114-
var apiUmbrellaApiId = apiUmbrellaWebResponse.result.data.api.id;
117+
var apiUmbrellaApiId = apiBackend.id;
115118

116119
// Tell Wizard the submission is complete
117120
context.done();

client/views/api_backends/api_backend_form/autoform.js

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,61 @@ AutoForm.hooks({
4444
context.result(false);
4545
}
4646
});
47+
},
48+
update: function (apiBackendForm) {
49+
// Keep the context to use inside the callback function
50+
var context = this;
51+
52+
// Get current API Backend document for modification
53+
var apiBackend = context.currentDoc;
54+
55+
// Get the set of updated properties
56+
var setApiBackendProperties = context.updateDoc.$set;
57+
58+
// Get the set of properties to remove
59+
var unsetApiBackendProperties = context.updateDoc.$unset;
60+
61+
// Update properties on API Backend document
62+
for (let property in setApiBackendProperties) {
63+
apiBackend[property] = setApiBackendProperties[property];
64+
}
65+
66+
// Delete unused properties from API Backend object
67+
for (let property in unsetApiBackendProperties) {
68+
delete apiBackend[property];
69+
}
70+
71+
// Get ID of API Umbrella backend (not the Apinf document ID)
72+
var apiUmbrellaBackendId = apiBackend.id;
73+
74+
// Send the API Backend to API Umbrella
75+
response = Meteor.call(
76+
'updateApiBackendOnApiUmbrella',
77+
apiUmbrellaBackendId,
78+
apiBackend,
79+
function(error, apiUmbrellaWebResponse) {
80+
// Check for API Umbrella error
81+
if (apiUmbrellaWebResponse.http_status === 204) {
82+
// If status is OK, submit form
83+
context.result(apiBackendForm);
84+
} else {
85+
// If there are errors
86+
var errors = _.values(apiUmbrellaWebResponse.errors);
87+
88+
// Flatten all error descriptions to show using sAlert
89+
errors = _.flatten(errors);
90+
_.each(errors, function(error) {
91+
//Display error to the user, keep the sAlert box visible.
92+
sAlert.error(error, {timeout: 'none'});
93+
// TODO: Figure out a way to send the errors back to the autoform fields, as if it were client validation,
94+
// and get rid of sAlert here.
95+
});
96+
97+
// Cancel form submission on error,
98+
// so user see the error message and edit the incorrect fields
99+
context.result(false);
100+
}
101+
});
47102
}
48103
},
49104
onSuccess: function () {
@@ -81,11 +136,13 @@ AutoForm.hooks({
81136

82137
// Publish the API Backend on API Umbrella
83138
Meteor.call('publishApiBackendOnApiUmbrella', apiUmbrellaApiId, function(error, apiUmbrellaWebResponse) {
84-
console.log(apiUmbrellaWebResponse);
85139

140+
// Check for a successful response
86141
if (apiUmbrellaWebResponse.http_status === 201) {
142+
// Alert the user of the success
87143
sAlert.success("API Backend successfully published.");
88144
} else {
145+
// If there are errors, inform the user
89146
var errors = _.values(apiUmbrellaWebResponse.errors);
90147

91148
// Flatten all error descriptions to show using sAlert

server/methods/apiBackends.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,29 @@ Meteor.methods({
6161
apiUmbrellaWebResponse.http_status = 422;
6262
}
6363
return apiUmbrellaWebResponse;
64+
},
65+
updateApiBackendOnApiUmbrella: function (apiUmbrellaBackendId, apiBackend) {
66+
// Construct an API Backend object for API Umbrella with one 'api' key
67+
var constructedBackend = {
68+
"api": apiBackend
69+
};
70+
71+
// Response object to be send back to client layer.
72+
var apiUmbrellaWebResponse = {
73+
result: {},
74+
http_status: 204,
75+
errors: {}
76+
};
77+
78+
try {
79+
// Send the API Backend to API Umbrella's endpoint for creation in the backend
80+
apiUmbrellaWebResponse.result = apiUmbrellaWeb.adminApi.v1.apiBackends.updateApiBackend(apiUmbrellaBackendId, constructedBackend);
81+
} catch (apiUmbrellaError) {
82+
83+
//set the errors object
84+
apiUmbrellaWebResponse.errors = {'default': [apiUmbrellaError.message]};
85+
apiUmbrellaWebResponse.http_status = 422;
86+
}
87+
return apiUmbrellaWebResponse;
6488
}
6589
});

server/startup.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,12 @@ Meteor.startup(function () {
5454
"backend_host": 1
5555
});
5656

57+
// Fetch all API Backends
58+
var apiBackends = ApiBackends.find().fetch();
59+
60+
// Check each backend to see if it has API Umbrella ID
61+
apiBackends.forEach(function (apiBackend) {console.log("id: ", apiBackend.id)});
62+
63+
// Initialize cron jobs
5764
SyncedCron.start();
5865
});

0 commit comments

Comments
 (0)