private ApiContext getApiContext() throws Exception {
        if (this.apiContext == null) {
            this.apiContext = new ApiContext();
            ApiCredential cred = apiContext.getApiCredential();
            cred.seteBayToken(auth);
            apiContext.setApiServerUrl(host);
            apiContext.setSite(SiteCodeType.GERMANY);
            executor = Executors.newFixedThreadPool(4);
            try {
                getApiCallsAvailable();
            } catch (OutOfCallsException e) {
                this.end();
                throw e;
            }
        }
        return this.apiContext;
} 

public void end() {
        if (executor != null) {
            executor.shutdown();
        }
		//Close connections
}
 
public long getApiCallsAvailable() throws Exception {
        GetApiAccessRulesCall gc = new GetApiAccessRulesCall(getApiContext());
        gc.getApiAccessRules();
        ApiAccessRuleType a = gc.getReturnedApiAccessRules()[0];
        if (a.getDailyUsage() > a.getDailyHardLimit()) {
            throw new OutOfCallsException();
        }
        return a.getDailyHardLimit() - a.getDailyUsage();
}
