Search in sources :

Example 1 with ApiException

use of com.google.api.ads.admanager.axis.v202108.ApiException in project hale by halestudio.

the class HaleConnectServiceImpl method createUploadFileCallback.

private ApiCallback<Feedback> createUploadFileCallback(final SettableFuture<Boolean> future, final ProgressIndicator progress, final File file, final int totalWork) {
    return new ApiCallback<Feedback>() {

        AtomicLong chunkWritten = new AtomicLong(0);

        AtomicLong bytesReported = new AtomicLong(0);

        @Override
        public void onDownloadProgress(long bytesRead, long contentLength, boolean done) {
        // not required
        }

        @Override
        public void onFailure(com.haleconnect.api.projectstore.v1.ApiException e, int statusCode, Map<String, List<String>> responseHeaders) {
            progress.end();
            future.setException(new HaleConnectException(e.getMessage(), e, statusCode, responseHeaders));
        }

        @Override
        public void onSuccess(Feedback result, int statusCode, Map<String, List<String>> responseHeaders) {
            if (result.getError()) {
                log.error(MessageFormat.format("Error uploading project file \"{0}\": {1}", file.getAbsolutePath(), result.getMessage()));
                future.set(false);
            } else {
                future.set(true);
            }
            progress.end();
        }

        @Override
        public void onUploadProgress(long bytesWritten, long contentLength, boolean done) {
            // bytesWritten contains the accumulated amount of bytes written
            if (totalWork != ProgressIndicator.UNKNOWN) {
                // Wait until at least 1 KiB was written
                long chunk = chunkWritten.get();
                chunk += bytesWritten - bytesReported.get();
                if (chunk >= 1024) {
                    long workToReport = chunk >> 10;
                    // cannot overflow, total size in KiB
                    // is guaranteed to be < Integer.MAX_VALUE
                    progress.advance(Math.toIntExact(workToReport));
                    chunk -= workToReport << 10;
                // chunkWritten now always < 1024
                }
                chunkWritten.set(chunk);
                bytesReported.set(bytesWritten);
            }
        }
    };
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) ApiCallback(com.haleconnect.api.projectstore.v1.ApiCallback) Feedback(com.haleconnect.api.projectstore.v1.model.Feedback) HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ApiException(com.haleconnect.api.user.v1.ApiException)

Example 2 with ApiException

use of com.google.api.ads.admanager.axis.v202108.ApiException in project hale by halestudio.

the class HaleConnectServiceImpl method getOrganisationInfo.

/**
 * @see eu.esdihumboldt.hale.io.haleconnect.HaleConnectService#getOrganisationInfo(java.lang.String)
 */
@Override
public HaleConnectOrganisationInfo getOrganisationInfo(String orgId) throws HaleConnectException {
    if (!this.isLoggedIn()) {
        return null;
    }
    if (!orgInfoCache.containsKey(orgId)) {
        OrganisationsApi api = UserServiceHelper.getOrganisationsApi(this, this.getSession().getToken());
        try {
            OrganisationInfo org = api.getOrganisation(orgId);
            orgInfoCache.put(org.getId(), new HaleConnectOrganisationInfo(org.getId(), org.getName()));
        } catch (ApiException e) {
            throw new HaleConnectException(e.getMessage(), e);
        }
    }
    return orgInfoCache.get(orgId);
}
Also used : HaleConnectOrganisationInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectOrganisationInfo) OrganisationInfo(com.haleconnect.api.user.v1.model.OrganisationInfo) HaleConnectOrganisationInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectOrganisationInfo) HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException) OrganisationsApi(com.haleconnect.api.user.v1.api.OrganisationsApi) ApiException(com.haleconnect.api.user.v1.ApiException)

Example 3 with ApiException

use of com.google.api.ads.admanager.axis.v202108.ApiException in project hale by halestudio.

the class HaleConnectServiceImpl method login.

/**
 * @see eu.esdihumboldt.hale.io.haleconnect.HaleConnectService#login(java.lang.String,
 *      java.lang.String)
 */
@Override
public boolean login(String username, String password) throws HaleConnectException {
    LoginApi loginApi = UserServiceHelper.getLoginApi(this);
    Credentials credentials = UserServiceHelper.buildCredentials(username, password);
    try {
        Token token = loginApi.login(credentials);
        if (token != null) {
            UsersApi usersApi = UserServiceHelper.getUsersApi(this, token.getToken());
            // First get the current user's profile to obtain the user ID
            // required to fetch the extended profile (including the user's
            // roles/organisations) in the next step
            UserInfo shortProfile = usersApi.getProfileOfCurrentUser();
            session = new HaleConnectSessionImpl(username, token.getToken(), usersApi.getProfile(shortProfile.getId()));
            notifyLoginStateChanged();
        } else {
            clearSession();
        }
    } catch (ApiException e) {
        if (e.getCode() == 401) {
            clearSession();
        } else {
            throw new HaleConnectException(e.getMessage(), e);
        }
    }
    return isLoggedIn();
}
Also used : UsersApi(com.haleconnect.api.user.v1.api.UsersApi) Token(com.haleconnect.api.user.v1.model.Token) UserInfo(com.haleconnect.api.user.v1.model.UserInfo) HaleConnectUserInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectUserInfo) HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException) LoginApi(com.haleconnect.api.user.v1.api.LoginApi) Credentials(com.haleconnect.api.user.v1.model.Credentials) ApiException(com.haleconnect.api.user.v1.ApiException)

Example 4 with ApiException

use of com.google.api.ads.admanager.axis.v202108.ApiException in project hale by halestudio.

the class HaleConnectInputSupplier method getLastModified.

/**
 * @return details on the hale connect project
 */
