Skip to content

Commit e3eecfd

Browse files
authored
Dart (#49)
* Dart release 22.3
1 parent f76a893 commit e3eecfd

22 files changed

+860
-0
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@
1919
[submodule "submodules/android"]
2020
path = submodules/android
2121
url = ../aspose-barcode-cloud-android
22+
[submodule "submodules/dart"]
23+
path = submodules/dart
24+
url = ../aspose-barcode-cloud-dart
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Aspose.BarCode Cloud SDK for Dart
2+
3+
- API version: {{appVersion}}
4+
- SDK version: {{pubVersion}}
5+
6+
{{pubDescription}}
7+
8+
## Demo applications
9+
10+
[Generate Barcode](https://products.aspose.app/barcode/generate) | [Recognize Barcode](https://products.aspose.app/barcode/recognize) | [Embed Barcode](https://products.aspose.app/barcode/embed)
11+
:---: | :---: | :---:
12+
[![Generate](https://products.aspose.app/barcode/generate/img/aspose_generate-app-48.png)](https://products.aspose.app/barcode/generate) | [![Recognize](https://products.aspose.app/barcode/recognize/img/aspose_recognize-app-48.png)](https://products.aspose.app/barcode/recognize) | [![Embed](https://products.aspose.app/barcode/embed/img/aspose_embed-app-48.png)](https://products.aspose.app/barcode/embed)
13+
14+
[Aspose.BarCode for Cloud](https://products.aspose.cloud/barcode/) is a REST API for Linear, 2D and postal barcode generation and recognition in the cloud. API recognizes and generates barcode images in a variety of formats. Barcode REST API allows to specify barcode image attributes like image width, height, border style and output image format in order to customize the generation process. Developers can also specify the barcode type and text attributes such as text location and font styles in order to suit the application requirements.
15+
16+
This repository contains Aspose.BarCode Cloud SDK for Dart source code. This SDK allows you to work with Aspose.BarCode for Cloud REST APIs in your Dart or Flutter applications quickly and easily.
17+
18+
To use these SDKs, you will need Client Id and Client Secret which can be looked up at [Aspose Cloud Dashboard](https://dashboard.aspose.cloud/applications) (free registration in Aspose Cloud is required for this).
19+
20+
## Prerequisites
21+
22+
To use Aspose.BarCode Cloud SDK for Dart you need to register an account with [Aspose Cloud](https://www.aspose.cloud) and lookup/create Client Secret and SID at [Cloud Dashboard](https://dashboard.aspose.cloud/applications). There is a free quota available. For more details, see [Aspose Cloud Pricing](https://purchase.aspose.cloud/pricing).
23+
24+
## Requirements
25+
26+
Dart 2.12.0 or later
27+
28+
## Installation & Usage
29+
Add this dependency to your *pubspec.yaml*:
30+
31+
```yaml
32+
dependencies:
33+
{{pubName}}: {{pubVersion}}
34+
```
35+
36+
## Sample usage
37+
38+
### Generate an image with barcode and then recognize it
39+
40+
The examples below show how you can generate QR barcode and save it into a local file and then recognize using **{{pubName}}**:
41+
42+
```dart
43+
import 'package:aspose_barcode_cloud/api.dart' as barcode;
44+
45+
import 'dart:typed_data';
46+
import 'dart:io';
47+
48+
import 'package:http/http.dart';
49+
50+
Future<void> main() async {
51+
const fileName = "qr.png";
52+
// Setup
53+
var apiClient = barcode.ApiClient(
54+
clientId: "Client Id from https://dashboard.aspose.cloud/applications",
55+
clientSecret:
56+
"Client Secret from https://dashboard.aspose.cloud/applications",
57+
);
58+
var api = barcode.BarcodeApi(apiClient);
59+
60+
// Generate image with barcode
61+
Uint8List? generated =
62+
await api.getBarcodeGenerate("QR", "text", textLocation: "None");
63+
// Save generated image to file
64+
await new File(fileName).writeAsBytes(generated!);
65+
print("Generated image saved to " + fileName);
66+
67+
// Recognize generated image
68+
var formFile = MultipartFile.fromBytes("image", generated.toList(),
69+
filename: "barcode.png");
70+
barcode.BarcodeResponseList? recognized =
71+
await api.postBarcodeRecognizeFromUrlOrContent(image: formFile);
72+
73+
print("Recognized Type: " + recognized!.barcodes![0].type!);
74+
print("Recognized Value: " + recognized.barcodes![0].barcodeValue!);
75+
}
76+
```
77+
78+
## Dependencies
79+
80+
- http: '>=0.13.0 <0.14.0'
81+
82+
## Licensing
83+
84+
All Aspose.BarCode for Cloud SDKs, helper scripts and templates are licensed under [MIT License](LICENSE).
85+
86+
## Resources
87+
88+
- [**Website**](https://www.aspose.cloud)
89+
- [**Product Home**](https://products.aspose.cloud/barcode/)
90+
- [**Documentation**](https://docs.aspose.cloud/barcode/)
91+
- [**Free Support Forum**](https://forum.aspose.cloud/c/barcode)
92+
- [**Paid Support Helpdesk**](https://helpdesk.aspose.cloud/)
93+
- [**Blog**](https://blog.aspose.cloud/category/barcode/)
94+
95+
## Documentation for API Endpoints
96+
97+
All URIs are relative to *<{{{basePath}}}>*
98+
99+
Class | Method | HTTP request | Description
100+
------------ | ------------- | ------------- | -------------
101+
{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}*{{classname}}* | [**{{operationId}}**](doc/api/{{classname}}.md#{{operationIdLowerCase}}) | **{{httpMethod}}** {{path}} | {{#summary}}{{summary}}{{/summary}}
102+
{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}
103+
## Documentation For Models
104+
105+
{{#models}}{{#model}}- [{{{classname}}}](doc/models/{{{classname}}}.md)
106+
{{/model}}{{/models}}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
part of {{pubName}}.api;
2+
3+
{{#operations}}
4+
5+
6+
class {{classname}} {
7+
final ApiClient apiClient;
8+
9+
{{classname}}(this.apiClient) {}
10+
11+
{{#operation}}
12+
/// {{summary}}
13+
///
14+
/// {{notes}}
15+
{{#isDeprecated}}
16+
@deprecated
17+
{{/isDeprecated}}
18+
{{#returnType}}Future<{{#isResponseFile}}Uint8List{{/isResponseFile}}{{^isResponseFile}}{{{returnType}}}{{/isResponseFile}}?> {{/returnType}}{{^returnType}}Future {{/returnType}}{{nickname}}({{#allParams}}{{#required}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}}{ {{#allParams}}{{^required}}{{{dataType}}}? {{paramName}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}} }{{/hasOptionalParams}}) async {
19+
Object? postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
20+
21+
// create path and map variables
22+
String requestPath = "{{{path}}}".replaceAll("{format}","json"){{#pathParams}}.replaceAll("{" + "{{baseName}}" + "}", {{{paramName}}}.toString()){{/pathParams}};
23+
24+
// query params
25+
List<QueryParam> queryParams = [];
26+
Map<String, String> headerParams = {};
27+
Map<String, String> formParams = {};
28+
{{#queryParams}}
29+
{{^required}}
30+
if({{paramName}} != null) {
31+
{{/required}}
32+
queryParams.addAll(_convertParametersForCollectionFormat("{{collectionFormat}}", "{{baseName}}", {{paramName}}));
33+
{{^required}}
34+
}
35+
{{/required}}
36+
{{/queryParams}}
37+
{{#headerParams}}headerParams["{{baseName}}"] = {{paramName}};
38+
{{/headerParams}}
39+
40+
List<String> contentTypes = [{{#consumes}}"{{{mediaType}}}"{{#hasMore}},{{/hasMore}}{{/consumes}}];
41+
42+
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
43+
List<String> authNames = [{{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}}];
44+
45+
if(contentType.startsWith("multipart/form-data")) {
46+
bool hasFields = false;
47+
MultipartRequest? mp;
48+
{{#formParams}}{{#notFile}}
49+
if ({{paramName}} != null) {
50+
hasFields = true;
51+
mp.fields['{{baseName}}'] = parameterToString({{paramName}});
52+
}
53+
{{/notFile}}{{#isFile}}
54+
mp = new MultipartRequest('{{httpMethod}}', Uri.parse(requestPath));
55+
if ({{paramName}} != null) {
56+
hasFields = true;
57+
mp.fields['{{baseName}}'] = {{paramName}}.field;
58+
mp.files.add({{paramName}});
59+
}
60+
61+
{{/isFile}}{{/formParams}}
62+
if (hasFields) postBody = mp;
63+
}
64+
else {
65+
{{#formParams}}{{#notFile}}if ({{paramName}} != null)
66+
formParams['{{baseName}}'] = parameterToString({{paramName}});{{/notFile}}
67+
{{/formParams}}
68+
}
69+
70+
var response = await apiClient.invokeAPI(requestPath,
71+
'{{httpMethod}}',
72+
queryParams,
73+
postBody,
74+
headerParams,
75+
formParams,
76+
contentType,
77+
authNames);
78+
79+
if(response.statusCode >= 400) {
80+
throw new ApiException(response.statusCode, response.body);
81+
} else if(response.body != null) {
82+
return
83+
{{#isListContainer}}
84+
{{#returnType}}(apiClient.deserialize(response.body, '{{{returnType}}}') as List).map((item) => item as {{returnBaseType}}).toList();{{/returnType}}
85+
{{/isListContainer}}
86+
{{^isListContainer}}
87+
{{#isMapContainer}}
88+
{{#returnType}}new {{{returnType}}}.from(apiClient.deserialize(response.body, '{{{returnType}}}')) {{/returnType}};
89+
{{/isMapContainer}}
90+
{{^isMapContainer}}
91+
{{#isResponseFile}}
92+
response.bodyBytes;
93+
{{/isResponseFile}}
94+
{{^isResponseFile}}
95+
{{#returnType}}apiClient.deserialize(response.body, '{{{returnType}}}') as {{{returnType}}} {{/returnType}};
96+
{{/isResponseFile}}
97+
{{/isMapContainer}}
98+
{{/isListContainer}}
99+
} else {
100+
return {{#returnType}}null{{/returnType}};
101+
}
102+
}
103+
{{/operation}}
104+
}
105+
{{/operations}}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
part of {{pubName}}.api;
2+
3+
class QueryParam {
4+
String name;
5+
String value;
6+
7+
QueryParam(this.name, this.value);
8+
}
9+
10+
class ApiClient {
11+
12+
String basePath;
13+
var client = new {{#browserClient}}Browser{{/browserClient}}Client();
14+
15+
Map<String, String> _defaultHeaderMap = {};
16+
late Authentication _authentication;
17+
18+
final _RegList = new RegExp(r'^List<(.*)>$');
19+
final _RegMap = new RegExp(r'^Map<String,(.*)>$');
20+
21+
ApiClient(
22+
{String? clientId,
23+
String? clientSecret,
24+
String? accessToken,
25+
tokenUrl: "https://api.aspose.cloud/connect/token",
26+
this.basePath: "https://api.aspose.cloud/v3.0"}) {
27+
_authentication = new OAuth(
28+
clientId: clientId,
29+
clientSecret: clientSecret,
30+
accessToken: accessToken,
31+
tokenUrl: tokenUrl);
32+
}
33+
34+
void addDefaultHeader(String key, String value) {
35+
_defaultHeaderMap[key] = value;
36+
}
37+
38+
dynamic _deserialize(dynamic value, String targetType) {
39+
try {
40+
switch (targetType) {
41+
case 'String':
42+
return '$value';
43+
case 'int':
44+
return value is int ? value : int.parse('$value');
45+
case 'bool':
46+
return value is bool ? value : '$value'.toLowerCase() == 'true';
47+
case 'double':
48+
return value is double ? value : double.parse('$value');
49+
{{#models}}
50+
{{#model}}
51+
case '{{classname}}':
52+
{{#isEnum}}
53+
return new {{classname}}.fromJson(value);
54+
{{/isEnum}}
55+
{{^isEnum}}
56+
return new {{classname}}.fromJson(value);
57+
{{/isEnum}}
58+
{{/model}}
59+
{{/models}}
60+
default:
61+
{
62+
RegExpMatch? match;
63+
if (value is List &&
64+
(match = _RegList.firstMatch(targetType)) != null) {
65+
var newTargetType = match![1];
66+
return value.map((v) => _deserialize(v, newTargetType!)).toList();
67+
} else if (value is Map &&
68+
(match = _RegMap.firstMatch(targetType)) != null) {
69+
var newTargetType = match![1];
70+
return new Map.fromIterables(value.keys,
71+
value.values.map((v) => _deserialize(v, newTargetType!)));
72+
}
73+
}
74+
}
75+
} on Exception catch (e, stack) {
76+
throw new ApiException.withInner(0, 'Exception during deserialization.', e, stack);
77+
}
78+
throw new ApiException(0, 'Could not find a suitable class for deserialization');
79+
}
80+
81+
dynamic deserialize(String jsonVal, String targetType) {
82+
// Remove all spaces. Necessary for reg expressions as well.
83+
targetType = targetType.replaceAll(' ', '');
84+
85+
if (targetType == 'String') return jsonVal;
86+
87+
var decodedJson = json.decode(jsonVal);
88+
return _deserialize(decodedJson, targetType);
89+
}
90+
91+
String serialize(Object? obj) {
92+
String serialized = '';
93+
if (obj == null) {
94+
serialized = '';
95+
} else {
96+
serialized = json.encode(obj);
97+
}
98+
return serialized;
99+
}
100+
101+
// We don't use a Map<String, String> for queryParams.
102+
// If collectionFormat is 'multi' a key might appear multiple times.
103+
Future<Response> invokeAPI(String path,
104+
String method,
105+
List<QueryParam> queryParams,
106+
Object? body,
107+
Map<String, String> headerParams,
108+
Map<String, String> formParams,
109+
String contentType,
110+
List<String> authNames) async {
111+
112+
await _updateParamsForAuth(queryParams, headerParams);
113+
114+
var ps = queryParams.map((p) => '${p.name}=${p.value}');
115+
String queryString = ps.isNotEmpty ?
116+
'?' + ps.join('&') :
117+
'';
118+
119+
String url = basePath + path + queryString;
120+
121+
headerParams.addAll(_defaultHeaderMap);
122+
headerParams['Content-Type'] = contentType;
123+
124+
if(body is MultipartRequest) {
125+
var request = new MultipartRequest(method, Uri.parse(url));
126+
request.fields.addAll(body.fields);
127+
request.files.addAll(body.files);
128+
request.headers.addAll(body.headers);
129+
request.headers.addAll(headerParams);
130+
var response = await client.send(request);
131+
return Response.fromStream(response);
132+
} else {
133+
var msgBody = contentType == "application/x-www-form-urlencoded" ? formParams : serialize(body);
134+
switch(method) {
135+
case "POST":
136+
return client.post(Uri.parse(url), headers: headerParams, body: msgBody);
137+
case "PUT":
138+
return client.put(Uri.parse(url), headers: headerParams, body: msgBody);
139+
case "DELETE":
140+
return client.delete(Uri.parse(url), headers: headerParams);
141+
case "PATCH":
142+
return client.patch(Uri.parse(url), headers: headerParams, body: msgBody);
143+
default:
144+
return client.get(Uri.parse(url), headers: headerParams);
145+
}
146+
}
147+
}
148+
149+
Future<void> _updateParamsForAuth(
150+
List<QueryParam> queryParams, Map<String, String> headerParams) async {
151+
await _authentication.applyToParams(queryParams, headerParams);
152+
}
153+
}

0 commit comments

Comments
 (0)