Skip to content

Commit

Permalink
Add Fields parameters to ListObjectsOptions and ListBucketsOptions
Browse files Browse the repository at this point in the history
An alternative design would be to accept a collection of strings, and perform the join ourselves - possibly adding "nextPageToken" implicitly.

This design is closer to a "raw" approach for fullest control.

Fixes #4019.
  • Loading branch information
jskeet committed Jan 6, 2020
1 parent 76ddfe0 commit f556739
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ public async Task CancellationTokenRespected()
await Assert.ThrowsAnyAsync<OperationCanceledException>(async () => await task);
}

[Fact]
public void PartialResponses()
{
var options = new ListBucketsOptions { Fields = "items(name,location),nextPageToken" };
var buckets = _fixture.Client.ListBuckets(_fixture.ProjectId, options).ToList();
foreach (var bucket in buckets)
{
// These fields are requested
Assert.NotNull(bucket.Name);
Assert.NotNull(bucket.Location);
// These are not
Assert.Null(bucket.LocationType);
Assert.Null(bucket.ETag);
}
}

// Fetches buckets using the given options in each possible way, validating that the expected bucket names are returned.
private async Task AssertBuckets(ListBucketsOptions options, params string[] expectedBucketNames)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ public async Task MultipleVersions()
await AssertObjects(name, null, name);
}

[Fact]
public void PartialResponses()
{
var options = new ListObjectsOptions { Fields = "items(name,contentType),nextPageToken" };
var objects = _fixture.Client.ListObjects(_fixture.ReadBucket, options: options).ToList();
foreach (var obj in objects)
{
// These fields are requested
Assert.NotNull(obj.Name);
Assert.NotNull(obj.ContentType);
// These are not
Assert.Null(obj.ContentEncoding);
Assert.Null(obj.ContentDisposition);
}
}

private async Task AssertObjects(string prefix, ListObjectsOptions options, params string[] expectedNames)
{
IEnumerable<Object> actual = _fixture.Client.ListObjects(_fixture.ReadBucket, prefix, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ public void ModifyRequest_AllOptions()
PageSize = 10,
Prefix = "prefix",
Projection = Projection.Full,
PageToken = "nextpage"
PageToken = "nextpage",
Fields = "items(name),nextPageToken"
};
options.ModifyRequest(request);
Assert.Equal(10, request.MaxResults);
Assert.Equal("prefix", request.Prefix);
Assert.Equal(ProjectionEnum.Full, request.Projection);
Assert.Equal("nextpage", request.PageToken);
Assert.Equal("items(name),nextPageToken", request.Fields);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public void ModifyRequest_AllOptions()
Versions = true,
UserProject = "proj",
PageToken = "nextpage",
Fields = "items(name),nextPageToken"
};
options.ModifyRequest(request);
Assert.Equal(10, request.MaxResults);
Expand All @@ -57,6 +58,7 @@ public void ModifyRequest_AllOptions()
Assert.True(request.Versions);
Assert.Equal("proj", request.UserProject);
Assert.Equal("nextpage", request.PageToken);
Assert.Equal("items(name),nextPageToken", request.Fields);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ public sealed class ListBucketsOptions
/// </summary>
public string PageToken { get; set; }

/// <summary>
/// If set, this specifies the fields to fetch in the result to obtain partial responses,
/// usually to improve performance.
/// For example, to fetch just the name and location of each bucket, set this property to
/// "items(name,location),nextPageToken". The "nextPageToken" field is required in order to
/// fetch multiple pages; the library does not add this automatically.
/// See https://cloud.google.com/storage/docs/json_api/v1/how-tos/performance#partial for more details
/// on specifying fields for partial responses.
/// </summary>
public string Fields { get; set; }

/// <summary>
/// Modifies the specified request for all non-null properties of this options object.
/// </summary>
Expand All @@ -68,6 +79,10 @@ internal void ModifyRequest(ListRequest request)
{
request.PageToken = PageToken;
}
if (Fields != null)
{
request.Fields = Fields;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ public sealed class ListObjectsOptions
/// </summary>
public string PageToken { get; set; }

/// <summary>
/// If set, this specifies the fields to fetch in the result to obtain partial responses,
/// usually to improve performance.
/// For example, to fetch just the name and content type of each object, set this property to
/// "items(name,contentType),nextPageToken". The "nextPageToken" field is required in order to
/// fetch multiple pages; the library does not add this automatically.
/// See https://cloud.google.com/storage/docs/json_api/v1/how-tos/performance#partial for more details
/// on specifying fields for partial responses.
/// </summary>
public string Fields { get; set; }

/// <summary>
/// Modifies the specified request for all non-null properties of this options object.
/// </summary>
Expand Down Expand Up @@ -103,6 +114,10 @@ internal void ModifyRequest(ListRequest request)
{
request.PageToken = PageToken;
}
if (Fields != null)
{
request.Fields = Fields;
}
}
}
}

0 comments on commit f556739

Please sign in to comment.