Skip to content

Commit d197f7b

Browse files
yil532rsamborski
andauthored
[Fixit] add type hints to storage/api (GoogleCloudPlatform#10101)
* add type hints * add copyright 2023 * fix lint * fix type --------- Co-authored-by: Remigiusz Samborski <rsamborski@users.noreply.github.com>
1 parent e4c43c7 commit d197f7b

File tree

8 files changed

+205
-52
lines changed

8 files changed

+205
-52
lines changed

storage/api/compose_objects.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2013 Google, Inc.
1+
# Copyright 2023 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -37,7 +37,11 @@
3737
import googleapiclient.discovery
3838

3939

40-
def main(bucket, destination, sources):
40+
def main(
41+
bucket: str,
42+
destination: str,
43+
sources: list,
44+
) -> str:
4145
# Construct the service object for the interacting with the Cloud Storage
4246
# API.
4347
service = googleapiclient.discovery.build('storage', 'v1')
@@ -80,6 +84,8 @@ def main(bucket, destination, sources):
8084
print('> Composed file contents:')
8185
print(res)
8286

87+
return res
88+
8389

8490
if __name__ == '__main__':
8591
parser = argparse.ArgumentParser(

storage/api/compose_objects_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Copyright 2015 Google LLC
1+
# Copyright 2023 Google LLC
22
#
3-
# Licensed under the Apache License, Version 2.0 (the 'License');
3+
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
55
# You may obtain a copy of the License at
66
#
@@ -14,14 +14,14 @@
1414

1515
import os
1616

17-
from compose_objects import main
17+
import compose_objects
1818

1919
RESOURCES = os.path.join(os.path.dirname(__file__), 'resources')
2020
BUCKET = os.environ['CLOUD_STORAGE_BUCKET']
2121

2222

23-
def test_main():
24-
main(
23+
def test_main() -> None:
24+
compose_objects.main(
2525
BUCKET,
2626
'dest.txt',
2727
[os.path.join(RESOURCES, 'file1.txt'),

storage/api/crud_object.py

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2016 Google, Inc.
1+
# Copyright 2023 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -11,7 +11,6 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
1514
"""Application for uploading an object using the Cloud Storage API.
1615
1716
This sample is used on this page:
@@ -29,7 +28,23 @@
2928
import googleapiclient.http
3029

3130

32-
def main(bucket, filename, readers=[], owners=[]):
31+
def main(
32+
bucket: str,
33+
filename: str,
34+
readers: list = [],
35+
owners: list = [],
36+
) -> None:
37+
"""Main method that calls on the function in this file.
38+
39+
Args:
40+
bucket: The name of the bucket in which to upload the object.
41+
filename: The name of the object to upload.
42+
readers: The email addresses of the users who can read the object.
43+
owners: The email addresses of the users who can write the object.
44+
45+
Returns:
46+
None.
47+
"""
3348
print('Uploading object..')
3449
resp = upload_object(bucket, filename, readers, owners)
3550
print(json.dumps(resp, indent=2))
@@ -45,15 +60,35 @@ def main(bucket, filename, readers=[], owners=[]):
4560
print('Done')
4661

4762

48-
def create_service():
49-
# Construct the service object for interacting with the Cloud Storage API -
50-
# the 'storage' service, at version 'v1'.
51-
# You can browse other available api services and versions here:
52-
# http://g.co/dv/api-client-library/python/apis/
63+
def create_service() -> googleapiclient.discovery.Resource:
64+
"""Construct the service object for interacting with the
65+
Cloud Storage API - the 'storage' service, at version 'v1'.
66+
You can browse other available api services and versions here:
67+
http://g.co/dv/api-client-library/python/apis/
68+
69+
Returns:
70+
The service object for the Cloud Storage API.
71+
"""
5372
return googleapiclient.discovery.build('storage', 'v1')
5473

5574

56-
def upload_object(bucket, filename, readers, owners):
75+
def upload_object(
76+
bucket: str,
77+
filename: str,
78+
readers: list = [],
79+
owners: list = [],
80+
) -> googleapiclient.http.HttpRequest:
81+
"""Upload an object.
82+
83+
Args:
84+
bucket: The name of the bucket in which to upload the object.
85+
filename: The name of the object to upload.
86+
readers: The email addresses of the users who can read the object.
87+
owners: The email addresses of the users who can write the object.
88+
89+
Returns:
90+
The response from the upload request.
91+
"""
5792
service = create_service()
5893

5994
# This is the request body as specified:
@@ -95,7 +130,20 @@ def upload_object(bucket, filename, readers, owners):
95130
return resp
96131

97132

98-
def get_object(bucket, filename, out_file):
133+
def get_object(
134+
bucket: str,
135+
filename: str,
136+
out_file: str = None,
137+
) -> googleapiclient.http.HttpRequest:
138+
"""Get an object.
139+
140+
Args:
141+
bucket: The name of the bucket in which to look for the object.
142+
filename: The name of the object to get.
143+
144+
Returns:
145+
The response from the get request.
146+
"""
99147
service = create_service()
100148

101149
# Use get_media instead of get to get the actual contents of the object.
@@ -112,7 +160,19 @@ def get_object(bucket, filename, out_file):
112160
return out_file
113161

114162

115-
def delete_object(bucket, filename):
163+
def delete_object(
164+
bucket: str,
165+
filename: str,
166+
) -> googleapiclient.http.HttpRequest:
167+
"""Delete an object.
168+
169+
Args:
170+
bucket: The name of the bucket in which to look for the object.
171+
filename: The name of the object to delete.
172+
173+
Returns:
174+
The response from the delete request.
175+
"""
116176
service = create_service()
117177

118178
req = service.objects().delete(bucket=bucket, object=filename)

storage/api/crud_object_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Copyright 2016 Google LLC
1+
# Copyright 2023 Google LLC
22
#
3-
# Licensed under the Apache License, Version 2.0 (the 'License');
3+
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
55
# You may obtain a copy of the License at
66
#
@@ -25,7 +25,7 @@
2525

2626

2727
@pytest.fixture()
28-
def test_file():
28+
def test_file() -> str:
2929
_, file_path = tempfile.mkstemp()
3030

3131
with open(file_path, "w+b") as f:
@@ -36,7 +36,7 @@ def test_file():
3636
os.remove(file_path)
3737

3838

39-
def test_main(capsys, test_file):
39+
def test_main(capsys: pytest.CaptureFixture, test_file: str) -> None:
4040
main(BUCKET, test_file)
4141
out, err = capsys.readouterr()
4242

storage/api/customer_supplied_keys.py

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
#!/usr/bin/env python
2-
3-
# Copyright 2016 Google Inc. All rights reserved.
1+
# Copyright 2023 Google LLC
42
#
5-
# Licensed under the Apache License, Version 2.0 (the 'License');
3+
# Licensed under the Apache License, Version 2.0 (the "License");
64
# you may not use this file except in compliance with the License.
75
# You may obtain a copy of the License at
86
#
9-
# http://www.apache.org/licenses/LICENSE-2.0
7+
# http://www.apache.org/licenses/LICENSE-2.0
108
#
119
# Unless required by applicable law or agreed to in writing, software
12-
# distributed under the License is distributed on an 'AS IS' BASIS,
10+
# distributed under the License is distributed on an "AS IS" BASIS,
1311
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1412
# See the License for the specific language governing permissions and
1513
# limitations under the License.
14+
1615
"""Command-line sample app demonstrating customer-supplied encryption keys.
1716
1817
This sample demonstrates uploading an object while supplying an encryption key,
@@ -52,7 +51,7 @@
5251
ANOTHER_KEY_HASH = '/gd0N3k3MK0SEDxnUiaswl0FFv6+5PHpo+5KD5SBCeA='
5352

5453

55-
def create_service():
54+
def create_service() -> googleapiclient.discovery.Resource:
5655
"""Creates the service object for calling the Cloud Storage API."""
5756
# Construct the service object for interacting with the Cloud Storage API -
5857
# the 'storage' service, at version 'v1'.
@@ -61,8 +60,23 @@ def create_service():
6160
return googleapiclient.discovery.build('storage', 'v1')
6261

6362

64-
def upload_object(bucket, filename, encryption_key, key_hash):
65-
"""Uploads an object, specifying a custom encryption key."""
63+
def upload_object(
64+
bucket: str,
65+
filename: str,
66+
encryption_key: str,
67+
key_hash: str,
68+
) -> googleapiclient.http.HttpRequest:
69+
"""Uploads an object, specifying a custom encryption key.
70+
71+
Args:
72+
bucket: The name of the bucket to upload to.
73+
filename: The name of the file to upload.
74+
encryption_key: The encryption key to use for the upload.
75+
key_hash: The hash of the encryption key
76+
77+
Returns:
78+
The http request object.
79+
"""
6680
service = create_service()
6781

6882
with open(filename, 'rb') as f:
@@ -82,8 +96,25 @@ def upload_object(bucket, filename, encryption_key, key_hash):
8296
return resp
8397

8498

85-
def download_object(bucket, obj, out_file, encryption_key, key_hash):
86-
"""Downloads an object protected by a custom encryption key."""
99+
def download_object(
100+
bucket: str,
101+
obj: str,
102+
out_file: str,
103+
encryption_key: str,
104+
key_hash: str,
105+
) -> googleapiclient.http.HttpRequest:
106+
"""Downloads an object protected by a custom encryption key.
107+
108+
Args:
109+
bucket: The name of the bucket to download from.
110+
obj: The name of the object to download.
111+
filename: The name of the file to download to.
112+
encryption_key: The encryption key to use for the download
113+
key_hash: The hash of the encryption key
114+
115+
Returns:
116+
The http request object.
117+
"""
87118
service = create_service()
88119

89120
request = service.objects().get_media(bucket=bucket, object=obj)
@@ -97,9 +128,27 @@ def download_object(bucket, obj, out_file, encryption_key, key_hash):
97128
out_file.write(request.execute())
98129

99130

100-
def rotate_key(bucket, obj, current_encryption_key, current_key_hash,
101-
new_encryption_key, new_key_hash):
102-
"""Changes the encryption key used to store an existing object."""
131+
def rotate_key(
132+
bucket: str,
133+
obj: str,
134+
current_encryption_key: str,
135+
current_key_hash: str,
136+
new_encryption_key: str,
137+
new_key_hash: str,
138+
) -> googleapiclient.http.HttpRequest:
139+
"""Changes the encryption key used to store an existing object.
140+
141+
Args:
142+
bucket: The name of the bucket to upload to.
143+
obj: The name of the object to upload.
144+
current_encryption_key: The current encryption key to use for the upload.
145+
current_key_hash: The current hash of the encryption key
146+
new_encryption_key: The new encryption key to use for the upload.
147+
new_key_hash: The new hash of the encryption key
148+
149+
Returns:
150+
The http request object.
151+
"""
103152
service = create_service()
104153

105154
request = service.objects().rewrite(
@@ -131,7 +180,19 @@ def rotate_key(bucket, obj, current_encryption_key, current_key_hash,
131180
rewrite_response.execute()
132181

133182

134-
def main(bucket, filename):
183+
def main(
184+
bucket: str,
185+
filename: str,
186+
) -> None:
187+
"""Main method to upload and download objects.
188+
189+
Args:
190+
bucket: The name of the bucket to upload to.
191+
filename: The name of the file to upload.
192+
193+
Returns:
194+
None.
195+
"""
135196
print(f'Uploading object gs://{bucket}/{filename}')
136197
upload_object(bucket, filename, ENCRYPTION_KEY, KEY_HASH)
137198
print('Downloading it back')

storage/api/customer_supplied_keys_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Copyright 2016 Google LLC
1+
# Copyright 2023 Google LLC
22
#
3-
# Licensed under the Apache License, Version 2.0 (the 'License');
3+
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
55
# You may obtain a copy of the License at
66
#
@@ -23,7 +23,7 @@
2323

2424

2525
@pytest.mark.flaky
26-
def test_main(capsys):
26+
def test_main(capsys: pytest.CaptureFixture) -> None:
2727
main(BUCKET, __file__)
2828
out, err = capsys.readouterr()
2929

0 commit comments

Comments
 (0)