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

Source Code for Module fliesclient.flieslib.rest.client

 1  #  
 2  # Flies Python Client 
 3  # 
 4  # Copyright (c) 2010 Jian Ni <jni@redhat.com> 
 5  # Copyright (c) 2010 Red Hat, Inc. 
 6  # 
 7  # This library is free software; you can redistribute it and/or 
 8  # modify it under the terms of the GNU Lesser General Public 
 9  # License as published by the Free Software Foundation; either 
10  # version 2 of the License, or (at your option) any later version. 
11  # 
12  # This library is distributed in the hope that it will be useful, 
13  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
15  # GNU Lesser General Public License for more details. 
16  # 
17  # You should have received a copy of the GNU Lesser General Public 
18  # License along with this program; if not, write to the 
19  # Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
20  # Boston, MA  02111-1307  USA 
21   
22  __all__ = ( 
23          "RestClient", 
24      )   
25   
26  import urlparse 
27  import urllib 
28  import base64 
29  import warnings  
30  import socket 
31  import sys 
32   
33  with warnings.catch_warnings(): 
34       warnings.filterwarnings("ignore",category=DeprecationWarning) 
35       import httplib2 
36        
37 -class RestClient():
38 - def __init__(self, base_url):
39 self.base_url = base_url 40 self.url = urlparse.urlparse(base_url)
41
42 - def request_get(self, resource, args = None, body = None, headers = {}):
43 return self.request(resource, "get", args, body, headers)
44
45 - def request_post(self, resource, args = None, body = None, headers = {}):
46 return self.request(resource, "post", args, body, headers)
47
48 - def request_put(self, resource, args = None, body = None, headers = {}):
49 return self.request(resource, "put", args, body, headers)
50
51 - def request(self, resource, method = "get", args = None, body = None, headers = {}):
52 path = resource 53 headers['Accept'] = 'application/json' 54 http = httplib2.Http(".cache") 55 56 if args: 57 if method == "put" or method == "post": 58 headers['Content-Type'] = 'application/json' 59 body = args 60 try: 61 response, content = http.request("%s%s" % (self.base_url, resource), method.upper(), body=body, headers=headers) 62 return (response, content.decode("UTF-8")) 63 except httplib2.ServerNotFoundError as e: 64 print "Error:%s, Maybe the flies sever is down?"%e 65 sys.exit(2) 66 except httplib2.HttpLib2Error as e: 67 print "Error:%s"%e 68 sys.exit(2)
69