aws_request 2.1.0
aws_request: ^2.1.0 copied to clipboard
Easily create, sign, and send API requests to AWS services without the hassle of implementing Signature Version 4.
aws_request
Easily create, sign, and send API requests to AWS.
Resources
Documentation Pub Package GitHub Repository
If you have feedback or have a use case that isn't covered feel free to open an issue.
Requirements #
- Dart SDK
>=3.0.0 <4.0.0
Getting Started #
Create a request and send it!
import 'package:aws_request/aws_request.dart';
void main() {
final AwsRequest request = AwsRequest(
awsAccessKey: 'awsAccessKey',
awsSecretKey: 'awsSecretKey',
region: 'region',
);
request.send(type: AwsRequestType.post, service: 'logs');
}
The following parameters can be provided to the send() function:
type: request type (GET, POST, PUT, etc)
service: aws service you are sending request to
signedHeaders: a list of headers aws requires in the signature.
Default included signed headers are: (content-type, host, x-amz-date)
(You do not need to provide these in [headers])
headers: any required headers. Any non-default headers included in the signedHeaders
must be added here.
jsonBody: the body of the request, formatted as json
queryPath: the aws query path
queryString: the url query string as a Map (duplicate keys not supported; Dart Map limitation)
timeout: overrides the constructor request timeout (default: 10 seconds)
endpoint: custom hostname override (defaults to {service}.{region}.amazonaws.com)
Supported HTTP methods are GET, POST, DELETE, PATCH, PUT, HEAD.
Important Notes: #
Temporary credentials (STS, IAM roles) #
Use the temporary access key ID and secret from STS in awsAccessKey / awsSecretKey.
You must also pass the session token and sign it: set
headers: {'X-Amz-Security-Token': '<token>'} and
signedHeaders: ['x-amz-security-token'] on send() / staticSend (header names are
matched case-insensitively).
Default Headers #
The default Content-Type is application/x-amz-json-1.1, which works for most AWS
JSON-protocol services (CloudWatch Logs, Lambda, etc.). Some services require a different
value — for example, DynamoDB uses application/x-amz-json-1.0, and S3/SQS/SNS use XML
content types. Override via the headers parameter when needed.
Custom Endpoints #
By default, requests are sent to {service}.{region}.amazonaws.com. If you need a
non-standard endpoint (S3 virtual-hosted-style buckets, VPC endpoints, LocalStack, etc.),
pass the endpoint parameter:
final AwsRequest request = AwsRequest(
awsAccessKey: 'awsAccessKey',
awsSecretKey: 'awsSecretKey',
region: 'us-east-1',
endpoint: 'mybucket.s3.us-east-1.amazonaws.com',
);
The endpoint can also be passed per-request via send() or staticSend(), and will
override the constructor value.
Retries and HTTP clients #
This package does not implement automatic retries (e.g. exponential backoff for HTTP 429/503). Add retry logic at your call site if you need it.
Each request uses a short-lived http Client that is closed after the call (see getRequest in
the implementation). There is no shared connection pool inside the library. For very high throughput,
consider your own client lifecycle or an AWS SDK.
Android #
If running on android, make sure you have
<uses-permission android:name="android.permission.INTERNET" />
in your app's android/app/src/main/AndroidManifest.xml
Examples #
Example 1 #
Here's an example of using aws_request to send a CloudWatch PutLogEvent request:
import 'package:aws_request/aws_request.dart';
import 'package:http/http.dart';
Future<void> awsRequestFunction(String logString) async {
final AwsRequest request = AwsRequest(
awsAccessKey: 'awsAccessKey',
awsSecretKey: 'awsSecretKey',
region: 'region',
);
final Response result = await request.send(
type: AwsRequestType.post,
jsonBody: '{"jsonKey": "jsonValue"}',
service: 'logs',
queryString: {'X-Amz-Expires': '10'},
headers: {'X-Amz-Security-Token': 'XXXXXXXXXXXX'},
);
print(result.statusCode);
}
Example 2 #
There is also a static method if you find that more useful:
import 'package:aws_request/aws_request.dart';
import 'package:http/http.dart';
Future<void> awsRequestFunction(String logString) async {
final Response result = await AwsRequest.staticSend(
awsAccessKey: 'awsAccessKey',
awsSecretKey: 'awsSecretKey',
region: 'region',
type: AwsRequestType.post,
jsonBody: '{"jsonKey": "jsonValue"}',
service: 'logs',
queryString: {'X-Amz-Expires': '10'},
headers: {'X-Amz-Security-Token': 'XXXXXXXXXXXX'},
);
print(result.statusCode);
}
Testing #
A MockAwsRequest class is provided for testing via the testing library.
It mirrors the AwsRequest API but uses a mock HTTP client instead of making
real network calls:
import 'package:aws_request/testing.dart';
import 'package:http/http.dart';
void main() {
final MockAwsRequest mockRequest = MockAwsRequest(
awsAccessKey: 'awsAccessKey',
awsSecretKey: 'awsSecretKey',
region: 'region',
mockFunction: (Request request) async {
return Response('{"status": "ok"}', 200);
},
);
mockRequest.send(
type: AwsRequestType.post,
service: 'logs',
jsonBody: '{"key": "value"}',
);
}
MIT License #
Copyright (c) Zachary Merritt
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.