Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding storage api #43

Merged
merged 49 commits into from
May 7, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
0613f53
storage work in progress
aozarov Mar 2, 2015
125d6e9
wip
aozarov Mar 3, 2015
40e9739
wip
aozarov Mar 4, 2015
79215c4
wip
aozarov Mar 18, 2015
d15a682
work in progress
aozarov Mar 20, 2015
1188ac6
work in progress
aozarov Apr 1, 2015
c9611f2
work in progress
aozarov Apr 1, 2015
43d0761
work in progress
aozarov Apr 2, 2015
7dcee4b
wip
aozarov Apr 3, 2015
59f0d85
work in progress
aozarov Apr 9, 2015
a7b661a
Merge remote-tracking branch 'upstream/master'
aozarov Apr 9, 2015
126b043
work in progress
aozarov Apr 9, 2015
4f65b2d
work in progress
aozarov Apr 10, 2015
a3cf907
work in progress
aozarov Apr 10, 2015
2cdc4e3
work in progress
aozarov Apr 11, 2015
24f8a0b
work in progress
aozarov Apr 14, 2015
4836863
work in progress
aozarov Apr 14, 2015
fcb3dcd
work in progress
aozarov Apr 14, 2015
d3f0356
work in progress
aozarov Apr 14, 2015
3171d8c
work in progress
aozarov Apr 16, 2015
f88107c
work in progress
aozarov Apr 18, 2015
8ef30c8
work in progress
aozarov Apr 21, 2015
89a5024
work in progress
aozarov Apr 21, 2015
2841929
work in progress
aozarov Apr 22, 2015
803527d
work in progress
aozarov Apr 22, 2015
f327dad
work in progress
aozarov Apr 23, 2015
c92f2ea
make AuthCredentials serializable
aozarov Apr 24, 2015
d504256
work in progress
aozarov Apr 24, 2015
c148817
work in progress
aozarov Apr 24, 2015
09a890f
work in progress
aozarov Apr 24, 2015
a4076b7
work in progress
aozarov Apr 25, 2015
9cb77fa
work in progress
aozarov Apr 29, 2015
acff3c1
add default content type to compose
aozarov Apr 29, 2015
ad396b7
work in progress
aozarov Apr 30, 2015
6df1164
adding batch
aozarov May 1, 2015
a4ee293
work in progress
aozarov May 2, 2015
2ed5ce1
work in progress
aozarov May 2, 2015
4c2f40e
complete batch
aozarov May 4, 2015
59410ab
add multi info to example
aozarov May 4, 2015
f0920d0
work in progress
aozarov May 4, 2015
64da673
find project-id from env
aozarov May 4, 2015
e3222e5
update ini hanlding
aozarov May 4, 2015
9b751ae
implement Serializable reader
aozarov May 4, 2015
2d279f4
work in progress
aozarov May 5, 2015
4260e8e
work in progress
aozarov May 5, 2015
e830ad3
work in progress
aozarov May 6, 2015
a964351
work in progress
aozarov May 6, 2015
6d4ac35
work in progress
aozarov May 6, 2015
d2c6344
complete output writer
aozarov May 7, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
work in progress
  • Loading branch information
aozarov committed Apr 24, 2015
commit c1488178dccfcef9749f9d9ae95eed7109e39f40
23 changes: 19 additions & 4 deletions src/main/java/com/google/gcloud/ServiceOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.gcloud.spi.ServiceRpcFactory;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -34,7 +35,7 @@
import java.net.URLConnection;
import java.util.Set;

