Skip to content

Instantly share code, notes, and snippets.

@abrahaj
Created October 22, 2014 13:53
Show Gist options
  • Save abrahaj/a8a3a5141e279d53daad to your computer and use it in GitHub Desktop.
Save abrahaj/a8a3a5141e279d53daad to your computer and use it in GitHub Desktop.
import al.norm.ddb.exceptions.ServiceUnavailableException
import grails.plugins.rest.client.RestBuilder
import org.springframework.http.HttpStatus
class ApiconsumerService {
def transactional=false
def grailsApplication
/**
* TODO improve the exception throwing
* Utilizes the grails.plugins.rest.client.RestBuilder
* Wrapped in this method due to error handling mainly
* @param url
* @return RestResponse
*/
def doGet(String url){
def rest = createRestBuilder()
log.info "String URL "+ url+createAuthString()
def response = rest.get(url+createAuthString())
if (response.getStatusCode()!=HttpStatus.OK) {
response.exceptionMessage= response.getMessage()
log.error "Failed retrieving a resource successfully"
throw new ServiceUnavailableException([status: response.getStatusCode(),
message: "Invoking service failed"],
"Invoking service failed")
}
return response
}
private createAuthString(){
return "&oauth_consumer_key="+grailsApplication.config.ddb.backend.apikey
}
private createRestBuilder() {
log.info "proxy use" + grailsApplication.config.proxy.use
if (grailsApplication.config.proxy.use==true) {
log.info "Found Proxy"
return new RestBuilder(connectTimeout:1000, readTimeout:20000, proxy:['proxy.fiz-karlsruhe.de':grailsApplication.config.proxy.port])
}
return new RestBuilder()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment