Line data Source code
1 : import 'dart:async'; 2 : 3 : import 'package:contentstack/client.dart'; 4 : 5 : /// This call fetches the latest version of a specific 6 : /// `asset` of a particular stack. 7 : /// Read more about 8 : /// Learn more about [Assets](https://www.contentstack.com/docs/developers/apis/content-delivery-api/#get-a-single-asset) 9 : /// 10 : class Asset { 11 : final HttpClient _client; 12 : final String _uid; 13 : String _urlPath; 14 : 15 : final Map<String, String> assetParameter = <String, String>{}; 16 : 17 : /// * [_uid] assetUid: 18 : /// Enter the unique ID of the asset of which you wish to retrieve 19 : /// the details. {Example: blt19c34e5374418484} 20 : /// ```dart 21 : /// var stack = contentstack.Stack(apiKey, deliveryToken, environment); 22 : /// final asset = stack.asset(asset_uid); 23 : /// asset.fetch<AssetModel, void>().then((response) { 24 : /// print(response); 25 : /// }).catchError((error) { 26 : /// print(error['error_code']); 27 1 : /// }); 28 5 : /// 29 5 : Asset(this._uid, [this._client]) { 30 : assetParameter['environment'] = _client.stackHeaders['environment']; 31 : _urlPath = '/${_client.stack.apiVersion}/assets'; 32 : } 33 : 34 : /// 35 : /// Enter the name of the [environment] if you wish to retrieve 36 : /// the assets published in a particular environment. 37 : /// [environment] required 38 : /// 39 : /// ```dart 40 : /// var stack = contentstack.Stack(apiKey, deliveryToken, environment); 41 : /// final asset = stack.asset(asset_uid)..environment('development'); 42 : /// asset.fetch<AssetModel, void>().then((response) { 43 : /// print(response); 44 : /// }).catchError((error) { 45 1 : /// print(error['error_code']); 46 2 : /// }); 47 : void environment(String environment) { 48 : assetParameter['environment'] = environment; 49 : } 50 : 51 : /// It fetch single asset data on the basis of the asset uid. 52 : /// 53 : /// ```dart 54 : /// var stack = contentstack.Stack(apiKey, deliveryToken, environment); 55 : /// asset.fetch<AssetModel, void>().then((response) { 56 : /// print(response); 57 : /// }).catchError((error) { 58 : /// print(error['error_code']); 59 : /// }); 60 : Future<T> fetch<T, K>() { 61 : if (_uid == null || _uid.isEmpty) { 62 : throw Exception('Provide asset uid to fetch single entry'); 63 : } 64 1 : final uri = 65 3 : Uri.https(_client.stack.endpoint, '$_urlPath/$_uid', assetParameter); 66 : return _client.sendRequest<T, K>(uri); 67 : } 68 : 69 : /// 70 : /// include the dimensions (height and width) of the image in the response. 71 : /// Supported image types: JPG, GIF, PNG, WebP, BMP, TIFF, SVG, and PSD. 72 : /// 73 : /// ```dart 74 : /// var stack = contentstack.Stack(apiKey, deliveryToken, environment); 75 : /// final asset = stack.asset(asset_uid)..includeDimension('development'); 76 : /// asset.fetch<AssetModel, void>().then((response) { 77 : /// print(response); 78 : /// }).catchError((error) { 79 : /// print(error['error_code']); 80 1 : /// }); 81 2 : void includeDimension() { 82 : assetParameter['include_dimension'] = 'true'; 83 : } 84 : 85 : /// 86 : /// Retrieve the published content of the fallback locale if an entry is not 87 : /// localized in specified locale. 88 : /// 89 : /// ```dart 90 : /// var stack = contentstack.Stack(apiKey, deliveryToken, environment); 91 : /// final asset = stack.asset(asset_uid)..includeFallback(); 92 : /// asset.fetch<AssetModel, void>().then((response) { 93 : /// print(response); 94 : /// }).catchError((error) { 95 : /// print(error['error_code']); 96 0 : /// }); 97 0 : void includeFallback() { 98 : assetParameter['include_fallback'] = 'true'; 99 : } 100 : 101 : /// 102 : /// Specify the version number of the asset that you wish to retrieve. 103 : /// If the version is not specified, 104 : /// the details of the latest version will be retrieved. 105 : /// To retrieve a specific version, keep the environment parameter blank. 106 : /// [version] required 107 : /// 108 : /// ```dart 109 1 : /// var stack = contentstack.Stack(apiKey, deliveryToken, environment); 110 3 : /// final asset = stack.asset(asset_uid)..version(2); 111 0 : /// asset.fetch<AssetModel, void>().then((response) { 112 : /// print(response); 113 : /// }).catchError((error) { 114 8 : /// print(error['error_code']); 115 2 : /// }); 116 : void version(int version) { 117 : assetParameter['version'] = version.toString(); 118 : } 119 : }