source: ndgCommon/trunk/ndg/common/src/clients/http/generichttpclient.py @ 5445

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/ndgCommon/trunk/ndg/common/src/clients/http/generichttpclient.py@5883
Revision 5445, 2.7 KB checked in by cbyrom, 11 years ago (diff)

Extend generichttpclient to allow http method to be set on request +
update vocabserverclient to do a PUT request following a POST one
should an update already have been ran on a particular term - to allow
this update to be overwritten.

Line 
1'''
2 Simple client to get resources over http
3 
4 @author: B Lawrence?, C Byrom, Tessella, Feb 09
5'''
6from xml.etree import ElementTree as ET
7import urllib2, logging, socket
8from ndg.common.src.lib.utilities import httpify
9           
10VALID_HTTP_METHODS = ['POST', 'PUT', 'GET', 'DELETE']           
11class GenericHTTPClient(object):
12    '''
13    Provides a generic HTTP request class
14    '''
15    def __init__(self, proxyServer=None):
16        '''
17        Constructor for client
18        @keyword proxyServer: a string representing a proxy server url to use
19        when opening an http request
20        '''
21        logging.debug("Instantiating GenericHTTPClient")
22        if proxyServer:
23            logging.debug("- using proxy server, '%s'" %proxyServer)
24            proxy = httpify(proxyServer)
25            proxyHandler = urllib2.ProxyHandler({'http':proxy})
26        else:
27            proxyHandler = urllib2.ProxyHandler({})
28       
29        self.opener = urllib2.build_opener(proxyHandler)
30        logging.debug("GenericHTTPClient instantiated")
31
32       
33    def readURL(self, url, payload = None, headersDict = None, httpMethod = 'GET'):
34        '''
35        Retrieve contents of url - or raise IOError if a problem is found
36        @param url: url to open and retrive data from
37        @keyword payload: data to POST in the request
38        @keyword headersDict: dict with headers info to use in the request
39        @keyword httpMethod: method to use with http - either GET, PUT, POST or DELETE. 
40        Default = 'GET'
41        @raise IOError if problem experienced whilst opening url
42        @return contents of url
43        '''
44        logging.info("Getting data from url: %s" %url)
45        url = httpify(url)
46        inputs = {}
47        if payload:
48            inputs['data'] = payload
49           
50        if headersDict:
51            inputs['headers'] = headersDict
52           
53        request=urllib2.Request(url, **inputs)
54       
55        if httpMethod not in VALID_HTTP_METHODS:
56            raise ValueError("Unrecognised http method, '%s'" %httpMethod)
57           
58        request.get_method = lambda: httpMethod
59       
60        response='Cannot obtain remote file: '
61        try:
62            # NB, if the connection is invalid, it will fail at this point
63            f = self.opener.open(request)
64            return f.read()
65       
66        except urllib2.URLError,e:
67            if hasattr(e,'reason'):
68                response += 'No access to server [%s]'%e.reason
69            else:
70                response += str(e)
71        except socket.error:
72            response += 'Network Socket problem'
73        except Exception,e:
74            response += '[%s]'%str(e)
75       
76        logging.error(response)
77        raise IOError(response) 
Note: See TracBrowser for help on using the repository browser.