contentchef_dart 1.0.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 58

ContentChef Dart/Flutter SDK #

Welcome to ContentChef API-First CMS's Dart/Flutter SDK.

How to use ContentChef client #

Dart/Flutter

Create your ContentChef instance like this:

    import 'package:contentchef_dart/contentchef_dart.dart';
    
    void main() {

        var configuration = Configuration(spaceId: SPACE_ID);
        var contentChef = (configuration: configuration);

    }

SPACE_ID can be retrieved from your ContentChef's dashboard.

You can now use your contentChef instance to get the channel you want to use to retrieve info: you have two channels, the OnlineChannel and the PreviewChannel.

With the OnlineChannel you can retrieve contents which are in live state and which are actually visible, while with the PreviewChannel you can retrieve contents which are in in both stage and live states and even contents that are not visible in the current date.

Both the OnlineChannel and the PreviewChannel have two methods which are getContent() and searchContents()

You can use the getContent() method to collect a specific content by its own publicId, for example to retrieve a single post from your blog, a single image from a gallery or a set of articles from your featured articles list. Otherwise you can use the searchContents() method to find contents with multiple matching criteria, like content definition name, publishing dates and more.

Examples #

How to retrieve a content from your OnlineChannel

SPACE_ID can be retrieved from your ContentChef's dashboard.

ONLINE_API_KEY can be retrieved from your ContentChef space homepage.

PUBLISHING_CHANNEL can be retrieved from your ContentChef's dashboard.


    /// Retrieve the *content-chef-site* content from the live status:

    void main() {

        var configuration = Configuration(spaceId: SPACE_ID);
        
        var contentChef = (configuration: configuration);

        var getContentFilters = GetContentFilters(publicId: 'content-chef-site');
    
        var onlineChannel = contentChef.getOnlineChannel(apiKey: ONLINE_API_KEY, publishingChannel: PUBLISHING_CHANNEL);

        try {
            var result = await onlineChannel.getContent(filters: getContentFilters);
            print(jsonEncode(result));
        } catch(e) { 
            print(jsonEncode(e));
        }

    }

How to retrieve a content in the future from your PreviewChannel

SPACE_ID can be retrieved from your ContentChef's dashboard.

PREVIEW_API_KEY can be retrieved from your ContentChef space homepage.

PUBLISHING_CHANNEL can be retrieved from your ContentChef's dashboard.


    // Preview the content *content-chef-site* content in a given future date from the live status
    // (i.e. if you want to see the content that will be online in 10 days)
    // **targetDateResolver** is used only in the PreviewChannel
    // targetDateResolver always return a TargetDate as a `TargetDateDef` so that you can change your date dynamically without caring to create a new ContentChef instance
    
    void main() {

        var configuration = Configuration(spaceId: SPACE_ID);
        
        var aDate10DaysInFuture = DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day + 10);
        vart targetDateResolver = TargetDateResolver(targetDateSource: aDate10DaysInFuture.toIso8601String());

        var contentChef = (configuration: configuration, targetDateResolver: targetDateResolver);

        var getContentFilters = GetContentFilters(publicId: 'content-chef-site');
        
        var previewChannel = contentChef.previewChannel(apiKey: PREVIEW_API_KEY, publishingChannel: PUBLISHING_CHANNEL, status: PublishingStatus.live);
        
        try {
            var result = await previewChannel.getContent(filters: getContentFilters);
            print(jsonEncode(result));
        } catch(e) { 
            print(jsonEncode(e));
        }
    }
      

How to Search for all the contents of a specific definition

SPACE_ID can be retrieved from your ContentChef's dashboard.

ONLINE_API_KEY can be retrieved from your ContentChef space homepage.

