From 4bbfa335349ea9ae7d935e7b7f891278de2521d3 Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Thu, 14 Sep 2017 21:50:52 +0530 Subject: [PATCH] Api integration stage 3: Defined apis for application and platform. --- .../{endpoints.js => applicationMgtApi.js} | 108 ++++++------------ .../publisher/src/api/authHandler.js | 2 - .../publisher/src/api/platformMgtApi.js | 68 +++++++++++ .../publisher/src/common/constants.js | 5 +- .../Application/ApplicationCreate.jsx | 8 +- .../Application/ApplicationListing.jsx | 59 +--------- .../Application/CreateSteps/Step1.jsx | 6 +- .../components/Platform/PlatformCreate.jsx | 8 +- 8 files changed, 124 insertions(+), 140 deletions(-) rename components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/api/{endpoints.js => applicationMgtApi.js} (58%) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/api/platformMgtApi.js diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/api/endpoints.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/api/applicationMgtApi.js similarity index 58% rename from components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/api/endpoints.js rename to components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/api/applicationMgtApi.js index 1491d2363b..11d7d58e86 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/api/endpoints.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/api/applicationMgtApi.js @@ -22,13 +22,11 @@ import AuthHandler from './authHandler'; import Constants from '../common/constants'; import Helper from './helpers/appMgtApiHelpers'; - - -export default class Endpoint { - - /* ================================================================= - * Application related apis - * */ +/** + * Api definitions related to application management. + * TODO: Work to be done on Application release. + * */ +export default class ApplicationMgtApi { /** * Api for create an application. @@ -38,19 +36,26 @@ export default class Endpoint { * From applicationData, the proper application object will be created and send it to the api. * */ static createApplication(applicationData) { - let app = Helper.buildApplication(applicationData).application; + let {application, images} = Helper.buildApplication(applicationData); const headers = AuthHandler.createAuthenticationHeaders("application/json"); - return Axios.post(Constants.appManagerEndpoints.CREATE_APP, app, {headers: headers}); + console.log(application); + console.log(images); + Axios.post(Constants.appManagerEndpoints.CREATE_APP, application, {headers: headers}); } /** * Upload the image artifacts (banner, icon, screenshots) related to the application. * @param appId: The application uuid of the application which the images should be uploaded to. + * @param images: The images object. This contains icon, banner and screenshots. * */ - static uploadImageArtifacts(appId) { - let user = AuthHandler.getUser(); + static uploadImageArtifacts(appId, images) { + let formData = new FormData(); + formData.append('icon', images.icon); + formData.append('banner', images.banner); + formData.append('screenshot', images.screenshots); + console.log("Image", formData); const headers = AuthHandler.createAuthenticationHeaders("multipart/form-data"); - return Axios.post(Constants.appManagerEndpoints.UPLOAD_IMAGES + appId, appId, {headers: headers}); + return Axios.post(Constants.appManagerEndpoints.UPLOAD_IMAGE_ARTIFACTS + appId, formData, {headers: headers}); } /** @@ -63,8 +68,11 @@ export default class Endpoint { /** * Promote the current life cycle state of the application. * @param appId: The uuid of the application which the state should be updated. + * @param nextState: The next lifecycle state that the application can be updated to. + * + * URL Pattern : /application/1.0/ * */ - static updateLifeCycleState(appId) { + static updateLifeCycleState(appId, nextState) { } @@ -86,9 +94,19 @@ export default class Endpoint { return Axios.put(Constants.appManagerEndpoints.CREATE_APP, app, {headers: headers}); } - static editApplicationArtofacts(appId) { + static getApplicationArtifacts(appId, artifactName) { + const headers = AuthHandler.createAuthenticationHeaders("image/png"); + return Axios.get(Constants.appManagerEndpoints.GET_IMAGE_ARTIFACTS + appId + "?name=" + artifactName, + {headers: headers}); + } + + static editApplicationArtifacts(appId, images) { + let formData = new FormData(); + formData.append('icon', images.icon); + formData.append('banner', images.banner); + formData.append('screenshot', images.screenshots); const headers = AuthHandler.createAuthenticationHeaders("application/json"); - return Axios.put(Constants.appManagerEndpoints.CREATE_APP, appId, {headers: headers}); + return Axios.put(Constants.appManagerEndpoints.UPLOAD_IMAGE_ARTIFACTS + appId, formData, {headers: headers}); } /** @@ -96,8 +114,6 @@ export default class Endpoint { * @return Object: The response object from the axios post. * */ static getApplications() { - let user = AuthHandler.getUser(); - console.log("Get all applications", user.getAuthToken()); const headers = AuthHandler.createAuthenticationHeaders("application/json"); return Axios.get(Constants.appManagerEndpoints.GET_ALL_APPS, {headers: headers}); } @@ -107,8 +123,6 @@ export default class Endpoint { * @param appId: The application Id. * */ static getApplication(appId) { - let user = AuthHandler.getUser(); - console.log("Get Application",appId, user.getAuthToken()); const headers = AuthHandler.createAuthenticationHeaders("application/json"); return Axios.get(Constants.appManagerEndpoints.GET_ALL_APPS + appId, {headers: headers}); } @@ -118,59 +132,7 @@ export default class Endpoint { * @param appId: The id of the application which is to be deleted. * */ static deleteApplication(appId) { - - } - - /* - * End of Application management apis. - * ================================================================= - * ================================================================= - * Platform related apis - * */ - - /** - * Create a new Platform - * @param platformData: The platform data object. - * */ - static createPlatform(platformData) { const headers = AuthHandler.createAuthenticationHeaders("application/json"); - Axios.post(Constants.platformManagerEndpoints.CREATE_PLATFORM, platformData, {headers: headers}).then( - function (response) { - console.log(response); - } - ).catch(function (err) { - console.log(err); - }); + return Axios.delete(Constants.appManagerEndpoints.GET_ALL_APPS + appId, {headers: headers}); } - - /** - * Get available platforms - * */ - static getPlatforms() { - const headers = AuthHandler.createAuthenticationHeaders("application/json"); - return Axios.get(Constants.platformManagerEndpoints.GET_ENABLED_PLATFORMS, {headers: headers}); - } - - /** - * Get the user specified platform - * @param platformId: The identifier of the platform - * */ - static getPlatform(platformId) { - const headers = AuthHandler.createAuthenticationHeaders("application/json"); - return Axios.get(Constants.platformManagerEndpoints.GET_PLATFORM + platformId, {headers: headers}); - } - - /** - * Delete specified platform - * @param platformId: The id of the platform which is to be deleted. - * */ - static deletePlatform(platformId) { - const headers = AuthHandler.createAuthenticationHeaders("application/json"); - return Axios.delete(Constants.platformManagerEndpoints.GET_PLATFORM + platformId, {headers: headers}); - } - - /* - * End of Platform management apis. - * ================================================================= - * */ -} +} \ No newline at end of file diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/api/authHandler.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/api/authHandler.js index ffd9426918..f5941423d4 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/api/authHandler.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/api/authHandler.js @@ -57,8 +57,6 @@ class AuthHandler { AuthHandler.setUser(user); } ); - - return login_promise; }; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/api/platformMgtApi.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/api/platformMgtApi.js new file mode 100644 index 0000000000..709343b3d3 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/api/platformMgtApi.js @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +'use strict'; + +import Axios from 'axios'; +import AuthHandler from './authHandler'; +import Constants from '../common/constants'; + +/** + * Api definitions for Platform management. + * */ +export default class PlatformMgtApi{ + /** + * Create a new Platform + * @param platformData: The platform data object. + * */ + static createPlatform(platformData) { + const headers = AuthHandler.createAuthenticationHeaders("application/json"); + Axios.post(Constants.platformManagerEndpoints.CREATE_PLATFORM, platformData, {headers: headers}).then( + function (response) { + console.log(response); + } + ).catch(function (err) { + console.log(err); + }); + } + + /** + * Get available platforms + * */ + static getPlatforms() { + const headers = AuthHandler.createAuthenticationHeaders("application/json"); + return Axios.get(Constants.platformManagerEndpoints.GET_ENABLED_PLATFORMS, {headers: headers}); + } + + /** + * Get the user specified platform + * @param platformId: The identifier of the platform + * */ + static getPlatform(platformId) { + const headers = AuthHandler.createAuthenticationHeaders("application/json"); + return Axios.get(Constants.platformManagerEndpoints.GET_PLATFORM + platformId, {headers: headers}); + } + + /** + * Delete specified platform + * @param platformId: The id of the platform which is to be deleted. + * */ + static deletePlatform(platformId) { + const headers = AuthHandler.createAuthenticationHeaders("application/json"); + return Axios.delete(Constants.platformManagerEndpoints.GET_PLATFORM + platformId, {headers: headers}); + } +} \ No newline at end of file diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/common/constants.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/common/constants.js index 1f0270e03e..336b122e7b 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/common/constants.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/common/constants.js @@ -17,6 +17,8 @@ */ 'use strict'; + +//TODO: Replace the server address with response from auth endpoint and remove hardcoded ids etc. export default class Constants { static scopes = 'perm:application:get perm:application:create perm:application:update perm:application-mgt:login' + @@ -25,7 +27,8 @@ export default class Constants { static appManagerEndpoints = { GET_ALL_APPS: 'https://localhost:8243/api/application-mgt/v1.0/applications/1.0.0/', CREATE_APP: 'https://localhost:8243/api/application-mgt/v1.0/applications/1.0.0/', - UPLOAD_IMAGES: '/api/application-mgt/v1.0/applications/1.0.0/upload-artifacts/', //+appId + UPLOAD_IMAGE_ARTIFACTS: 'https://localhost:8243/api/application-mgt/v1.0/applications/1.0.0/upload-image-artifacts/', //+appId + GET_IMAGE_ARTIFACTS: "https://localhost:8243/api/application-mgt/v1.0/applications/1.0.0/image-artifacts/" }; static platformManagerEndpoints = { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/components/Application/ApplicationCreate.jsx b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/components/Application/ApplicationCreate.jsx index 8f81330022..f466db3608 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/components/Application/ApplicationCreate.jsx +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/components/Application/ApplicationCreate.jsx @@ -18,7 +18,7 @@ import React, {Component} from 'react'; import Dialog from 'material-ui/Dialog'; -import Endpoint from '../../api/endpoints'; +import ApplicationMgtApi from '../../api/applicationMgtApi'; import {withRouter} from 'react-router-dom'; import FlatButton from 'material-ui/FlatButton'; import {Step1, Step2, Step3} from './CreateSteps'; @@ -83,10 +83,8 @@ class ApplicationCreate extends Component { * */ handleSubmit() { let stepData = this.state.stepData; - let applicationCreationPromise = Endpoint.createApplication(stepData); - applicationCreationPromise.then(response => { - console.log(response); - let uploadArtifactsPromise = Endpoint.uploadImageArtifacts(response.data.uuid); + let applicationCreationPromise = ApplicationMgtApi.createApplication(stepData); + applicationCreationPromise.then( response => { this.handleYes(); } ).catch( diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/components/Application/ApplicationListing.jsx b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/components/Application/ApplicationListing.jsx index 5c712952f0..e34c6d31f3 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/components/Application/ApplicationListing.jsx +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/components/Application/ApplicationListing.jsx @@ -17,7 +17,7 @@ */ import React, {Component} from 'react'; -import EndPoint from '../../api/endpoints'; +import ApplicationMgtApi from '../../api/applicationMgtApi'; import {withRouter} from 'react-router-dom'; import TextField from 'material-ui/TextField'; import DataTable from '../UIComponents/DataTable'; @@ -49,8 +49,6 @@ class ApplicationListing extends Component { this.scriptId = "application-listing"; } - data = []; - // headers = [ { data_id: "image", @@ -86,8 +84,6 @@ class ApplicationListing extends Component { ]; componentWillMount() { - //Fetch all the applications from backend and create application objects. - this.setState({data: this.data}); /** *Loading the theme files based on the the user-preference. @@ -101,7 +97,7 @@ class ApplicationListing extends Component { } componentDidMount() { - let getApps = EndPoint.getApplications(); + let getApps = ApplicationMgtApi.getApplications(); getApps.then(response => { let apps = this.setData(response.data.applications); console.log(apps); @@ -112,53 +108,10 @@ class ApplicationListing extends Component { }); } + /** + * Extract application from application list and update the state. + * */ setData(applications) { - // { - // id: Math.random(), - // applicationName: "one", - // platform: 'Android', - // category: "Public", - // status: "Created" - // } - - // - // "uuid":"f59ca462-7fa0-4cef-8536-96c17905e587", - // "name":"sdkfsdkf", - // "shortDescription":"shdkfhsd[f sfs;df dsf","description":"khsdkhfkjdss hfdsff\nsdf\ndsf", - // "tags":["dsfds","f","dsfs"], - // "platform":{ - // "name":"jdslkjfljs", - // "description":"ljlksdjlfjdsljf", - // "identifier":"sdjflsjdfjlkj", - // "fileBased":false, - // "shared":false, - // "enabled":false, - // "defaultTenantMapping":false - // }, - // - // "category":{ - // "id":1 - // }, - // - // "createdAt":"Tue, 12 Sep 2017 18:53:54 IST", - // "modifiedAt":"Tue, 12 Sep 2017 18:53:54 IST", - // "currentLifecycle":{ - // "lifecycleState":{ - // "id":1, - // "name":"CREATED", - // "identifier":"CREATED", - // "description":"Application creation initial state" - // }, - // - // "lifecycleStateModifiedAt":"Tue, 12 Sep 2017 18:53:54 IST", - // "getLifecycleStateModifiedBy":"admin"}, - // "screenShotCount":0, - // "user":{ - // "userName":"admin", - // "tenantId":-1234 - // } - // } - let apps = []; for (let app in applications) { let application = {}; @@ -212,7 +165,7 @@ class ApplicationListing extends Component { } onRowClick(id) { - EndPoint.getApplication(id).then(response => { + ApplicationMgtApi.getApplication(id).then(response => { console.log(response); }).catch(err => { console.log(err) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/components/Application/CreateSteps/Step1.jsx b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/components/Application/CreateSteps/Step1.jsx index 3c256e750e..7f35eb5bf1 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/components/Application/CreateSteps/Step1.jsx +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/components/Application/CreateSteps/Step1.jsx @@ -22,7 +22,7 @@ import MenuItem from 'material-ui/MenuItem'; import SelectField from 'material-ui/SelectField'; import RaisedButton from 'material-ui/RaisedButton'; import Theme from '../../../theme'; -import Endpoint from "../../../api/endpoints"; +import PlatformMgtApi from "../../../api/platformMgtApi"; import AuthHandler from "../../../api/authHandler"; /** @@ -69,7 +69,7 @@ class Step1 extends Component { } componentDidMount() { //Get the list of available platforms and set to the state. - Endpoint.getPlatforms().then(response => { + PlatformMgtApi.getPlatforms().then(response => { console.log(response); this.setPlatforms(response.data); }).catch(err => { @@ -95,7 +95,7 @@ class Step1 extends Component { * Persist the current form data to the state. * */ setStepData() { - console.log(this.state.platforms); + console.log("Platforms",this.state.platforms); let step = { store: this.state.store, platform: this.state.platforms[this.state.platformSelectedIndex] diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/components/Platform/PlatformCreate.jsx b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/components/Platform/PlatformCreate.jsx index 70de1bcf07..ae13f541c7 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/components/Platform/PlatformCreate.jsx +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/components/Platform/PlatformCreate.jsx @@ -27,13 +27,13 @@ import FlatButton from 'material-ui/FlatButton'; import IconButton from 'material-ui/IconButton'; import SelectField from 'material-ui/SelectField'; import RaisedButton from 'material-ui/RaisedButton'; +import PlatformMgtApi from '../../api/platformMgtApi'; import Clear from 'material-ui/svg-icons/content/clear'; import {GridList, GridTile} from 'material-ui/GridList'; import Close from 'material-ui/svg-icons/navigation/close'; import {Card, CardActions, CardTitle} from 'material-ui/Card'; import AddCircleOutline from 'material-ui/svg-icons/content/add-circle-outline'; import Theme from '../../theme'; -import Endpoint from '../../api/endpoints'; /** * Platform Create component. @@ -221,6 +221,9 @@ class PlatformCreate extends Component { } }; + /** + * Create platform object and call the create platform api. + * */ onCreatePlatform() { //Call the platform create api. let platform = {}; @@ -234,8 +237,7 @@ class PlatformCreate extends Component { platform.allTenants = this.state.allTenants; platform.defaultTenantMapping = true; - Endpoint.createPlatform(platform); - + PlatformMgtApi.createPlatform(platform); }