ApiController.groovy
A Controller class written in Grails framework that authorizes API callers and fulfill API requests enabled by 3Scale. It is this class written in Groovy that serves all the API request outlined on the left. You may find the code snippet in Groovy useful when integrating with API Proxy - 3Scale. ApiController.groovy: package hko.dealnews.controller import java.text.SimpleDateFormat import java.util.List; import net.threescale.api.v2.* import hko.dealnews.helper.ThreeScaleApi import hko.dealnews.domain.* import hko.dealnews.util import grails.converters.JSON import groovy.json.* /** * To add an API method to this Controller, please also add a mapping to UrlMappings.groovy * * Sample code please see: https://github.com/3scale/3scale_ws_api_for_java/blob/master/src/test/java/net/threescale/api/v2/Example.java * * @author hko * */ class ApiController { private static Api2 apiServer = ThreeScaleApi.getInstance() private String clientAppId private clientAppKey long authenticatedIn // millisecond def dealService def beforeInterceptor = [action: this.&authorizeBy3Scale] def afterInterceptor = [action: this.&reportTo3Scale] //////////// // private helper functions borrowed from https://github.com/3scale/3scale_ws_api_for_java/blob/master/src/test/java/net/threescale/api/v2/Example.java private String nowTimeStamp(Date timestamp) { SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"); return dateFormatter.format(timestamp); } private int maxDailyHits(AuthorizeResponse response) { ApiUsageMetric metric = findMetricForHitsPerDay(response); return (metric != null) ? Integer.parseInt(metric.getMaxValue()) : 0; } private int currentDailyHits(AuthorizeResponse response) { ApiUsageMetric metric = findMetricForHitsPerDay(response); return (metric != null) ? Integer.parseInt(metric.getCurrentValue()) : 0; } private ApiUsageMetric findMetricForHitsPerDay(AuthorizeResponse response) { return findMetricForPeriod(response.getUsageReports(), "hits", "day"); } // Find a specific metric/period usage metric private ApiUsageMetric findMetricForPeriod(ArrayList
|