lib/fanforce/api/_main.rb in fanforce-api-0.14.7 vs lib/fanforce/api/_main.rb in fanforce-api-0.14.8
- old
+ new
@@ -38,22 +38,32 @@
RestClient.get(url(path, req_params), :accept => :json) do |response, request, result, &block|
Fanforce::API::Response.process(response, request, complete_url(path), req_params)
end
end
- def post(path, req_params={})
+ def post(path, req_params={}, retries=0)
url = complete_url(path)
- req_params = apply_auth(req_params)
+ req_params = apply_auth(req_params) unless retries > 0
RestClient.post(url, MultiJson.dump(req_params), :content_type => :json, :accept => :json) do |response, request, result, &block|
- Fanforce::API::Response.process(response, request, url, req_params)
+ if response.code == 400 and response.body == '{"error":"Invalid JSON"}' and retries <= 2
+ puts "retried #{path} #{retries} times"
+ post(path, req_params, retries+1)
+ else
+ Fanforce::API::Response.process(response, request, url, req_params)
+ end
end
end
- def put(path, req_params={})
+ def put(path, req_params={}, retries=0)
url = complete_url(path)
- req_params = apply_auth(req_params)
+ req_params = apply_auth(req_params) unless retries > 0
RestClient.put(url, MultiJson.dump(req_params), :content_type => :json, :accept => :json) do |response, request, result, &block|
- Fanforce::API::Response.process(response, request, url, req_params)
+ if response.code == 400 and response.body == '{"error":"Invalid JSON"}' and retries <= 2
+ puts "retried #{path} #{retries} times"
+ post(path, req_params, retries+1)
+ else
+ Fanforce::API::Response.process(response, request, url, req_params)
+ end
end
end
def delete(path, req_params={})
url = complete_url(path)