summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--testapi/opnfv_testapi/ui/app.js36
-rw-r--r--testapi/opnfv_testapi/ui/components/deploy-results/deployResultsController.js14
-rw-r--r--testapi/opnfv_testapi/ui/components/pods/podsController.js43
-rw-r--r--testapi/opnfv_testapi/ui/components/projects/projects.html9
-rw-r--r--testapi/opnfv_testapi/ui/components/projects/projectsController.js23
-rw-r--r--testapi/opnfv_testapi/ui/components/results/resultsController.js16
-rw-r--r--testapi/testapi-client/__init__.py0
-rw-r--r--testapi/testapi-client/setup.cfg47
-rw-r--r--testapi/testapi-client/testapiclient/cli/__init__.py0
-rw-r--r--testapi/testapi-client/testapiclient/cli/auth.py (renamed from testapi/testapi-client/testapiclient/auth.py)4
-rw-r--r--testapi/testapi-client/testapiclient/cli/pods.py (renamed from testapi/testapi-client/testapiclient/pods.py)33
-rw-r--r--testapi/testapi-client/testapiclient/cli/projects.py (renamed from testapi/testapi-client/testapiclient/projects.py)33
-rw-r--r--testapi/testapi-client/testapiclient/http_client.py74
-rw-r--r--testapi/testapi-client/testapiclient/main.py12
-rw-r--r--testapi/testapi-client/testapiclient/utils/__init__.py0
-rw-r--r--testapi/testapi-client/testapiclient/utils/command.py (renamed from testapi/testapi-client/testapiclient/command.py)16
-rw-r--r--testapi/testapi-client/testapiclient/utils/http_client.py68
-rw-r--r--testapi/testapi-client/testapiclient/utils/identity.py (renamed from testapi/testapi-client/testapiclient/identity.py)23
-rw-r--r--testapi/testapi-client/testapiclient/utils/url_parse.py (renamed from testapi/testapi-client/testapiclient/url_parse.py)0
-rw-r--r--testapi/testapi-client/testapiclient/utils/user.py (renamed from testapi/testapi-client/testapiclient/user.py)0
20 files changed, 243 insertions, 208 deletions
diff --git a/testapi/opnfv_testapi/ui/app.js b/testapi/opnfv_testapi/ui/app.js
index def88d2..ada7577 100644
--- a/testapi/opnfv_testapi/ui/app.js
+++ b/testapi/opnfv_testapi/ui/app.js
@@ -28,6 +28,42 @@
angular
.module('testapiApp')
+ .service("keepState", function(){
+ this.filter = {};
+ });
+
+ angular
+ .module('testapiApp')
+ .service('sortService', function(){
+
+ this.sortFunction = function(data, field, ascending){
+ if(ascending){
+ data.sort(function(a,b) {
+ if (a[field].toLowerCase() > b[field].toLowerCase()) {
+ return -1;
+ }
+ if (a[field].toLowerCase() < b[field].toLowerCase()) {
+ return 1;
+ }
+ return 0;
+ });
+ }else{
+ data.sort(function(a,b) {
+ if (a[field].toLowerCase() < b[field].toLowerCase()) {
+ return -1;
+ }
+ if (a[field].toLowerCase() > b[field].toLowerCase()) {
+ return 1;
+ }
+ return 0;
+ });
+ }
+ return data
+ }
+ });
+
+ angular
+ .module('testapiApp')
.directive('dynamicModel', ['$compile', '$parse', function ($compile, $parse) {
return {
restrict: 'A',
diff --git a/testapi/opnfv_testapi/ui/components/deploy-results/deployResultsController.js b/testapi/opnfv_testapi/ui/components/deploy-results/deployResultsController.js
index 5230a75..1128825 100644
--- a/testapi/opnfv_testapi/ui/components/deploy-results/deployResultsController.js
+++ b/testapi/opnfv_testapi/ui/components/deploy-results/deployResultsController.js
@@ -20,7 +20,8 @@
.controller('DeployResultsController', DeployResultsController);
DeployResultsController.$inject = [
- '$scope', '$http', '$filter', '$state', 'testapiApiUrl','raiseAlert'
+ '$scope', '$http', '$filter', '$state', 'testapiApiUrl','raiseAlert',
+ 'keepState'
];
/**
@@ -29,7 +30,7 @@
* a listing of community uploaded results.
*/
function DeployResultsController($scope, $http, $filter, $state, testapiApiUrl,
- raiseAlert) {
+ raiseAlert, keepState) {
var ctrl = this;
ctrl.open = open;
@@ -122,6 +123,15 @@
function filterList(){
if(ctrl.filter && ctrl.filterText!="" && ctrl.filterText!=undefined){
ctrl.tagArray[ctrl.filter] = ctrl.filterText;
+ if(!keepState.filter.deployResultFilter){
+ keepState.filter.deployResultFilter = {}
+ }
+ keepState.filter.deployResultFilter[ctrl.filter] = ctrl.filterText
+ }
+ else if(Object.keys(ctrl.tagArray).length==0){
+ if(keepState.filter.deployResultFilter){
+ ctrl.tagArray = keepState.filter.deployResultFilter
+ }
}
ctrl.showError = false;
var content_url = testapiApiUrl + '/deployresults' +
diff --git a/testapi/opnfv_testapi/ui/components/pods/podsController.js b/testapi/opnfv_testapi/ui/components/pods/podsController.js
index 81f6c0a..f405ecb 100644
--- a/testapi/opnfv_testapi/ui/components/pods/podsController.js
+++ b/testapi/opnfv_testapi/ui/components/pods/podsController.js
@@ -21,7 +21,7 @@
PodsController.$inject = [
'$scope', '$http', '$filter', '$state', '$window', '$uibModal', 'testapiApiUrl','raiseAlert',
- 'confirmModal'
+ 'confirmModal', 'keepState', 'sortService'
];
/**
@@ -30,7 +30,7 @@
* through pods declared in TestAPI.
*/
function PodsController($scope, $http, $filter, $state, $window, $uibModal, testapiApiUrl,
- raiseAlert, confirmModal) {
+ raiseAlert, confirmModal, keepState, sortService) {
var ctrl = this;
ctrl.url = testapiApiUrl + '/pods';
ctrl.checkBox = []
@@ -47,33 +47,11 @@
ctrl.podDelete = podDelete
ctrl.batchDelete = batchDelete;
ctrl.viewPod = viewPod
- ctrl.filterText = ''
ctrl.sortBy = sortBy
function sortBy(field){
- if(ctrl.sorting[field]){
- ctrl.data.pods.sort(function(a,b) {
- if (a[field].toLowerCase() > b[field].toLowerCase()) {
- return -1;
- }
- if (a[field].toLowerCase() < b[field].toLowerCase()) {
- return 1;
- }
- return 0;
- });
- ctrl.sorting[field] = false
- }else{
- ctrl.data.pods.sort(function(a,b) {
- if (a[field].toLowerCase() < b[field].toLowerCase()) {
- return -1;
- }
- if (a[field].toLowerCase() > b[field].toLowerCase()) {
- return 1;
- }
- return 0;
- });
- ctrl.sorting[field] = true
- }
+ ctrl.data.pods = sortService.sortFunction(ctrl.data.pods, field , ctrl.sorting[field] )
+ ctrl.sorting[field]=!ctrl.sorting[field]
}
/**
@@ -96,7 +74,6 @@
function create(pod) {
ctrl.showError = false;
ctrl.showSuccess = false;
- console.log(pod);
if(pod.name != ""){
var pods_url = ctrl.url;
var body = {
@@ -127,13 +104,22 @@
function listPods() {
ctrl.showError = false;
var reqURL = ctrl.url;
- if(ctrl.filterText!=''){
+ if(ctrl.filterText!=undefined){
reqURL = ctrl.url + "?name=" + ctrl.filterText
}
+ else if(keepState.filter.podFilter){
+ for (var filter in keepState.filter.podFilter){
+ reqURL = ctrl.url + '?' + filter + '=' + keepState.filter.podFilter[filter]
+ ctrl.filterText = keepState.filter.podFilter[filter]
+ }
+ }
ctrl.podsRequest =
$http.get(reqURL).success(function (data) {
ctrl.data = data;
ctrl.sortBy("name")
+ keepState.filter.podFilter = {
+ 'name': ctrl.filterText
+ }
}).catch(function (data) {
ctrl.data = null;
ctrl.showError = true;
@@ -195,7 +181,6 @@
* message
*/
function openDeleteModal(name) {
- console.log(name)
confirmModal("Delete", 'pod', ctrl.podDelete, name);
}
diff --git a/testapi/opnfv_testapi/ui/components/projects/projects.html b/testapi/opnfv_testapi/ui/components/projects/projects.html
index 84902f8..5d514d1 100644
--- a/testapi/opnfv_testapi/ui/components/projects/projects.html
+++ b/testapi/opnfv_testapi/ui/components/projects/projects.html
@@ -38,7 +38,14 @@
<tr style="
text-align: center;">
<th style="width: 1%;">Bulk Select</th>
- <th style="width: 19%;">Name</th>
+ <th style="width: 19%;">Name
+ <a class="text-danger" ng-click="ctrl.sortByName()" ng-class="{ 'hidden': !ctrl.sortName }" >
+ <span class="glyphicon glyphicon-sort-by-alphabet pull-right" aria-hidden="true"></span>
+ </a>
+ <a class="text-danger" ng-click="ctrl.sortByName()" ng-class="{ 'hidden': ctrl.sortName }" >
+ <span class="glyphicon glyphicon-sort-by-alphabet-alt pull-right" aria-hidden="true"></span>
+ </a>
+ </th>
<th style="width: 70%;">Description</th>
<th style="width: 10%;" ng-class="{'hidden': ! ((auth.projectNames.length>0) && auth.isAuthenticated) && authenticate}">Operations</th>
</tr>
diff --git a/testapi/opnfv_testapi/ui/components/projects/projectsController.js b/testapi/opnfv_testapi/ui/components/projects/projectsController.js
index 0d0ec99..07a58fe 100644
--- a/testapi/opnfv_testapi/ui/components/projects/projectsController.js
+++ b/testapi/opnfv_testapi/ui/components/projects/projectsController.js
@@ -21,7 +21,7 @@
ProjectsController.$inject = [
'$scope', '$http', '$filter', '$state', '$window', '$uibModal', 'testapiApiUrl',
- 'raiseAlert', 'confirmModal', 'authenticate'
+ 'raiseAlert', 'confirmModal', 'authenticate', 'keepState', 'sortService'
];
/**
@@ -30,7 +30,7 @@
* through projects declared in TestAPI.
*/
function ProjectsController($scope, $http, $filter, $state, $window, $uibModal, testapiApiUrl,
- raiseAlert, confirmModal, authenticate) {
+ raiseAlert, confirmModal, authenticate, keepState, sortService) {
var ctrl = this;
ctrl.url = testapiApiUrl + '/projects';
@@ -44,12 +44,13 @@
ctrl.openBatchDeleteModal = openBatchDeleteModal;
ctrl.projectDelete = projectDelete;
ctrl.batchDelete = batchDelete;
+ ctrl.sortByName = sortByName
ctrl.checkBox = [];
ctrl.checkBoxList = [];
ctrl.name = '';
ctrl.details = '';
- ctrl.filterText='';
+ ctrl.ascending = false;
/**
* This will contact the TestAPI to create a new project.
@@ -73,6 +74,11 @@
});
}
+ function sortByName(){
+ ctrl.data.projects = sortService.sortFunction(ctrl.data.projects, 'name', ctrl.ascending)
+ ctrl.ascending = !ctrl.ascending
+ }
+
/**
* This will open the modal that will show the create
* project view
@@ -148,13 +154,22 @@
ctrl.showError = false;
var content_url = ctrl.url + '?';
var filterText = ctrl.filterText;
- if(filterText != ''){
+ if(filterText != undefined){
content_url = content_url + 'name=' +
filterText;
}
+ else if(keepState.filter.projectFilter){
+ for (var filter in keepState.filter.projectFilter){
+ content_url = content_url + filter + '=' + keepState.filter.projectFilter[filter]
+ ctrl.filterText = keepState.filter.projectFilter[filter]
+ }
+ }
ctrl.resultsRequest =
$http.get(content_url).success(function (data) {
ctrl.data = data;
+ keepState.filter.projectFilter = {
+ 'name': ctrl.filterText
+ }
}).catch(function (data) {
ctrl.data = null;
ctrl.showError = true;
diff --git a/testapi/opnfv_testapi/ui/components/results/resultsController.js b/testapi/opnfv_testapi/ui/components/results/resultsController.js
index 24f9f55..13ead6e 100644
--- a/testapi/opnfv_testapi/ui/components/results/resultsController.js
+++ b/testapi/opnfv_testapi/ui/components/results/resultsController.js
@@ -38,7 +38,8 @@
}]);
ResultsController.$inject = [
- '$scope', '$http', '$filter', '$state', 'testapiApiUrl','raiseAlert'
+ '$scope', '$http', '$filter', '$state', 'testapiApiUrl', 'raiseAlert',
+ 'keepState'
];
/**
@@ -47,7 +48,7 @@
* a listing of community uploaded results.
*/
function ResultsController($scope, $http, $filter, $state, testapiApiUrl,
- raiseAlert) {
+ raiseAlert, keepState) {
var ctrl = this;
ctrl.open = open;
@@ -219,6 +220,15 @@
function filterList(){
if(ctrl.filter && ctrl.filterText!="" && ctrl.filterText!=undefined){
ctrl.tagArray[ctrl.filter] = ctrl.filterText;
+ if(!keepState.filter.resultFilter){
+ keepState.filter.resultFilter = {}
+ }
+ keepState.filter.resultFilter[ctrl.filter] = ctrl.filterText
+ }
+ else if(Object.keys(ctrl.tagArray).length==0){
+ if(keepState.filter.resultFilter){
+ ctrl.tagArray = keepState.filter.resultFilter
+ }
}
ctrl.showError = false;
var content_url = testapiApiUrl + '/results' +
@@ -260,6 +270,7 @@
});
ctrl.filterText = ''
}
+
ctrl.filterList();
@@ -283,6 +294,7 @@
function clearFilters() {
ctrl.tagArray = {}
ctrl.filter = undefined
+ keepState.filter.resultFilter = {}
ctrl.filterList();
}
}
diff --git a/testapi/testapi-client/__init__.py b/testapi/testapi-client/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/testapi/testapi-client/__init__.py
diff --git a/testapi/testapi-client/setup.cfg b/testapi/testapi-client/setup.cfg
index 672c9ae..72a5a57 100644
--- a/testapi/testapi-client/setup.cfg
+++ b/testapi/testapi-client/setup.cfg
@@ -14,43 +14,18 @@ console_scripts =
testapi = testapiclient.main:main
testapi =
- auth = testapiclient.auth:Auth
- pod create = testapiclient.pods:PodCreate
- pod get = testapiclient.pods:PodGet
- pod delete = testapiclient.pods:PodDelete
- pod getone = testapiclient.pods:PodGetOne
+ auth = testapiclient.cli.auth:Auth
+ pod create = testapiclient.cli.pods:PodCreate
+ pod get = testapiclient.cli.pods:PodGet
+ pod delete = testapiclient.cli.pods:PodDelete
+ pod getone = testapiclient.cli.pods:PodGetOne
+
+ project create = testapiclient.cli.projects:ProjectCreate
+ project get = testapiclient.cli.projects:ProjectGet
+ project getone = testapiclient.cli.projects:ProjectGetOne
+ project delete = testapiclient.cli.projects:ProjectDelete
+ project put = testapiclient.cli.projects:ProjectPut
- project create = testapiclient.projects:ProjectCreate
- project get = testapiclient.projects:ProjectGet
- project getone = testapiclient.projects:ProjectGetOne
- project delete = testapiclient.projects:ProjectDelete
- project put = testapiclient.projects:ProjectPut
-
- testcase create = testapiclient.testcase:TestCaseCreate
- testcase get = testapiclient.testcase:TestCaseGet
- testcase delete = testapiclient.testcase:TestCaseDelete
- testcase put = testapiclient.testcase:TestCasePut
-
- scenario create = testapiclient.scenario:ScenarioCreate
- scenario get = testapiclient.scenario:ScenarioGet
- scenario delete = testapiclient.scenario:ScenarioDelete
- scenario put = testapiclient.scenario:ScenarioPut
-
- scenario addscore = testapiclient.scenario:ScenarioAddScore
-
- scenario addyi = testapiclient.scenario:ScenarioAddTI
-
- scenario addcustom = testapiclient.scenario:ScenarioAddCustom
- scenario updatecustom = testapiclient.scenario:ScenarioUpdateCustom
- scenario deletecustom = testapiclient.scenario:ScenarioDeleteCustom
-
- scenario addproject = testapiclient.scenario:ScenarioAddProject
- scenario deleteproject = testapiclient.scenario:ScenarioDeleteProject
-
- scenario addversion = testapiclient.scenario:ScenarioAddVersion
- scenario deleteversion = testapiclient.scenario:ScenarioDeleteVersion
-
- result get = testapiclient.results:ResultGet
[egg_info]
tag_build =
tag_date = 0
diff --git a/testapi/testapi-client/testapiclient/cli/__init__.py b/testapi/testapi-client/testapiclient/cli/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/testapi/testapi-client/testapiclient/cli/__init__.py
diff --git a/testapi/testapi-client/testapiclient/auth.py b/testapi/testapi-client/testapiclient/cli/auth.py
index 3728498..434c55a 100644
--- a/testapi/testapi-client/testapiclient/auth.py
+++ b/testapi/testapi-client/testapiclient/cli/auth.py
@@ -1,5 +1,5 @@
-from testapiclient import command
-from testapiclient import identity
+from testapiclient.utils import command
+from testapiclient.utils import identity
class Auth(command.Command):
diff --git a/testapi/testapi-client/testapiclient/pods.py b/testapi/testapi-client/testapiclient/cli/pods.py
index c49f254..cdedc3e 100644
--- a/testapi/testapi-client/testapiclient/pods.py
+++ b/testapi/testapi-client/testapiclient/cli/pods.py
@@ -1,9 +1,9 @@
import json
-from testapiclient import command
-from testapiclient import http_client
-from testapiclient import identity
-from testapiclient import url_parse
+from testapiclient.utils import command
+from testapiclient.utils import http_client as client
+from testapiclient.utils import identity
+from testapiclient.utils import url_parse
def pods_url():
@@ -25,9 +25,7 @@ class PodGet(command.Lister):
return parser
def take_action(self, parsed_args):
- pods = http_client.get(self.filter_by_name(pods_url(),
- parsed_args))
- print pods
+ self.show(client.get(self.filter_by_name(pods_url(), parsed_args)))
class PodGetOne(command.ShowOne):
@@ -35,15 +33,13 @@ class PodGetOne(command.ShowOne):
def get_parser(self, prog_name):
parser = super(PodGetOne, self).get_parser(prog_name)
- parser.add_argument('-name',
+ parser.add_argument('name',
default='',
- help='Find pod using name',
- required=True)
+ help='Find pod using name')
return parser
def take_action(self, parsed_args):
- pods = http_client.get(pod_url(parsed_args))
- print pods
+ self.show(client.get(pod_url(parsed_args)))
class PodCreate(command.Command):
@@ -62,11 +58,8 @@ class PodCreate(command.Command):
@identity.authenticate
def take_action(self, parsed_args):
- response = http_client.post(pods_url(), parsed_args.pod)
- if response.status_code == 200:
- print "Pod has been successfully created!"
- else:
- print response.text
+ self.show('Create',
+ client.post(pods_url(), parsed_args.pod))
class PodDelete(command.Command):
@@ -74,12 +67,12 @@ class PodDelete(command.Command):
def get_parser(self, prog_name):
parser = super(PodDelete, self).get_parser(prog_name)
- parser.add_argument('-name',
+ parser.add_argument('name',
type=str,
- required=True,
help='Delete pods using name')
return parser
@identity.authenticate
def take_action(self, parsed_args):
- print http_client.delete(pod_url(parsed_args))
+ self.show('Delete',
+ client.delete(pod_url(parsed_args)))
diff --git a/testapi/testapi-client/testapiclient/projects.py b/testapi/testapi-client/testapiclient/cli/projects.py
index ad42293..113b030 100644
--- a/testapi/testapi-client/testapiclient/projects.py
+++ b/testapi/testapi-client/testapiclient/cli/projects.py
@@ -1,13 +1,13 @@
import json
-from testapiclient import command
-from testapiclient import http_client
-from testapiclient import identity
-from testapiclient import url_parse
+from testapiclient.utils import command
+from testapiclient.utils import http_client as client
+from testapiclient.utils import identity
+from testapiclient.utils import url_parse
def projects_url():
- url_parse.resource_join('projects')
+ return url_parse.resource_join('projects')
def project_url(parsed_args):
@@ -24,9 +24,7 @@ class ProjectGet(command.Lister):
return parser
def take_action(self, parsed_args):
- projects = http_client.get(self.filter_name(projects_url(),
- parsed_args))
- print projects
+ self.show(client.get(self.filter_name(projects_url(), parsed_args)))
class ProjectGetOne(command.ShowOne):
@@ -40,8 +38,7 @@ class ProjectGetOne(command.ShowOne):
return parser
def take_action(self, parsed_args):
- project = http_client.get(project_url(parsed_args))
- print project
+ self.show(client.get(project_url(parsed_args)))
class ProjectCreate(command.Command):
@@ -57,11 +54,8 @@ class ProjectCreate(command.Command):
@identity.authenticate
def take_action(self, parsed_args):
- response = http_client.post(projects_url(), parsed_args.project)
- if response.status_code == 200:
- print "Project has been successfully created!"
- else:
- print response.text
+ self.show('Create',
+ client.post(projects_url(), parsed_args.project))
class ProjectDelete(command.Command):
@@ -76,8 +70,8 @@ class ProjectDelete(command.Command):
@identity.authenticate
def take_action(self, parsed_args):
- projects = http_client.delete(project_url(parsed_args))
- print projects
+ self.show('Delete',
+ client.delete(project_url(parsed_args)))
class ProjectPut(command.Command):
@@ -97,6 +91,5 @@ class ProjectPut(command.Command):
@identity.authenticate
def take_action(self, parsed_args):
- projects = http_client.put(project_url(parsed_args),
- parsed_args.project)
- print projects
+ self.show('Update',
+ client.put(project_url(parsed_args), parsed_args.project))
diff --git a/testapi/testapi-client/testapiclient/http_client.py b/testapi/testapi-client/testapiclient/http_client.py
deleted file mode 100644
index 59b4f8e..0000000
--- a/testapi/testapi-client/testapiclient/http_client.py
+++ /dev/null
@@ -1,74 +0,0 @@
-import json
-
-import requests
-from testapiclient import user
-
-
-class HTTPClient(object):
-
- __instance = None
- headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
-
- @staticmethod
- def get_Instance():
- """ Static access method. """
- if HTTPClient.__instance is None:
- HTTPClient()
- return HTTPClient.__instance
-
- def __init__(self):
- """ Virtually private constructor. """
- if HTTPClient.__instance is not None:
- raise Exception("This class is a singleton!")
- else:
- HTTPClient.__instance = self
-
- def get(self, url):
- r = requests.get(url)
- if r.status_code == 200:
- return r.json()
- else:
- return r.text
-
- def _session_request(self, method, *args, **kwargs):
- return getattr(user.User.session, method)(*args, **kwargs)
-
- def post(self, url, data):
- return self._session_request('post', url,
- data=json.dumps(data),
- headers=HTTPClient.headers)
-
- def put(self, url, data):
- return self._session_request('put', url,
- data=json.dumps(data),
- headers=HTTPClient.headers).text
-
- def delete(self, url, *args):
- if(args.__len__ > 0):
- r = self._session_request('delete', url,
- data=json.dumps(args[0]),
- headers=HTTPClient.headers)
- else:
- r = self._session_request('delete', url)
- return r.text
-
-
-def http_request(method, *args, **kwargs):
- client = HTTPClient.get_Instance()
- return getattr(client, method)(*args, **kwargs)
-
-
-def get(url):
- return http_request('get', url)
-
-
-def post(url, data):
- return http_request('post', url, data)
-
-
-def put(url, data):
- return http_request('put', url, data)
-
-
-def delete(url, data=None):
- return http_request('delete', url, data)
diff --git a/testapi/testapi-client/testapiclient/main.py b/testapi/testapi-client/testapiclient/main.py
index 6c6cf56..dfa6284 100644
--- a/testapi/testapi-client/testapiclient/main.py
+++ b/testapi/testapi-client/testapiclient/main.py
@@ -1,21 +1,21 @@
import sys
-from cliff.app import App
-from cliff.commandmanager import CommandManager
+from cliff import app
+from cliff import commandmanager
import requests
-from testapiclient import user
+from testapiclient.utils import user
-class TestAPIClient(App):
+class TestAPIClient(app.App):
def __init__(self):
super(TestAPIClient, self).__init__(
description='TestAPI Client',
version='0.1',
- command_manager=CommandManager('testapi'),
+ command_manager=commandmanager.CommandManager('testapi'),
deferred_help=True,
- )
+ )
user.User.session = requests.Session()
def initialize_app(self, argv):
diff --git a/testapi/testapi-client/testapiclient/utils/__init__.py b/testapi/testapi-client/testapiclient/utils/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/testapi/testapi-client/testapiclient/utils/__init__.py
diff --git a/testapi/testapi-client/testapiclient/command.py b/testapi/testapi-client/testapiclient/utils/command.py
index 2864f9a..f9c75a7 100644
--- a/testapi/testapi-client/testapiclient/command.py
+++ b/testapi/testapi-client/testapiclient/utils/command.py
@@ -1,5 +1,6 @@
from cliff import command
-from testapiclient import url_parse
+
+from testapiclient.utils import url_parse
class Command(command.Command):
@@ -14,6 +15,11 @@ class Command(command.Command):
return parser
+ def show(self, request, response):
+ print ' '.join([request,
+ 'success' if response.status_code < 300
+ else 'failed: {}'.format(response.reason)])
+
class Lister(command.Command):
@@ -24,6 +30,12 @@ class Lister(command.Command):
return query_url() if parsed_args.name else url
+ def show(self, response):
+ print response.json() if response.status_code < 300 \
+ else 'Get failed: {}'.format(response.reason)
+
class ShowOne(command.Command):
- pass
+ def show(self, response):
+ print response.json() if response.status_code < 300 \
+ else 'Get failed: {}'.format(response.reason)
diff --git a/testapi/testapi-client/testapiclient/utils/http_client.py b/testapi/testapi-client/testapiclient/utils/http_client.py
new file mode 100644
index 0000000..6be33ee
--- /dev/null
+++ b/testapi/testapi-client/testapiclient/utils/http_client.py
@@ -0,0 +1,68 @@
+import json
+
+import requests
+
+from testapiclient.utils import user
+
+
+class HTTPClient(object):
+
+ __instance = None
+ headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
+
+ @staticmethod
+ def get_Instance():
+ """ Static access method. """
+ if HTTPClient.__instance is None:
+ HTTPClient()
+ return HTTPClient.__instance
+
+ def __init__(self):
+ """ Virtually private constructor. """
+ if HTTPClient.__instance is not None:
+ raise Exception("This class is a singleton!")
+ else:
+ HTTPClient.__instance = self
+
+ def get(self, url):
+ return requests.get(url)
+
+ def post(self, url, data):
+ return self._request('post', url,
+ data=json.dumps(data),
+ headers=self.headers)
+
+ def put(self, url, data):
+ return self._request('put', url,
+ data=json.dumps(data),
+ headers=self.headers)
+
+ def delete(self, url, *args):
+ data = json.dumps(args[0]) if len(args) > 0 else None
+ return self._request('delete', url,
+ data=data,
+ headers=self.headers)
+
+ def _request(self, method, *args, **kwargs):
+ return getattr(user.User.session, method)(*args, **kwargs)
+
+
+def _request(method, *args, **kwargs):
+ client = HTTPClient.get_Instance()
+ return getattr(client, method)(*args, **kwargs)
+
+
+def get(url):
+ return _request('get', url)
+
+
+def post(url, data):
+ return _request('post', url, data)
+
+
+def put(url, data):
+ return _request('put', url, data)
+
+
+def delete(url, data=None):
+ return _request('delete', url, data)
diff --git a/testapi/testapi-client/testapiclient/identity.py b/testapi/testapi-client/testapiclient/utils/identity.py
index 03b3ac5..2aeb87a 100644
--- a/testapi/testapi-client/testapiclient/identity.py
+++ b/testapi/testapi-client/testapiclient/utils/identity.py
@@ -3,7 +3,8 @@ import os
import urllib
import requests
-from testapiclient import user
+
+from testapiclient.utils import user
def _authenticate(username, password):
@@ -17,19 +18,21 @@ def _authenticate(username, password):
'form_id': 'user_login'
}
response = session.post(hostname, data)
- user.User.session = session
+ if "login" not in response.text:
+ user.User.session = session
return response
def authenticate(xstep):
@functools.wraps(xstep)
def wrapper(self, parsed_args):
- username = parsed_args.u
- password = parsed_args.p
- if(username and password):
- response = _authenticate(username, password)
- if "login" in response.text:
- print "Authentication has failed."
- else:
- xstep(self, parsed_args)
+ if(user.User.session is None):
+ username = parsed_args.u
+ password = parsed_args.p
+ if(username and password):
+ response = _authenticate(username, password)
+ if "login" in response.text:
+ print "Authentication has failed."
+ return
+ xstep(self, parsed_args)
return wrapper
diff --git a/testapi/testapi-client/testapiclient/url_parse.py b/testapi/testapi-client/testapiclient/utils/url_parse.py
index 08f7a63..08f7a63 100644
--- a/testapi/testapi-client/testapiclient/url_parse.py
+++ b/testapi/testapi-client/testapiclient/utils/url_parse.py
diff --git a/testapi/testapi-client/testapiclient/user.py b/testapi/testapi-client/testapiclient/utils/user.py
index 7e72163..7e72163 100644
--- a/testapi/testapi-client/testapiclient/user.py
+++ b/testapi/testapi-client/testapiclient/utils/user.py