diff options
author | thuva4 <tharma.thuva@gmail.com> | 2018-02-08 22:42:55 +0530 |
---|---|---|
committer | thuva4 <tharma.thuva@gmail.com> | 2018-02-11 09:08:22 +0530 |
commit | a5450070c0877604f89a1148a90d1e4c3e5ee96f (patch) | |
tree | 739ba166adbadbe5ed4d4c45d472f8978a255ac8 | |
parent | 86f8a2036521e9401d8c9431e4b6412878e6d94f (diff) |
add projects in testapi client
Add CRUD functionalities for projects in testapi
client
Change-Id: I9db23625a7ef12385f42266e68c1d9ffca74fd2c
Signed-off-by: thuva4 <tharma.thuva@gmail.com>
-rw-r--r-- | testapi/testapi-client/setup.cfg | 1 | ||||
-rw-r--r-- | testapi/testapi-client/testapiclient/projects.py | 104 |
2 files changed, 105 insertions, 0 deletions
diff --git a/testapi/testapi-client/setup.cfg b/testapi/testapi-client/setup.cfg index 1e25b73..8174720 100644 --- a/testapi/testapi-client/setup.cfg +++ b/testapi/testapi-client/setup.cfg @@ -25,6 +25,7 @@ testapi = 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 diff --git a/testapi/testapi-client/testapiclient/projects.py b/testapi/testapi-client/testapiclient/projects.py new file mode 100644 index 0000000..57def5d --- /dev/null +++ b/testapi/testapi-client/testapiclient/projects.py @@ -0,0 +1,104 @@ +import json +from user import User +from cliff.command import Command +from httpClient import HTTPClient +from authHandler import AuthHandler +from config import Config + + +class ProjectBase(Command): + projects_url = Config.config.get("api", "url") + "/projects" + + +class ProjectGet(ProjectBase): + + def get_parser(self, prog_name): + parser = super(ProjectGet, self).get_parser(prog_name) + parser.add_argument('-name', default='', help='Search projects by name') + return parser + + def take_action(self, parsed_args): + httpClient = HTTPClient.get_Instance() + url = ProjectGet.projects_url + if parsed_args.name: + url = url + "?name=" + parsed_args.name + projects = httpClient.get(url) + print projects + + +class ProjectGetOne(ProjectBase): + + def get_parser(self, prog_name): + parser = super(ProjectGetOne, self).get_parser(prog_name) + parser.add_argument('-name', default='', required=True, help='Search project by name') + return parser + + def take_action(self, parsed_args): + httpClient = HTTPClient.get_Instance() + url = ProjectGet.projects_url + "/" + parsed_args.name + project = httpClient.get(url) + print project + + +class ProjectCreate(ProjectBase): + + def get_parser(self, prog_name): + parser = super(ProjectCreate, self).get_parser(prog_name) + parser.add_argument('-u', type=str, help='Username for authentication') + parser.add_argument('-p', type=str, help='Password for authentication') + parser.add_argument('project', type=json.loads, help='Project create request format :{ "name": (required)"", "description": (optional)""}') + return parser + + def take_action(self, parsed_args): + httpClient = HTTPClient.get_Instance() + if(parsed_args.u and parsed_args.p): + response = AuthHandler.authenticate(parsed_args.u, parsed_args.p) + if "login" in response.text: + print "Authentication has failed. Please check your username and password." + return + response = httpClient.post(ProjectCreate.projects_url, User.session, parsed_args.project) + if response.status_code == 200: + print "Project has been successfully created!" + else: + print response.text + + +class ProjectDelete(ProjectBase): + + def get_parser(self, prog_name): + parser = super(ProjectDelete, self).get_parser(prog_name) + parser.add_argument('-u', type=str, help='Username for authentication') + parser.add_argument('-p', type=str, help='Password for authentication') + parser.add_argument('-name', type=str, required=True, help='Delete project by name') + return parser + + def take_action(self, parsed_args): + httpClient = HTTPClient.get_Instance() + if(parsed_args.u and parsed_args.p): + response = AuthHandler.authenticate(parsed_args.u, parsed_args.p) + if "login" in response.text: + print "Authentication has failed. Please check your username and password." + return + projects = httpClient.delete(ProjectDelete.projects_url + "/" + parsed_args.name, User.session) + print projects + + +class ProjectPut(ProjectBase): + + def get_parser(self, prog_name): + parser = super(ProjectPut, self).get_parser(prog_name) + parser.add_argument('-u', type=str, help='Username for authentication') + parser.add_argument('-p', type=str, help='Password for authentication') + parser.add_argument('-name', type=str, required=True, help='Update project by name') + parser.add_argument('project', type=json.loads, help='Project Update request format :{ "name": (required)"", "description": (optional)""}') + return parser + + def take_action(self, parsed_args): + httpClient = HTTPClient.get_Instance() + if(parsed_args.u and parsed_args.p): + response = AuthHandler.authenticate(parsed_args.u, parsed_args.p) + if "login" in response.text: + print "Authentication has failed. Please check your username and password." + return + projects = httpClient.put(ProjectPut.projects_url + "/" + parsed_args.name, User.session, parsed_args.project) + print projects |