PUBLISHING_CHANNEL can be retrieved from your ContentChef's dashboard.

 
    // Search for 10 the contents created using the definition *content-chef-site*
   
    void main() {

        var configuration = Configuration(spaceId: SPACE_ID);

        var searchContentFilters = SearchContentsFilters(skip: 0, take: 10, contentDefinition: ['top-sites']);

        var contentChef = (configuration: configuration);
        
        var onlineChannel = contentChef.onlineChannel(apiKey: ONLINE_API_KEY, publishingChannel: PUBLISHING_CHANNEL);
        
        try {
            var result = await previewChannel.searchContents(filters: searchContentFilters);
            print(jsonEncode(result));
        } catch(e) { 
            print(jsonEncode(e))
        }
    }

How to use Media helper #

If you request a content that contain a media field, it will return its publicId as value of the field. In order to retrieve the media public url, you will need to use the Media helper class.

Examples #

How to retrieve a media without transformations


    void main() {
    
        var mediaHelper = Media();
        var publicUrl = mediaHelper.getUrl(publicId: 'your-field-media-publicId')

        print(publicUrl);

    }

How to retrieve a media without transformations

    
    // If you desire to have transformed media you can specify some parameters with the transformation parameter while getting your media publicUrl.
    // The possible transformations are: mediaHeight, mediaWidth, autoFormat and mediaFormat, for more information refer to `MediaTransformations` class

    void main() {
    
        var mediaHelper = Media();
        var transformations = MediaTransformations(mediaWidth: 1000, mediaHeight: 1000);
        var publicUrl = mediaHelper.getUrl(publicId: 'your-field-media-publicId', transformations: transformations);

        print(publicUrl);

    }

Installation #

  1. Depend on it

Add this to your package's pubspec.yaml file:


    dependencies:
        contentchef_dart: ^1.0.3

  1. Install it

You can install packages from the command line:

with pub:


    $ pub get

with Flutter:


    $ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

  1. Import it

Now in your Dart code, you can use:


import 'package:contentchef_dart/contentchef_dart.dart';

1.0.3 #

  • add fromJson param in GetContent and SearchContents methods to help the user to serialize contents payload

1.0.2 #

  • Change usage Uri.https in favour of Uri to better handle search query parameters as Iterable

1.0.1 #

  • Add github repository link

1.0.0 #

  • Initial version, created by ContentChef team

example/contentchef_dart_example.dart

import 'dart:convert';

import 'package:contentchef_dart/contentchef_dart.dart';

/// This class is used to map a content of type "top-site"
///
class TopSiteContent {
  String title;
  String description;
  String url;
  String image;

  TopSiteContent({this.title, this.description, this.url, this.image});

  static TopSiteContent fromJson(Map<String, dynamic> json) {
    return TopSiteContent(
        title: json['title'] as String,
        description: json['description'] as String,
        url: json['description'] as String,
        image: json['image'] as String
    );
  }

  // Method needed to deserialize TopSiteContent class in JSON object
  Map<String, dynamic> toJson() => {
    'title': title,
    'description': description,
    'url': url,
    'image': image
  };
}

void main() async {
  var configuration = Configuration(spaceId: 'yours-spaceId');

  var contentChef = ContentChef(configuration: configuration);

  try {
    var result = await contentChef
      .getPreviewChannel(apiKey: 'test-api-key', status: PublishingStatus.stage, publishingChannel: 'your-channel-mnemonicId')
      .searchContents<TopSiteContent>(
        filters: SearchContentsFilters(skip: 0, take: 10, contentDefinition: ['top-site']),
        fromJson: TopSiteContent.fromJson);
      print(jsonEncode(result));
  } catch (e) {
    print(e);
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  contentchef_dart: ^1.0.3

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:contentchef_dart/contentchef_dart.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
16
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
58
Learn more about scoring.

We analyzed this package on May 30, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.2
  • pana: 0.13.8-dev

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
http ^0.12.0+4 0.12.1
meta ^1.1.8 1.1.8
Transitive dependencies
charcode 1.1.3
collection 1.14.12
http_parser 3.1.4
path 1.7.0
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
mockito ^4.1.1
pedantic ^1.8.0 1.9.0
test ^1.6.0