Skip to content

Commit 5b2467a

Browse files
Dillon NysJordan-Nelson
authored andcommitted
chore(sigv4): Clean up example
Remove local type annotations since these are not used throughout the rest of the codebase and we should not establish a precedence we don't follow ourselves. The original intention was to make things more clear but it can set a bad example. commit-id:4c0d01a1
1 parent 8ac3242 commit 5b2467a

File tree

3 files changed

+45
-50
lines changed

3 files changed

+45
-50
lines changed

packages/aws_signature_v4/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import 'package:aws_common/aws_common.dart';
4242
import 'package:aws_signature_v4/aws_signature_v4.dart';
4343
4444
// Create the signer instance with credentials from the environment.
45-
const AWSSigV4Signer signer = AWSSigV4Signer(
45+
const signer = AWSSigV4Signer(
4646
credentialsProvider: AWSCredentialsProvider.environment(),
4747
);
4848
@@ -52,7 +52,7 @@ final scope = AWSCredentialScope(
5252
region: region,
5353
service: AWSService.cognitoIdentityProvider,
5454
);
55-
final AWSHttpRequest request = AWSHttpRequest(
55+
final request = AWSHttpRequest(
5656
method: AWSHttpMethod.post,
5757
uri: Uri.https('cognito-idp.$region.amazonaws.com', '/'),
5858
headers: const {
@@ -65,13 +65,13 @@ final AWSHttpRequest request = AWSHttpRequest(
6565
);
6666
6767
// Sign and send the HTTP request
68-
final AWSSignedRequest signedRequest = await signer.sign(
68+
final signedRequest = await signer.sign(
6969
request,
7070
credentialScope: scope,
7171
);
72-
final AWSStreamedHttpResponse resp = await signedRequest.send();
73-
final String respBody = await resp.decodeBody();
74-
print(respBody);
72+
final resp = await signedRequest.send();
73+
final respBody = await resp.decodeBody();
74+
safePrint(respBody);
7575
```
7676

7777
For a full example, check out the [example](https://github.com/aws-amplify/amplify-flutter/tree/main/packages/aws_signature_v4/example) project in the GitHub repo.

packages/aws_signature_v4/example/bin/example.dart

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
// ignore_for_file: omit_local_variable_types
16-
1715
import 'dart:convert';
1816
import 'dart:io';
1917
import 'dart:math';
@@ -73,11 +71,11 @@ Future<void> main(List<String> args) async {
7371
);
7472

7573
final parsedArgs = argParser.parse(args);
76-
final String? accessKeyId = Platform.environment[$awsAccessKeyId] ??
74+
final accessKeyId = Platform.environment[$awsAccessKeyId] ??
7775
parsedArgs[accessKeyIdArg] as String?;
78-
final String? secretAccessKey = Platform.environment[$awsSecretAccessKey] ??
76+
final secretAccessKey = Platform.environment[$awsSecretAccessKey] ??
7977
parsedArgs[secretAccessKeyArg] as String?;
80-
final String? sessionToken = Platform.environment[$awsSessionToken] ??
78+
final sessionToken = Platform.environment[$awsSessionToken] ??
8179
parsedArgs[sessionTokenArg] as String?;
8280

8381
if (accessKeyId == null || secretAccessKey == null) {
@@ -95,29 +93,29 @@ Future<void> main(List<String> args) async {
9593
);
9694
}
9795

98-
final AWSCredentials credentials =
96+
final credentials =
9997
AWSCredentials(accessKeyId, secretAccessKey, sessionToken);
100-
final AWSSigV4Signer signer = AWSSigV4Signer(
98+
final signer = AWSSigV4Signer(
10199
credentialsProvider: AWSCredentialsProvider(credentials),
102100
);
103101

104102
// Set up S3 values
105-
final AWSCredentialScope scope = AWSCredentialScope(
103+
final scope = AWSCredentialScope(
106104
region: region,
107105
service: AWSService.s3,
108106
);
109-
final String host = '$bucket.s3.$region.amazonaws.com';
110-
final ServiceConfiguration serviceConfiguration = S3ServiceConfiguration();
107+
final host = '$bucket.s3.$region.amazonaws.com';
108+
final serviceConfiguration = S3ServiceConfiguration();
111109

112110
// Create the bucket
113-
final List<int> createBody = utf8.encode(
111+
final createBody = utf8.encode(
114112
'''
115113
<CreateBucketConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
116114
<LocationConstraint>$region</LocationConstraint>
117115
</CreateBucketConfiguration>
118116
''',
119117
);
120-
final AWSHttpRequest createRequest = AWSHttpRequest.put(
118+
final createRequest = AWSHttpRequest.put(
121119
Uri.https(host, '/'),
122120
body: createBody,
123121
headers: {
@@ -128,14 +126,13 @@ Future<void> main(List<String> args) async {
128126
);
129127

130128
stdout.writeln('Creating bucket $bucket...');
131-
final AWSSignedRequest signedCreateRequest = await signer.sign(
129+
final signedCreateRequest = await signer.sign(
132130
createRequest,
133131
credentialScope: scope,
134132
serviceConfiguration: serviceConfiguration,
135133
);
136-
final AWSStreamedHttpResponse createResponse =
137-
await signedCreateRequest.send();
138-
final int createStatus = createResponse.statusCode;
134+
final createResponse = await signedCreateRequest.send();
135+
final createStatus = createResponse.statusCode;
139136
stdout.writeln('Create Bucket Response: $createStatus');
140137
if (createStatus == 409) {
141138
exitWithError('Bucket name already exists!');
@@ -146,9 +143,9 @@ Future<void> main(List<String> args) async {
146143
stdout.writeln('Bucket creation succeeded!');
147144

148145
// Upload the file
149-
final Stream<List<int>> file = File(filename).openRead();
150-
final String path = '/${p.basename(filename)}';
151-
final AWSStreamedHttpRequest uploadRequest = AWSStreamedHttpRequest.put(
146+
final file = File(filename).openRead();
147+
final path = '/${p.basename(filename)}';
148+
final uploadRequest = AWSStreamedHttpRequest.put(
152149
Uri.https(host, path),
153150
body: file,
154151
headers: {
@@ -158,28 +155,27 @@ Future<void> main(List<String> args) async {
158155
);
159156

160157
stdout.writeln('Uploading file $filename to $path...');
161-
final AWSSignedRequest signedUploadRequest = await signer.sign(
158+
final signedUploadRequest = await signer.sign(
162159
uploadRequest,
163160
credentialScope: scope,
164161
serviceConfiguration: serviceConfiguration,
165162
);
166-
final AWSStreamedHttpResponse uploadResponse =
167-
await signedUploadRequest.send();
168-
final int uploadStatus = uploadResponse.statusCode;
163+
final uploadResponse = await signedUploadRequest.send();
164+
final uploadStatus = uploadResponse.statusCode;
169165
stdout.writeln('Upload File Response: $uploadStatus');
170166
if (uploadStatus != 200) {
171167
exitWithError('Could not upload file');
172168
}
173169
stdout.writeln('File uploaded successfully!');
174170

175171
// Create a pre-signed URL for downloading the file
176-
final AWSHttpRequest urlRequest = AWSHttpRequest.get(
172+
final urlRequest = AWSHttpRequest.get(
177173
Uri.https(host, path),
178174
headers: {
179175
AWSHeaders.host: host,
180176
},
181177
);
182-
final Uri signedUrl = await signer.presign(
178+
final signedUrl = await signer.presign(
183179
urlRequest,
184180
credentialScope: scope,
185181
serviceConfiguration: serviceConfiguration,

packages/aws_signature_v4/example/web/main.dart

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,34 +61,34 @@ void main() {
6161
}
6262

6363
Future<void> upload(BucketUpload bucketUpload) async {
64-
final String bucketName = bucketUpload.bucketName;
65-
final String region = bucketUpload.region;
66-
final File file = bucketUpload.file;
67-
final String filename = p.basename(bucketUpload.file.name);
64+
final bucketName = bucketUpload.bucketName;
65+
final region = bucketUpload.region;
66+
final file = bucketUpload.file;
67+
final filename = p.basename(bucketUpload.file.name);
6868

69-
const AWSSigV4Signer signer = AWSSigV4Signer();
69+
const signer = AWSSigV4Signer();
7070

7171
// Set up S3 values
72-
final AWSCredentialScope scope = AWSCredentialScope(
72+
final scope = AWSCredentialScope(
7373
region: region,
7474
service: AWSService.s3,
7575
);
76-
final ServiceConfiguration serviceConfiguration = S3ServiceConfiguration();
77-
final String host = '$bucketName.s3.$region.amazonaws.com';
78-
final String path = '/$filename';
76+
final serviceConfiguration = S3ServiceConfiguration();
77+
final host = '$bucketName.s3.$region.amazonaws.com';
78+
final path = '/$filename';
7979

8080
// Read the file's bytes
81-
final Blob fileBlob = file.slice();
82-
final FileReader reader = FileReader();
81+
final fileBlob = file.slice();
82+
final reader = FileReader();
8383
reader.readAsArrayBuffer(fileBlob);
8484
await reader.onLoadEnd.first;
85-
final Uint8List? fileBytes = reader.result as Uint8List?;
85+
final fileBytes = reader.result as Uint8List?;
8686
if (fileBytes == null) {
8787
throw Exception('Cannot read bytes from Blob.');
8888
}
8989

9090
// Upload the file
91-
final AWSHttpRequest uploadRequest = AWSHttpRequest.put(
91+
final uploadRequest = AWSHttpRequest.put(
9292
Uri.https(host, path),
9393
body: fileBytes,
9494
headers: {
@@ -98,28 +98,27 @@ Future<void> upload(BucketUpload bucketUpload) async {
9898
);
9999

100100
safePrint('Uploading file $filename to $path...');
101-
final AWSSignedRequest signedUploadRequest = await signer.sign(
101+
final signedUploadRequest = await signer.sign(
102102
uploadRequest,
103103
credentialScope: scope,
104104
serviceConfiguration: serviceConfiguration,
105105
);
106-
final AWSStreamedHttpResponse uploadResponse =
107-
await signedUploadRequest.send();
108-
final int uploadStatus = uploadResponse.statusCode;
106+
final uploadResponse = await signedUploadRequest.send();
107+
final uploadStatus = uploadResponse.statusCode;
109108
safePrint('Upload File Response: $uploadStatus');
110109
if (uploadStatus != 200) {
111110
throw Exception('Could not upload file');
112111
}
113112
safePrint('File uploaded successfully!');
114113

115114
// Create a pre-signed URL for downloading the file
116-
final AWSHttpRequest urlRequest = AWSHttpRequest.get(
115+
final urlRequest = AWSHttpRequest.get(
117116
Uri.https(host, path),
118117
headers: {
119118
AWSHeaders.host: host,
120119
},
121120
);
122-
final Uri signedUrl = await signer.presign(
121+
final signedUrl = await signer.presign(
123122
urlRequest,
124123
credentialScope: scope,
125124
serviceConfiguration: serviceConfiguration,

0 commit comments

Comments
 (0)