Skip to content

Commit 296a31f

Browse files
Fixed type issues
1 parent 0931509 commit 296a31f

File tree

4 files changed

+96
-36
lines changed

4 files changed

+96
-36
lines changed

README.md

Lines changed: 86 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,97 @@
22

33
A dart package to interact with the WooCommerce API. It uses OAuth1.0a behind the scenes to generate the signature and URL string. It then makes calls and return the data back to the calling function.
44

5-
## Getting Started
5+
## Complete Usage Example
6+
```
7+
import 'dart:async';
68
7-
- Import the package
9+
import 'package:flutter/material.dart';
10+
import 'package:woocommerce_api/woocommerce_api.dart';
811
9-
`import 'package:woocommerce_api/woocommerce_api.dart';`
12+
void main() => runApp(new MyApp());
1013
11-
- Initialize the SDK
14+
class MyApp extends StatelessWidget {
15+
@override
16+
Widget build(BuildContext context) {
17+
return new MaterialApp(
18+
title: 'Flutter Demo',
19+
theme: new ThemeData(
20+
primarySwatch: Colors.blue,
21+
),
22+
home: new MyHomePage(title: 'WooCommerce API Demo'),
23+
);
24+
}
25+
}
1226
13-
```
14-
WooCommerceAPI wc_api = new WooCommerceAPI(
15-
"http://www.mywoocommerce.com",
16-
"ck_...",
17-
"cs_..."
18-
);
19-
```
27+
class MyHomePage extends StatefulWidget {
28+
MyHomePage({Key key, this.title}) : super(key: key);
2029
21-
- Use functions
30+
final String title;
2231
23-
```
24-
List _products = new List();
32+
@override
33+
_MyHomePageState createState() => new _MyHomePageState();
34+
}
35+
36+
class _MyHomePageState extends State<MyHomePage> {
37+
38+
List<Widget> products = [];
39+
40+
Future getProducts() async {
41+
WooCommerceAPI wc_api = new WooCommerceAPI(
42+
"http://samarth.todaylivedeal.com",
43+
"ck_2b548f9652a3468f67bbc5bdb04e48edf270a000",
44+
"cs_fa349f1ba49b519ba9493eb2b64c88781b286b52"
45+
);
46+
47+
var p = await wc_api.getAsync("products");
48+
return p;
49+
}
50+
51+
@override
52+
void initState() {
53+
super.initState();
54+
}
55+
56+
@override
57+
Widget build(BuildContext context) {
58+
return new Scaffold(
59+
appBar: new AppBar(
60+
title: new Text(widget.title),
61+
),
62+
body: FutureBuilder(
63+
future: getProducts(),
64+
builder: (_, s){
65+
66+
if(s.data == null){
67+
return Container(
68+
child: Center(
69+
child: Text("Loading..."),
70+
),
71+
);
72+
}
73+
74+
return ListView.builder(
75+
itemCount: s.data.length,
76+
itemBuilder: (_, index){
77+
78+
return ListTile(
79+
leading: CircleAvatar(
80+
child: Image.network(s.data[index]["images"][0]["src"]),
81+
),
82+
title: Text(s.data[index]["name"]),
83+
subtitle: Text("Buy now for \$ " + s.data[index]["price"]),
84+
);
85+
86+
}
87+
);
88+
},
89+
),
90+
);
91+
}
92+
}
2593
26-
wc_api.getAsync("products?page=2").then((val) {
27-
List products = val;
28-
print("Got " + products.length + "products received");
29-
});
3094
```
95+
96+
## Output
97+
98+
![Example code's output](Screenshot.jpg)

Screenshot.jpg

45.4 KB
Loading

intermediate.tar.tmp

-1.59 MB
Binary file not shown.

lib/woocommerce_api.dart

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ class WooCommerceAPI {
5252
var nonce = new String.fromCharCodes(codeUnits);
5353
int timestamp = (new DateTime.now().millisecondsSinceEpoch / 1000).toInt();
5454

55-
print(timestamp);
56-
print(nonce);
55+
//print(timestamp);
56+
//print(nonce);
5757

5858
var method = request_method;
5959
var path = url.split("?")[0];
@@ -96,39 +96,31 @@ class WooCommerceAPI {
9696
"&" +
9797
Uri.encodeQueryComponent(parameterString);
9898

99-
print(baseString);
99+
//print(baseString);
100100

101101
var signingKey = consumerSecret + "&" + token;
102-
print(signingKey);
102+
//print(signingKey);
103103
//print(UTF8.encode(signingKey));
104104
var hmacSha1 =
105105
new crypto.Hmac(crypto.sha1, utf8.encode(signingKey)); // HMAC-SHA1
106106
var signature = hmacSha1.convert(utf8.encode(baseString));
107107

108-
print(signature);
108+
//print(signature);
109109

110110
var finalSignature = base64Encode(signature.bytes);
111-
print(finalSignature);
111+
//print(finalSignature);
112112

113113
var requestUrl = "";
114114

115115
if (containsQueryParams == true) {
116-
print(url.split("?")[0] +
117-
"?" +
118-
parameterString +
119-
"&oauth_signature=" +
120-
Uri.encodeQueryComponent(finalSignature));
116+
//print(url.split("?")[0] + "?" + parameterString + "&oauth_signature=" + Uri.encodeQueryComponent(finalSignature));
121117
requestUrl = url.split("?")[0] +
122118
"?" +
123119
parameterString +
124120
"&oauth_signature=" +
125121
Uri.encodeQueryComponent(finalSignature);
126122
} else {
127-
print(url +
128-
"?" +
129-
parameterString +
130-
"&oauth_signature=" +
131-
Uri.encodeQueryComponent(finalSignature));
123+
//print(url + "?" + parameterString + "&oauth_signature=" + Uri.encodeQueryComponent(finalSignature));
132124
requestUrl = url +
133125
"?" +
134126
parameterString +
@@ -139,7 +131,7 @@ class WooCommerceAPI {
139131
return requestUrl;
140132
}
141133

142-
Future<Response> getAsync(String endPoint) async {
134+
Future<dynamic> getAsync(String endPoint) async {
143135

144136
var url = this._getOAuthURL("GET", endPoint);
145137

@@ -149,7 +141,7 @@ class WooCommerceAPI {
149141

150142
}
151143

152-
Future<Response> postAsync(String endPoint, Object data) async {
144+
Future<dynamic> postAsync(String endPoint, Object data) async {
153145

154146
var url = this._getOAuthURL("POST", endPoint);
155147

0 commit comments

Comments
 (0)