Class: Projects::Util::ZohoHTTPClient
- Inherits:
-
Object
- Object
- Projects::Util::ZohoHTTPClient
- Includes:
- Pexception
- Defined in:
- lib/projects/util/ZohoHTTPClient.rb
Overview
-
ZohoHTTPClient is used to make a HTTP request execution for GET, POST, PUT and DELETE requests.
Class Method Summary collapse
- .delete(url, queryMap) ⇒ Object
- .get(url, queryMap) ⇒ Object
-
.getHeader ⇒ Object
Returns.
-
.getHttpUri(url) ⇒ Object
-
Returns the http and uri objects for making request and response.
-
-
.getResult(response) ⇒ Object
-
Evaluates the response.
-
-
.getUrl(url, queryMap) ⇒ Object
-
Returns the url string for making the request.
-
-
.post(url, queryMap, paramMap, fileBody = nil) ⇒ Object
-
Make a POST request and create a resource for the given URL and a MultiPart form data.
-
-
.throwsException(message) ⇒ Object
-
Raises ProjectsException when this method is called.
-
Class Method Details
.delete(url, queryMap) ⇒ Object
196 197 198 199 200 201 202 |
# File 'lib/projects/util/ZohoHTTPClient.rb', line 196 def self.delete(url,queryMap) url = getUrl(url, queryMap) http,uri= getHttpUri(url) request = Net::HTTP::Delete.new(uri.request_uri, initheader = getHeader) response = http.request(request) getResult(response) end |
.get(url, queryMap) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/projects/util/ZohoHTTPClient.rb', line 113 def self.get(url,queryMap) url = getUrl(url, queryMap) http,uri= getHttpUri(url) request = Net::HTTP::Get.new(uri.request_uri, initheader = getHeader) response = http.request(request) getResult(response) end |
.getHeader ⇒ Object
Returns
-
header for the http request
75 76 77 78 79 80 81 82 |
# File 'lib/projects/util/ZohoHTTPClient.rb', line 75 def self.getHeader header = Hash.new header["Accept"] = "application/json" header["Content-Type"] = "application/x-www-form-urlencoded" header["Accept-Charset"] = "UTF-8" header["User-Agent"] = "ZohoProjects-Ruby-Wrappers/0.0.6" return header end |
.getHttpUri(url) ⇒ Object
-
Returns the http and uri objects for making request and response
Parameters
- http
-
Object of NET::HTTP
-
- uri
-
Object of URI which parses the url of the request
-
48 49 50 51 52 53 54 55 56 |
# File 'lib/projects/util/ZohoHTTPClient.rb', line 48 def self.getHttpUri(url) uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE http.open_timeout = 60 http.read_timeout = 80 return [http, uri] end |
.getResult(response) ⇒ Object
-
Evaluates the response
Parameters
- response
-
The response of the HTTP request
-
-
Returns response body is success or terminates the program
92 93 94 95 96 97 98 99 100 |
# File 'lib/projects/util/ZohoHTTPClient.rb', line 92 def self.getResult(response) if response.code.to_i == 200 || response.code.to_i == 201 return response.body elsif response.code.to_i == 204 abort("No Result for the specified request") else throwsException(response.body) end end |
.getUrl(url, queryMap) ⇒ Object
-
Returns the url string for making the request.
Parameters
- url
-
Service URL passed by the user.
-
- queryMap
-
This queryMap contains the query string parameters in the form of key, value pair.
-
Returns
-
Url in String.
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/projects/util/ZohoHTTPClient.rb', line 26 def self.getUrl(url,queryMap) url = url + '?' i = 0 queryMap.each do |key, value| if i == 0 url = url+key+'='+String(value) i=i+1 else url = url+'&'+key+'='+String(value) end end return url end |
.post(url, queryMap, paramMap, fileBody = nil) ⇒ Object
-
Make a POST request and create a resource for the given URL and a MultiPart form data.
-
Calls getResult to return the JSON response String.
Parameters
- url
-
Service URL passed by the user.
-
- queryMap
-
This queryMap contains the query string parameters in the form of key, value pair.
-
- requestBody
-
This requestBody contains the form data for the POST request.
-
- fileBody
-
This fileBody contains the attachment files for the POST request.
-
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/projects/util/ZohoHTTPClient.rb', line 135 def self.post(url, queryMap, paramMap, fileBody=nil) if fileBody == nil url = getUrl(url, queryMap) http,uri= getHttpUri(url) request = Net::HTTP::Post.new(uri.request_uri, initheader = getHeader) request.set_form_data(paramMap) response = http.request(request) getResult(response) else begin url = getUrl(url, queryMap) @BOUNDARY = "AaB03x" header = Hash.new header["Accept"] = "application/json" header["Content-Type"] = "multipart/form-data; boundary="+ @BOUNDARY header["Accept-Charset"] = "UTF-8" header["User-Agent"] = "ZohoBooks-Java-Wrappers/1.0" http,uri= getHttpUri(url) request = Net::HTTP::Post.new(uri.request_uri, initheader = header) post_body = [] paramMap.each do |key, val| post_body << "--#{@BOUNDARY}\r\n" post_body << "Content-Disposition: form-data; name=\"#{key}\"\r\n\r\n#{val}\r\n" end fileBody.each do |key, val| for i in 0...val.length post_body << "--#{@BOUNDARY}\r\n" post_body << "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{File.basename(val[i].path)}\"\r\n" post_body << "Content-Type: application/octet-stream\r\n" post_body << "\r\n" post_body << File.read(val[i].path) post_body << "\r\n" end end post_body << "--#{@BOUNDARY}--\r\n" request.body = post_body.join response = http.request(request) getResult(response) ensure fileBody.each do |key, val| for i in 0...val.length if !val[i].closed? val[i].close end end end end end end |
.throwsException(message) ⇒ Object
-
Raises ProjectsException when this method is called
Parameters
- message
-
Response message of the request
-
64 65 66 67 68 |
# File 'lib/projects/util/ZohoHTTPClient.rb', line 64 def self.throwsException() error_json = JSON.parse error_array = error_json["error"] raise ProjectsException.new(error_array["code"],error_array["message"]) end |