summaryrefslogtreecommitdiffstats
path: root/testapi
diff options
context:
space:
mode:
authorSerenaFeng <feng.xiaowei@zte.com.cn>2018-03-07 20:14:45 +0800
committerSerenaFeng <feng.xiaowei@zte.com.cn>2018-03-08 10:33:25 +0800
commitf3a5531761a38cf40d0469209145394b31af2088 (patch)
treeea52fb52dc8bcf2479cbb054ebfe50a40ef57403 /testapi
parent8d66710a9b919fd7c42944ced79b1e25bc6c5482 (diff)
refactor url proces
Change-Id: I8a253cc921875b810b954abafaad84f61f559ef5 Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
Diffstat (limited to 'testapi')
-rw-r--r--testapi/testapi-client/testapiclient/command.py9
-rw-r--r--testapi/testapi-client/testapiclient/identity.py13
-rw-r--r--testapi/testapi-client/testapiclient/pods.py23
-rw-r--r--testapi/testapi-client/testapiclient/projects.py30
-rw-r--r--testapi/testapi-client/testapiclient/url_parse.py22
5 files changed, 66 insertions, 31 deletions
diff --git a/testapi/testapi-client/testapiclient/command.py b/testapi/testapi-client/testapiclient/command.py
index 7dba312..2864f9a 100644
--- a/testapi/testapi-client/testapiclient/command.py
+++ b/testapi/testapi-client/testapiclient/command.py
@@ -1,4 +1,5 @@
from cliff import command
+from testapiclient import url_parse
class Command(command.Command):
@@ -15,7 +16,13 @@ class Command(command.Command):
class Lister(command.Command):
- pass
+
+ @staticmethod
+ def filter_by_name(url, parsed_args):
+ def query_url():
+ return url_parse.query_join(url, name=parsed_args.name)
+
+ return query_url() if parsed_args.name else url
class ShowOne(command.Command):
diff --git a/testapi/testapi-client/testapiclient/identity.py b/testapi/testapi-client/testapiclient/identity.py
index 9101090..03b3ac5 100644
--- a/testapi/testapi-client/testapiclient/identity.py
+++ b/testapi/testapi-client/testapiclient/identity.py
@@ -8,11 +8,14 @@ from testapiclient import user
def _authenticate(username, password):
session = requests.Session()
- hostname = '{}{}{}'.format(
- os.environ.get('testapi_cas_auth_url'),
- urllib.quote(os.environ.get('testapi_url')),
- os.environ.get('testapi_cas_signin_return'))
- data = {'name': username, 'pass': password, 'form_id': 'user_login'}
+ hostname = '{}{}{}'.format(os.environ.get('testapi_cas_auth_url'),
+ urllib.quote(os.environ.get('testapi_url')),
+ os.environ.get('testapi_cas_signin_return'))
+ data = {
+ 'name': username,
+ 'pass': password,
+ 'form_id': 'user_login'
+ }
response = session.post(hostname, data)
user.User.session = session
return response
diff --git a/testapi/testapi-client/testapiclient/pods.py b/testapi/testapi-client/testapiclient/pods.py
index 0e58324..c49f254 100644
--- a/testapi/testapi-client/testapiclient/pods.py
+++ b/testapi/testapi-client/testapiclient/pods.py
@@ -1,11 +1,17 @@
import json
-import os
from testapiclient import command
from testapiclient import http_client
from testapiclient import identity
+from testapiclient import url_parse
-PODS_URL = os.environ.get('testapi_url') + "/pods"
+
+def pods_url():
+ return url_parse.resource_join('pods')
+
+
+def pod_url(parsed_args):
+ return url_parse.path_join(pods_url(), parsed_args.name)
class PodGet(command.Lister):
@@ -19,10 +25,8 @@ class PodGet(command.Lister):
return parser
def take_action(self, parsed_args):
- url = PODS_URL
- if(parsed_args.name):
- url = PODS_URL + "?name=" + parsed_args.name
- pods = http_client.get(url)
+ pods = http_client.get(self.filter_by_name(pods_url(),
+ parsed_args))
print pods
@@ -38,7 +42,7 @@ class PodGetOne(command.ShowOne):
return parser
def take_action(self, parsed_args):
- pods = http_client.get(PODS_URL + "/" + parsed_args.name)
+ pods = http_client.get(pod_url(parsed_args))
print pods
@@ -58,8 +62,7 @@ class PodCreate(command.Command):
@identity.authenticate
def take_action(self, parsed_args):
- response = http_client.post(PODS_URL,
- parsed_args.pod)
+ response = http_client.post(pods_url(), parsed_args.pod)
if response.status_code == 200:
print "Pod has been successfully created!"
else:
@@ -79,4 +82,4 @@ class PodDelete(command.Command):
@identity.authenticate
def take_action(self, parsed_args):
- print http_client.delete(PODS_URL + "/" + parsed_args.name)
+ print http_client.delete(pod_url(parsed_args))
diff --git a/testapi/testapi-client/testapiclient/projects.py b/testapi/testapi-client/testapiclient/projects.py
index 0e52b09..ad42293 100644
--- a/testapi/testapi-client/testapiclient/projects.py
+++ b/testapi/testapi-client/testapiclient/projects.py
@@ -1,11 +1,17 @@
import json
-import os
from testapiclient import command
from testapiclient import http_client
from testapiclient import identity
+from testapiclient import url_parse
-PROJECTS_URL = os.environ.get('testapi_url') + "/projects"
+
+def projects_url():
+ url_parse.resource_join('projects')
+
+
+def project_url(parsed_args):
+ return url_parse.path_join(projects_url(), parsed_args.name)
class ProjectGet(command.Lister):
@@ -18,10 +24,8 @@ class ProjectGet(command.Lister):
return parser
def take_action(self, parsed_args):
- url = PROJECTS_URL
- if parsed_args.name:
- url = url + "?name=" + parsed_args.name
- projects = http_client.get(url)
+ projects = http_client.get(self.filter_name(projects_url(),
+ parsed_args))
print projects
@@ -36,8 +40,7 @@ class ProjectGetOne(command.ShowOne):
return parser
def take_action(self, parsed_args):
- url = PROJECTS_URL + "/" + parsed_args.name
- project = http_client.get(url)
+ project = http_client.get(project_url(parsed_args))
print project
@@ -54,8 +57,7 @@ class ProjectCreate(command.Command):
@identity.authenticate
def take_action(self, parsed_args):
- response = http_client.post(ProjectCreate.projects_url,
- parsed_args.project)
+ response = http_client.post(projects_url(), parsed_args.project)
if response.status_code == 200:
print "Project has been successfully created!"
else:
@@ -74,8 +76,7 @@ class ProjectDelete(command.Command):
@identity.authenticate
def take_action(self, parsed_args):
- projects = http_client.delete(
- PROJECTS_URL + "/" + parsed_args.name)
+ projects = http_client.delete(project_url(parsed_args))
print projects
@@ -96,7 +97,6 @@ class ProjectPut(command.Command):
@identity.authenticate
def take_action(self, parsed_args):
- projects = http_client.put(
- PROJECTS_URL + "/" + parsed_args.name,
- parsed_args.project)
+ projects = http_client.put(project_url(parsed_args),
+ parsed_args.project)
print projects
diff --git a/testapi/testapi-client/testapiclient/url_parse.py b/testapi/testapi-client/testapiclient/url_parse.py
new file mode 100644
index 0000000..08f7a63
--- /dev/null
+++ b/testapi/testapi-client/testapiclient/url_parse.py
@@ -0,0 +1,22 @@
+import os
+
+from six.moves.urllib import parse
+
+
+def path_join(base, *urls):
+ def _path_join(base, url):
+ if not base.endswith('/'):
+ base += '/'
+ return parse.urljoin(base, url)
+
+ urls = (base,) + urls
+ return reduce(_path_join, urls)
+
+
+def query_join(base, **queries):
+ return base + '?' + parse.urlencode(queries)
+
+
+def resource_join(url):
+ testapi_url = os.environ.get('testapi_url')
+ return path_join(testapi_url, url)