Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,58 @@ Flutter package for Amazon PA-API 5.0.

## Getting Started

As this package is not available via pub you have to add it manually to your `pubspec.yaml`.
There are at least two ways to use it in your project.

### Option 1 - Add git repo to `pubspec.yaml`
```yaml
flutter_amazon_pa_api:
git:
url: https://github.com/meganii/flutter_amazon_pa_api.git
ref: master
```

### Option 2 - Add local copy to `pubspec.yaml`
```yaml
flutter_amazon_pa_api:
path: ../flutter_amazon_pa_api
```

## Usage in your project
After adding the package to your `pubspec.yaml` you have to add the package to your code.
```dart
import 'package:flutter_amazon_pa_api/flutter_amazon_pa_api.dart';
import 'package:flutter_amazon_pa_api/search_items_response.dart';
import 'package:flutter_amazon_pa_api/paapi_resource.dart';

// list all resources you want to fetch
var resources = [
PaAPIResource.BrowseNodeInfo_BrowseNodes.name,
PaAPIResource.Images_Primary_Small.name,
PaAPIResource.Images_Primary_Medium.name,
PaAPIResource.Images_Primary_Large.name,
PaAPIResource.ItemInfo_ByLineInfo.name,
PaAPIResource.ItemInfo_ContentInfo.name,
PaAPIResource.ItemInfo_Title.name,
PaAPIResource.Offers_Listings_Price.name,
PaAPIResource.Offers_Listings_SavingBasis.name,
PaAPIResource.Offers_Listings_Availability_MaxOrderQuantity.name,
PaAPIResource.Offers_Listings_DeliveryInfo_IsAmazonFulfilled.name,
];

PaAPI paAPI = PaAPI(
accessKey: "<your access key>",
secretKey: "<your secretkey>",
partnerTag: "<your partner tag>",
// marketplace is defaulting to US. Use the PaAPIMarketplace enum to select your marketplace
marketplace: PaAPIMarketplace.de);

// this will get all Items for this ASIN
await paAPI.getItems(["B00S7N8G2Q"], resources);
// this will get all Items for these keywords
await paAPI.searchItems("urban classics damen ladies laces dress kleid", resources)
```