public abstract class ServiceOptions implements Serializable {
public abstract class ServiceOptions<R, O extends ServiceOptions<R, O>> implements Serializable {

private static final String DEFAULT_HOST = "https://www.googleapis.com";
private static final long serialVersionUID = 1203687993961393350L;
Expand All @@ -43,6 +44,7 @@ public abstract class ServiceOptions implements Serializable {
private final HttpTransportFactory httpTransportFactory;
private final AuthCredentials authCredentials;
private final RetryParams retryParams;
private final ServiceRpcFactory<R, O> serviceRpcFactory;

public interface HttpTransportFactory extends Serializable {
HttpTransport create();
Expand Down Expand Up @@ -72,12 +74,14 @@ public HttpTransport create() {
}
}

protected abstract static class Builder<B extends Builder<B>> {
protected abstract static class Builder<R, O extends ServiceOptions<R, O>,
B extends Builder<R, O, B>> {

private String host;
private HttpTransportFactory httpTransportFactory;
private AuthCredentials authCredentials;
private RetryParams retryParams;
private ServiceRpcFactory<R, O> serviceRpcFactory;

protected Builder() {}

Expand All @@ -86,6 +90,7 @@ protected Builder(ServiceOptions options) {
httpTransportFactory = options.httpTransportFactory;
authCredentials = options.authCredentials;
retryParams = options.retryParams;
serviceRpcFactory = options.serviceRpcFactory;
}

protected abstract ServiceOptions build();
Expand Down Expand Up @@ -114,14 +119,20 @@ public B retryParams(RetryParams retryParams) {
this.retryParams = retryParams;
return self();
}

public B serviceRpcFactory(ServiceRpcFactory<R, O> serviceRpcFactory) {
this.serviceRpcFactory = serviceRpcFactory;
return self();
}
}

protected ServiceOptions(Builder<?> builder) {
protected ServiceOptions(Builder<R, O, ?> builder) {
host = firstNonNull(builder.host, DEFAULT_HOST);
httpTransportFactory =
firstNonNull(builder.httpTransportFactory, new DefaultHttpTransportFactory());
authCredentials = firstNonNull(builder.authCredentials, defaultAuthCredentials());
retryParams = builder.retryParams;
serviceRpcFactory = builder.serviceRpcFactory;
}

private static AuthCredentials defaultAuthCredentials() {
Expand Down Expand Up @@ -204,10 +215,14 @@ public RetryParams retryParams() {
return retryParams;
}

public ServiceRpcFactory<R, O> serviceRpcFactory() {
return serviceRpcFactory;
}

public HttpRequestInitializer httpRequestInitializer() {
HttpTransport httpTransport = httpTransportFactory.create();
return authCredentials().httpRequestInitializer(httpTransport, scopes());
}

public abstract Builder<?> toBuilder();
public abstract Builder<R, O, ?> toBuilder();
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,25 @@
import java.util.Iterator;
import java.util.Set;

public class DatastoreServiceOptions extends ServiceOptions {
public class DatastoreServiceOptions extends ServiceOptions<DatastoreRpc, DatastoreServiceOptions> {

private static final long serialVersionUID = -8636602944160689193L;
private static final String DATASET_ENV_NAME = "DATASTORE_DATASET";
private static final String DATASTORE_SCOPE = "https://www.googleapis.com/auth/datastore";
private static final String USERINFO_SCOPE = "https://www.googleapis.com/auth/userinfo.email";
private static final Set<String> SCOPES = ImmutableSet.of(DATASTORE_SCOPE, USERINFO_SCOPE);

private final String dataset;
private final String namespace;
private final boolean force;
private final DatastoreRpc datastoreRpc;
private final boolean normalizeDataset;
private final boolean defaultDatastoreRpc;

public static class Builder extends ServiceOptions.Builder<Builder> {
public static class Builder extends
ServiceOptions.Builder<DatastoreRpc, DatastoreServiceOptions, Builder> {

private String dataset;
private String namespace;
private boolean force;
private DatastoreRpc datastoreRpc;
private boolean normalizeDataset = true;

private Builder() {
Expand All @@ -63,7 +63,6 @@ private Builder(DatastoreServiceOptions options) {
dataset = options.dataset;
force = options.force;
namespace = options.namespace;
datastoreRpc = options.datastoreRpc;
normalizeDataset = options.normalizeDataset;
}

Expand All @@ -73,11 +72,6 @@ public DatastoreServiceOptions build() {
return normalizeDataset ? options.normalize() : options;
}

public Builder datastoreRpc(DatastoreRpc datastoreRpc) {
this.datastoreRpc = datastoreRpc;
return this;
}

public Builder dataset(String dataset) {
this.dataset = validateDataset(dataset);
return this;
Expand Down Expand Up @@ -105,8 +99,6 @@ private DatastoreServiceOptions(Builder builder) {
namespace = builder.namespace != null ? builder.namespace : defaultNamespace();
force = builder.force;
dataset = firstNonNull(builder.dataset, defaultDataset());
datastoreRpc = firstNonNull(builder.datastoreRpc, ServiceRpcProvider.datastore(this));
defaultDatastoreRpc = builder.datastoreRpc == null;
}

private DatastoreServiceOptions normalize() {
Expand All @@ -123,7 +115,7 @@ private DatastoreServiceOptions normalize() {
.build();
requestPb.addKey(key);
try {
LookupResponse responsePb = datastoreRpc.lookup(requestPb.build());
LookupResponse responsePb = datastoreRpc().lookup(requestPb.build());
if (responsePb.getDeferredCount() > 0) {
key = responsePb.getDeferred(0);
} else {
Expand All @@ -132,9 +124,6 @@ private DatastoreServiceOptions normalize() {
key = combinedIter.next().getEntity().getKey();
}
builder.dataset(key.getPartitionId().getDatasetId());
if (defaultDatastoreRpc && !dataset.equals(builder.dataset)) {
builder.datastoreRpc(ServiceRpcProvider.datastore(builder.build()));
}
return new DatastoreServiceOptions(builder);
} catch (DatastoreRpcException e) {
throw DatastoreServiceException.translateAndThrow(e);
Expand Down Expand Up @@ -185,8 +174,11 @@ public Builder toBuilder() {
return new Builder(this);
}

public DatastoreRpc datastoreRpc() {
return datastoreRpc;
DatastoreRpc datastoreRpc() {
if (serviceRpcFactory() != null) {
return serviceRpcFactory().create(this);
}
return ServiceRpcProvider.datastore(this);
}

public static Builder builder() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/google/gcloud/spi/ServiceRpcFactory.java
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/* * Copyright 2015 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.google.gcloud.spi;import com.google.gcloud.ServiceOptions;public interface ServiceRpcFactory<S, O extends ServiceOptions> { S create(O options);}
/* * Copyright 2015 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.google.gcloud.spi;import com.google.gcloud.ServiceOptions;import java.io.Serializable;public interface ServiceRpcFactory<S, O extends ServiceOptions> extends Serializable { S create(O options);}
Expand Down
21 changes: 9 additions & 12 deletions src/main/java/com/google/gcloud/storage/StorageServiceOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@

import java.util.Set;

public class StorageServiceOptions extends ServiceOptions {
public class StorageServiceOptions extends ServiceOptions<StorageRpc, StorageServiceOptions> {

private static final long serialVersionUID = -7804860602287801084L;
private static final String GCS_SCOPE = "https://www.googleapis.com/auth/devstorage.full_control";
private static final Set<String> SCOPES = ImmutableSet.of(GCS_SCOPE);
private static final String DEFAULT_PATH_DELIMITER = "/";
private static final String PROJECT_ENV_NAME = "default_project_id";

private final StorageRpc storageRpc;
private final String project;
private final String pathDelimiter;

public static class Builder extends ServiceOptions.Builder<Builder> {
public static class Builder extends
ServiceOptions.Builder<StorageRpc, StorageServiceOptions, Builder> {

private String project;
private String pathDelimiter;
private StorageRpc storageRpc;

private Builder() {}

Expand All @@ -58,11 +58,6 @@ public Builder pathDelimiter(String pathDelimiter) {
return this;
}

public Builder storageRpc(StorageRpc storageRpc) {
this.storageRpc = storageRpc;
return this;
}

@Override
public StorageServiceOptions build() {
return new StorageServiceOptions(this);
Expand All @@ -74,16 +69,18 @@ private StorageServiceOptions(Builder builder) {
pathDelimiter = MoreObjects.firstNonNull(builder.pathDelimiter, DEFAULT_PATH_DELIMITER);
project = builder.project != null ? builder.project : defaultProject();
Preconditions.checkArgument(project != null, "Missing required project id");
storageRpc = MoreObjects.firstNonNull(builder.storageRpc, ServiceRpcProvider.storage(this));
}

@Override
protected Set<String> scopes() {
return SCOPES;
}

public StorageRpc storageRpc() {
return storageRpc;
StorageRpc storageRpc() {
if (serviceRpcFactory() != null) {
return serviceRpcFactory().create(this);
}
return ServiceRpcProvider.storage(this);
}

public String project() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.junit.Assert.assertTrue;

import com.google.gcloud.spi.DatastoreRpc;
import com.google.gcloud.spi.DatastoreRpcFactory;

import org.easymock.EasyMock;
import org.junit.Before;
Expand All @@ -33,17 +34,23 @@
public class DatastoreServiceOptionsTest {

private static final String DATASET = "dataset";
private DatastoreRpcFactory datastoreRpcFactory;
private DatastoreRpc datastoreRpc;
private DatastoreServiceOptions.Builder options;

@Before
public void setUp() throws IOException, InterruptedException {
datastoreRpcFactory = EasyMock.createMock(DatastoreRpcFactory.class);
datastoreRpc = EasyMock.createMock(DatastoreRpc.class);
options = DatastoreServiceOptions.builder()
.normalizeDataset(false)
.datastoreRpc(datastoreRpc)
.serviceRpcFactory(datastoreRpcFactory)
.dataset(DATASET)
.host("http://localhost:" + LocalGcdHelper.PORT);
EasyMock.expect(datastoreRpcFactory.create(EasyMock.anyObject(DatastoreServiceOptions.class)))
.andReturn(datastoreRpc)
.anyTimes();
EasyMock.replay(datastoreRpcFactory, datastoreRpc);
}

@Test
Expand All @@ -70,6 +77,7 @@ public void testForce() throws Exception {

@Test
public void testDatastore() throws Exception {
assertSame(datastoreRpcFactory, options.build().serviceRpcFactory());
assertSame(datastoreRpc, options.build().datastoreRpc());
}

Expand Down