refactor url proces 37/53237/3
authorSerenaFeng <feng.xiaowei@zte.com.cn>
Wed, 7 Mar 2018 12:14:45 +0000 (20:14 +0800)
committerSerenaFeng <feng.xiaowei@zte.com.cn>
Thu, 8 Mar 2018 02:33:25 +0000 (10:33 +0800)
Change-Id: I8a253cc921875b810b954abafaad84f61f559ef5
Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
testapi/testapi-client/testapiclient/command.py
testapi/testapi-client/testapiclient/identity.py
testapi/testapi-client/testapiclient/pods.py
testapi/testapi-client/testapiclient/projects.py
testapi/testapi-client/testapiclient/url_parse.py [new file with mode: 0644]

index 7dba312..2864f9a 100644 (file)
@@ -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):
index 9101090..03b3ac5 100644 (file)
@@ -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
index 0e58324..c49f254 100644 (file)
@@ -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))
index 0e52b09..ad42293 100644 (file)
@@ -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 (file)
index 0000000..08f7a63
--- /dev/null
@@ -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)