public HttpDatastoreRpc(DatastoreOptions options) { HttpTransportOptions httpTransportOptions = (HttpTransportOptions) options.getTransportOptions(); HttpTransport transport = httpTransportOptions.getHttpTransportFactory().create(); com.google.datastore.v1.client.DatastoreOptions.Builder clientBuilder = new com.google.datastore.v1.client.DatastoreOptions.Builder() .projectId(options.getProjectId()) .initializer(getHttpRequestInitializer(options, httpTransportOptions)) .transport(transport); String normalizedHost = options.getHost() != null ? options.getHost().toLowerCase() : ""; if (isLocalHost(normalizedHost)) { clientBuilder = clientBuilder.localHost(removeScheme(normalizedHost)); } else if (!removeScheme(com.google.datastore.v1.client.DatastoreFactory.DEFAULT_HOST) .equals(removeScheme(normalizedHost)) && !normalizedHost.isEmpty()) { String fullUrl = normalizedHost; if (fullUrl.charAt(fullUrl.length() - 1) != '/') { fullUrl = fullUrl + '/'; } fullUrl = fullUrl + com.google.datastore.v1.client.DatastoreFactory.VERSION + "/projects/" + options.getProjectId(); clientBuilder = clientBuilder.projectId(null).projectEndpoint(fullUrl); } client = com.google.datastore.v1.client.DatastoreFactory.get().create(clientBuilder.build()); }
private static void setProjectEndpointFromEnv(DatastoreOptions.Builder options) { // DATASTORE_HOST is deprecated. if (System.getenv("DATASTORE_HOST") != null) { logger.warning(String.format( "Ignoring value of environment variable DATASTORE_HOST. " + "To point datastore to a host running locally, use " + "the environment variable %s.", LOCAL_HOST_ENV_VAR)); } String projectId = getProjectIdFromEnv(); if (System.getenv(URL_OVERRIDE_ENV_VAR) != null) { options.projectEndpoint(String.format("%s/projects/%s", System.getenv(URL_OVERRIDE_ENV_VAR), projectId)); return; } if (System.getenv(LOCAL_HOST_ENV_VAR) != null) { options.projectId(projectId); options.localHost(System.getenv(LOCAL_HOST_ENV_VAR)); return; } options.projectId(projectId); return; }
private static void setProjectEndpointFromEnv(DatastoreOptions.Builder options) { // DATASTORE_HOST is deprecated. if (System.getenv("DATASTORE_HOST") != null) { logger.warning(String.format( "Ignoring value of environment variable DATASTORE_HOST. " + "To point datastore to a host running locally, use " + "the environment variable %s.", LOCAL_HOST_ENV_VAR)); } String projectId = getProjectIdFromEnv(); if (System.getenv(URL_OVERRIDE_ENV_VAR) != null) { options.projectEndpoint(String.format("%s/projects/%s", System.getenv(URL_OVERRIDE_ENV_VAR), projectId)); return; } if (System.getenv(LOCAL_HOST_ENV_VAR) != null) { options.projectId(projectId); options.localHost(System.getenv(LOCAL_HOST_ENV_VAR)); return; } options.projectId(projectId); return; }
public HttpDatastoreRpc(DatastoreOptions options) { HttpTransportOptions httpTransportOptions = (HttpTransportOptions) options.getTransportOptions(); HttpTransport transport = httpTransportOptions.getHttpTransportFactory().create(); com.google.datastore.v1.client.DatastoreOptions.Builder clientBuilder = new com.google.datastore.v1.client.DatastoreOptions.Builder() .projectId(options.getProjectId()) .initializer(getHttpRequestInitializer(options, httpTransportOptions)) .transport(transport); String normalizedHost = options.getHost() != null ? options.getHost().toLowerCase() : ""; if (isLocalHost(normalizedHost)) { clientBuilder = clientBuilder.localHost(removeScheme(normalizedHost)); } else if (!removeScheme(com.google.datastore.v1.client.DatastoreFactory.DEFAULT_HOST) .equals(removeScheme(normalizedHost)) && !normalizedHost.isEmpty()) { String fullUrl = normalizedHost; if (fullUrl.charAt(fullUrl.length() - 1) != '/') { fullUrl = fullUrl + '/'; } fullUrl = fullUrl + com.google.datastore.v1.client.DatastoreFactory.VERSION + "/projects/" + options.getProjectId(); clientBuilder = clientBuilder.projectId(null).projectEndpoint(fullUrl); } client = com.google.datastore.v1.client.DatastoreFactory.get().create(clientBuilder.build()); }
/** * Builds a Cloud Datastore client for the given pipeline options, project and an optional * locahost. */ public Datastore getDatastore( PipelineOptions pipelineOptions, String projectId, @Nullable String localhost) { Credentials credential = pipelineOptions.as(GcpOptions.class).getGcpCredential(); HttpRequestInitializer initializer; if (credential != null) { initializer = new ChainingHttpRequestInitializer( new HttpCredentialsAdapter(credential), new RetryHttpRequestInitializer()); } else { initializer = new RetryHttpRequestInitializer(); } DatastoreOptions.Builder builder = new DatastoreOptions.Builder().projectId(projectId).initializer(initializer); if (localhost != null) { builder.localHost(localhost); } else { builder.host("batch-datastore.googleapis.com"); } return DatastoreFactory.get().create(builder.build()); }