public Long getLastModified() {
    if (lastModified == null) {
        Owner owner = HaleConnectUrnBuilder.extractProjectOwner(getLocation());
        String projectId = HaleConnectUrnBuilder.extractProjectId(getLocation());
        final BucketsApi api = ProjectStoreHelper.getBucketsApi(basePathResolver, apiKey);
        final ApiResponse<BucketDetail> response;
        try {
            response = api.getBucketInfoWithHttpInfo(owner.getType().getJsonValue(), owner.getId(), projectId);
            lastModified = response.getData().getLastModified();
        } catch (ApiException e) {
        // Not fatal
        }
    }
    return lastModified;
}
Also used : BucketsApi(com.haleconnect.api.projectstore.v1.api.BucketsApi) BucketDetail(com.haleconnect.api.projectstore.v1.model.BucketDetail) ApiException(com.haleconnect.api.projectstore.v1.ApiException)

Example 5 with ApiException

use of com.google.api.ads.admanager.axis.v202108.ApiException in project googleads-java-lib by googleads.

the class CreateProposals method main.

public static void main(String[] args) {
    AdManagerSession session;
    try {
        // Generate a refreshable OAuth2 credential.
        Credential oAuth2Credential = new OfflineCredentials.Builder().forApi(Api.AD_MANAGER).fromFile().build().generateCredential();
        // Construct a AdManagerSession.
        session = new AdManagerSession.Builder().fromFile().withOAuth2Credential(oAuth2Credential).build();
    } catch (ConfigurationLoadException cle) {
        System.err.printf("Failed to load configuration from the %s file. Exception: %s%n", DEFAULT_CONFIGURATION_FILENAME, cle);
        return;
    } catch (ValidationException ve) {
        System.err.printf("Invalid configuration in the %s file. Exception: %s%n", DEFAULT_CONFIGURATION_FILENAME, ve);
        return;
    } catch (OAuthException oe) {
        System.err.printf("Failed to create OAuth credentials. Check OAuth settings in the %s file. " + "Exception: %s%n", DEFAULT_CONFIGURATION_FILENAME, oe);
        return;
    }
    AdManagerServices adManagerServices = new AdManagerServices();
    CreateProposalsParams params = new CreateProposalsParams();
    if (!params.parseArguments(args)) {
        // Either pass the required parameters for this example on the command line, or insert them
        // into the code here. See the parameter class definition above for descriptions.
        params.primarySalespersonId = Long.parseLong("INSERT_PRIMARY_SALESPERSON_ID_HERE");
        params.primaryTraffickerId = Long.parseLong("INSERT_PRIMARY_TRAFFICKER_ID_HERE");
        params.programmaticBuyerId = Long.parseLong("INSERT_PROGRAMMATIC_BUYER_ID_HERE");
    }
    try {
        runExample(adManagerServices, session, params.primarySalespersonId, params.primaryTraffickerId, params.programmaticBuyerId);
    } catch (ApiException apiException) {
        // ApiException is the base class for most exceptions thrown by an API request. Instances
        // of this exception have a message and a collection of ApiErrors that indicate the
        // type and underlying cause of the exception. Every exception object in the admanager.axis
        // packages will return a meaningful value from toString
        // 
        // ApiException extends RemoteException, so this catch block must appear before the
        // catch block for RemoteException.
        System.err.println("Request failed due to ApiException. Underlying ApiErrors:");
        if (apiException.getErrors() != null) {
            int i = 0;
            for (ApiError apiError : apiException.getErrors()) {
                System.err.printf("  Error %d: %s%n", i++, apiError);
            }
        }
    } catch (RemoteException re) {
        System.err.printf("Request failed unexpectedly due to RemoteException: %s%n", re);
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) ValidationException(com.google.api.ads.common.lib.exception.ValidationException) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) OAuthException(com.google.api.ads.common.lib.exception.OAuthException) ApiError(com.google.api.ads.admanager.axis.v202111.ApiError) RemoteException(java.rmi.RemoteException) AdManagerSession(com.google.api.ads.admanager.lib.client.AdManagerSession) AdManagerServices(com.google.api.ads.admanager.axis.factory.AdManagerServices) ApiException(com.google.api.ads.admanager.axis.v202111.ApiException)

Aggregations

ConfigurationLoadException (com.google.api.ads.common.lib.conf.ConfigurationLoadException)490 OAuthException (com.google.api.ads.common.lib.exception.OAuthException)490 ValidationException (com.google.api.ads.common.lib.exception.ValidationException)490 RemoteException (java.rmi.RemoteException)490 AdManagerServices (com.google.api.ads.admanager.axis.factory.AdManagerServices)487 AdManagerSession (com.google.api.ads.admanager.lib.client.AdManagerSession)487 Credential (com.google.api.client.auth.oauth2.Credential)487 StatementBuilder (com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder)233 ApiException (com.google.api.ads.admanager.axis.v202202.ApiException)164 ApiException (com.google.api.ads.admanager.axis.v202108.ApiException)163 ApiException (com.google.api.ads.admanager.axis.v202111.ApiException)163 ApiError (com.google.api.ads.admanager.axis.v202202.ApiError)163 ApiError (com.google.api.ads.admanager.axis.v202108.ApiError)162 ApiError (com.google.api.ads.admanager.axis.v202111.ApiError)162 StatementBuilder (com.google.api.ads.admanager.axis.utils.v202111.StatementBuilder)117 StatementBuilder (com.google.api.ads.admanager.axis.utils.v202202.StatementBuilder)117 IOException (java.io.IOException)59 Random (java.util.Random)25 UpdateResult (com.google.api.ads.admanager.axis.v202108.UpdateResult)18 NetworkServiceInterface (com.google.api.ads.admanager.axis.v202108.NetworkServiceInterface)15