This project is a starting point for a Dart
[package](https://flutter.dev/developing-packages/),
a library module containing code that can be shared easily across
Expand Down
63 changes: 42 additions & 21 deletions lib/flutter_amazon_pa_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ library flutter_amazon_pa_api;
import 'dart:convert';

import 'package:flutter_amazon_pa_api/get_items_response.dart';
import 'package:flutter_amazon_pa_api/search_items_response.dart';
import 'package:flutter_amazon_pa_api/paapi_operation.dart';
import 'package:flutter_amazon_pa_api/paapi_marketplace.dart';
import 'package:http/http.dart' as http;
import 'package:crypto/crypto.dart';
import 'package:intl/intl.dart';
Expand All @@ -20,45 +23,63 @@ class PaAPI {
/// Service name
String service = 'ProductAdvertisingAPI';

/// Host default=JP
String host = 'webservices.amazon.co.jp';
/// Host
late String host;

/// Region default=JP
String region = 'us-west-2';
/// Region
late String region;

String marketplace = 'www.amazon.co.jp';
/// Markeptlace default=us
PaAPIMarketplace marketplace;

/// API Request PATH
late String path;

/// Amazon associate tag
late String partnerTag;

PaAPI({required this.accessKey, required this.secretKey});
PaAPI(
{required this.accessKey,
required this.secretKey,
required this.partnerTag,
this.marketplace = PaAPIMarketplace.us}) {
this.host = this.marketplace.host;
this.region = this.marketplace.region;
}

Future<GetItemsResponse> getItems(List<String> items) async {
Future<GetItemsResponse> getItems(
List<String> items, List<String> resources) async {
final body = {
"ItemIds": items,
"Resources": [
"BrowseNodeInfo.BrowseNodes",
"Images.Primary.Small",
"Images.Primary.Medium",
"Images.Primary.Large",
"ItemInfo.ByLineInfo",
"ItemInfo.ContentInfo",
"ItemInfo.Title"
],
"Resources": resources,
"PartnerTag": this.partnerTag,
"PartnerType": "Associates",
"Marketplace": this.marketplace,
"Operation": "GetItems"
"Marketplace": this.marketplace.name,
"Operation": PaAPIOperation.GetItems.name
};
final response = await _post('/paapi5/getitems', body);
final response =
await _post('/paapi5/getitems', body, PaAPIOperation.GetItems);
return GetItemsResponse.fromJson(response);
}

Future<dynamic> _post(String path, Map<String, dynamic> body) async {
var headers = _createHeaders(path, 'GetItems', body);
Future<SearchItemsResponse> searchItems(
String keywords, List<String> resources) async {
final body = {
"Keywords": keywords,
"Resources": resources,
"PartnerTag": this.partnerTag,
"PartnerType": "Associates",
"Marketplace": this.marketplace.name,
"Operation": PaAPIOperation.SearchItems.name
};
final response =
await _post('/paapi5/getitems', body, PaAPIOperation.SearchItems);
return SearchItemsResponse.fromJson(response);
}

Future<dynamic> _post(
String path, Map<String, dynamic> body, PaAPIOperation operation) async {
var headers = _createHeaders(path, operation.name, body);
var url = Uri.parse('https://$host$path');
var response =
await http.post(url, headers: headers, body: json.encode(body));
Expand Down
6 changes: 5 additions & 1 deletion lib/item.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter_amazon_pa_api/images.dart';
import 'package:flutter_amazon_pa_api/item_info.dart';
import 'package:flutter_amazon_pa_api/offers.dart';
import 'package:json_annotation/json_annotation.dart';

part 'item.g.dart';
Expand All @@ -18,7 +19,10 @@ class Item {
@JsonKey(name: 'ItemInfo')
ItemInfo? itemInfo;

Item(this.asin, this.detailPageURL, this.images, this.itemInfo);
@JsonKey(name: 'Offers')
Offers? offers;

Item(this.asin, this.detailPageURL, this.images, this.itemInfo, this.offers);

factory Item.fromJson(Map<String, dynamic> json) => _$ItemFromJson(json);
Map<String, dynamic> toJson() => _$ItemToJson(this);
Expand Down
4 changes: 4 additions & 0 deletions lib/item.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

127 changes: 127 additions & 0 deletions lib/listings.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import 'package:json_annotation/json_annotation.dart';

part 'listings.g.dart';

@JsonSerializable()
class Listings {
@JsonKey(name: 'Availability')
Availability? availability;

@JsonKey(name: 'DeliveryInfo')
DeliveryInfo? deliveryInfo;

@JsonKey(name: 'Price')
Price? price;

@JsonKey(name: 'SavingBasis')
SavingBasis? savingBasis;

Listings(this.availability, this.deliveryInfo, this.price, this.savingBasis);

factory Listings.fromJson(Map<String, dynamic> json) =>
_$ListingsFromJson(json);
Map<String, dynamic> toJson() => _$ListingsToJson(this);
}

@JsonSerializable()
class Availability {
@JsonKey(name: 'MaxOrderQuantity')
num? maxOrderQuantity;

@JsonKey(name: 'Message')
String? message;

@JsonKey(name: 'MinOrderQuantity')
num? minOrderQuantity;

@JsonKey(name: 'Type')
String? type;

Availability(
this.maxOrderQuantity, this.message, this.minOrderQuantity, this.type);

factory Availability.fromJson(Map<String, dynamic> json) =>
_$AvailabilityFromJson(json);
Map<String, dynamic> toJson() => _$AvailabilityToJson(this);
}

@JsonSerializable()
class DeliveryInfo {
@JsonKey(name: 'IsAmazonFulfilled')
bool? isAmazonFulfilled;

@JsonKey(name: 'IsFreeShippingEligible')
bool? isFreeShippingEligible;

@JsonKey(name: 'IsPrimeEligible')
bool? isPrimeEligible;

DeliveryInfo(this.isAmazonFulfilled, this.isFreeShippingEligible,
this.isPrimeEligible);

factory DeliveryInfo.fromJson(Map<String, dynamic> json) =>
_$DeliveryInfoFromJson(json);
Map<String, dynamic> toJson() => _$DeliveryInfoToJson(this);
}

@JsonSerializable()
class Price {
@JsonKey(name: 'Amount')
num? amount;

@JsonKey(name: 'Currency')
String? currency;

@JsonKey(name: 'DisplayAmount')
String? displayAmount;

@JsonKey(name: 'Savings')
Savings? savings;

Price(this.amount, this.currency, this.displayAmount, this.savings);

factory Price.fromJson(Map<String, dynamic> json) => _$PriceFromJson(json);
Map<String, dynamic> toJson() => _$PriceToJson(this);
}

@JsonSerializable()
class Savings {
@JsonKey(name: 'Amount')
num? amount;

@JsonKey(name: 'Currency')
String? currency;

@JsonKey(name: 'DisplayAmount')
String? displayAmount;

@JsonKey(name: 'Percentage')
num? percentage;

Savings(this.amount, this.currency, this.displayAmount, this.percentage);

factory Savings.fromJson(Map<String, dynamic> json) =>
_$SavingsFromJson(json);
Map<String, dynamic> toJson() => _$SavingsToJson(this);
}

@JsonSerializable()
class SavingBasis {
@JsonKey(name: 'Amount')
num? amount;

@JsonKey(name: 'Currency')
String? currency;

@JsonKey(name: 'DisplayAmount')
String? displayAmount;

@JsonKey(name: 'PriceType')
String? priceType;

SavingBasis(this.amount, this.currency, this.displayAmount, this.priceType);

factory SavingBasis.fromJson(Map<String, dynamic> json) =>
_$SavingBasisFromJson(json);
Map<String, dynamic> toJson() => _$SavingBasisToJson(this);
}
Loading