Package fliesclient :: Package flieslib :: Module projectservice
[hide private]
[frames] | no frames]

Source Code for Module fliesclient.flieslib.projectservice

  1  #vim:set et sts=4 sw=4:  
  2  #  
  3  # Flies Python Client 
  4  # 
  5  # Copyright (c) 2010 Jian Ni <jni@redhat.com> 
  6  # Copyright (c) 2010 Red Hat, Inc. 
  7  # 
  8  # This library is free software; you can redistribute it and/or 
  9  # modify it under the terms of the GNU Lesser General Public 
 10  # License as published by the Free Software Foundation; either 
 11  # version 2 of the License, or (at your option) any later version. 
 12  # 
 13  # This library is distributed in the hope that it will be useful, 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  # GNU Lesser General Public License for more details. 
 17  # 
 18  # You should have received a copy of the GNU Lesser General Public 
 19  # License along with this program; if not, write to the 
 20  # Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
 21  # Boston, MA  02111-1307  USA 
 22   
 23  __all__ = ( 
 24          "ProjectService",  
 25     ) 
 26   
 27  import sys 
 28  import json 
 29  from rest.client import RestClient 
 30  from project import Project 
 31  from project import Iteration 
 32  from error import * 
 33   
34 -class ProjectService:
35 """ 36 Provides services to interact with Project, handle operaions of list, create and retrieve Project Resources 37 """
38 - def __init__(self, base_url, usrname, apikey):
39 self.restclient = RestClient(base_url) 40 self.iterations = IterationService(base_url, usrname, apikey) 41 self.username = usrname 42 self.apikey = apikey
43
44 - def list(self):
45 """ 46 List the Project Resources on the Flies server 47 @return: list of Project object 48 """ 49 res, content = self.restclient.request_get('/projects') 50 51 if res['status'] == '200': 52 projects = [] 53 projects_json = json.loads(content) 54 55 for p in projects_json: 56 projects.append(Project(json = p)) 57 return projects
58
59 - def get(self, projectid):
60 """ 61 Retrieve a specified Project Resource on Flies server 62 @param projectid: Id of Project Resource 63 @return: Project object 64 @raise NoSuchProjectException: 65 """ 66 res, content = self.restclient.request_get('/projects/p/%s'%projectid) 67 if res['status'] == '200': 68 return Project(json = json.loads(content), iterations = self.iterations) 69 elif res['status'] == '404': 70 raise NoSuchProjectException('Error 404', 'No Such project')
71
72 - def create(self, project):
73 """ 74 Create a Project Resource on Flies Server 75 @param project: Project object 76 @return: Success if status of response is 201 77 @raise ProjectExistException: 78 @raise NoSuchProjectException: 79 @raise UnAuthorizedException: 80 @raise BadRequestException: 81 """ 82 exist = False 83 headers = {} 84 headers['X-Auth-User'] = self.username 85 headers['X-Auth-Token'] = self.apikey 86 try: 87 self.get(project.id) 88 raise ProjectExistException('Status 200', 'The project is already exist') 89 except NoSuchProjectException: 90 exist = False 91 92 body ='''{"name":"%s","id":"%s","description":"%s","type":"IterationProject"}'''%(project.name,project.id,project.desc) 93 res, content = self.restclient.request_put('/projects/p/%s'%project.id, args=body, headers=headers) 94 95 if res['status'] == '201': 96 return "Success" 97 elif rest['status'] == '200': 98 raise ProjectExistException('Status 200', 'The project is already exist') 99 elif res['status'] == '404': 100 raise NoSuchProjectException('Error 404', 'No Such project') 101 elif res['status'] == '401': 102 raise UnAuthorizedException('Error 401', 'Un Authorized Operation') 103 elif res['status'] == '400': 104 raise BadRequestException('Error 400', 'Bad Request')
105
106 - def delete(self):
107 pass
108
109 - def status(self):
110 pass
111
112 -class IterationService:
113 """ 114 Provides services to interact with Project iteration, handle operaions of list, create and retrieve iteration Resources 115 """
116 - def __init__(self, base_url, usrname = None, apikey = None):
117 self.restclient = RestClient(base_url) 118 self.username = usrname 119 self.apikey = apikey
120
121 - def get(self, projectid, iterationid):
122 """ 123 Retrieve a specified Iteration Resource on Flies server 124 @param projectid: Id of Project Resource 125 @param iterationid: Id of Iteration Resource 126 @return: Iteration object 127 @raise NoSuchProjectException: 128 """ 129 res, content = self.restclient.request_get('/projects/p/%s/iterations/i/%s'%(projectid,iterationid)) 130 if res['status'] == '200': 131 return Iteration(json.loads(content)) 132 elif res['status'] == '404': 133 raise NoSuchProjectException('Error 404', 'No Such project')
134
135 - def create(self, projectid, iteration):
136 """ 137 Create a Iteration Resource on Flies Server 138 @param projectid: Id of Project Resource 139 @param iteration: Iteration object 140 @return: Success if status of response is 201 141 @raise ProjectExistException: 142 @raise NoSuchProjectException: 143 @raise UnAuthorizedException: 144 @raise BadRequestException: 145 """ 146 headers = {} 147 headers['X-Auth-User'] = self.username 148 headers['X-Auth-Token'] = self.apikey 149 150 body = '''{"name":"%s","id":"%s","description":"%s"}'''%(iteration.name, iteration.id, iteration.desc) 151 res, content = self.restclient.request_put('/projects/p/%s/iterations/i/%s'%(projectid,iteration.id), args=body, headers=headers) 152 if res['status'] == '201': 153 return "Success" 154 elif res['status'] == '200': 155 raise ProjectExistException('Status 200', 'The project is already exist') 156 elif res['status'] == '404': 157 raise NoSuchProjectException('Error 404', 'No Such project') 158 elif res['status'] == '401': 159 raise UnAuthorizedException('Error 401', 'UnAuthorized Operation')
160
161 - def delete(self):
162 pass
163