Dart 版 Planning Center API

Planning Center 是一个在线教会管理平台。

它提供了多种应用程序,用于签到、服务计划、志愿者管理、CRM、奉献等。
此外,它们还提供了一个强大的API,几乎可以访问
他们系统的所有方面,实现远程访问。

功能

此包提供了与所有 Planning Center 应用程序通过当前可用的最新 API 版本进行交互的代码(请参阅更新日志)。

目前,该包依赖于您可以在此处生成的组织级别的开发者密钥和密码。

Planning Center 开发者控制台.

TODO:将来,此库将支持 OAuth 身份验证流程。

入门

安装包

dart pub add planningcenter_api

用法

在访问 Planning Center API 之前,您需要使用 appIdsecret 初始化该库。然后,您可以通过静态成员 PlanningCenter.instance 直接访问 API 对象,并通过 PlanningCenter.instance.call(endpoint, HTTPverb, queryObject) 来执行 API 调用。

但是,一旦库初始化完成

if (PlanningCenter.initialized) ...

然后,您可以直接通过其类的静态方法访问不同的应用程序,或者通过调用现有对象上的方法来访问。

import 'dart:io'; // to exit the script faster
import 'dart:convert'; // for the pretty printing of json

import 'package:planningcenter_api/planningcenter_api.dart';

/// this is where I store my [appid] and [secret] constants
import '../secrets.dart';

/// This funciton might come in handy for you sometime :-)
String pretty(Object obj) {
  JsonEncoder encoder = JsonEncoder.withIndent('  ', (obj) {
    try {
      return obj.toJson();
    } catch (_) {
      return obj.toString();
    }
  });
  return encoder.convert(obj);
}

/// here's the real example code
void main() async {
  // begin by initializing the PlanningCenter api
  PlanningCenter.init(appid, secret);

  // Now, all classes beginning with Pco are available for use

  /// Get the service types on the default organization (defaults to grabbing 25)
  /// will return List<PcoServicesServiceType>
  var serviceTypes = await PcoServicesServiceType.getMany();
  if (serviceTypes.isNotEmpty) {
    var service = serviceTypes.first;
    print('Found Service Type: ${service.name}');

    /// most class instances have methods allowing you to fetch related items
    /// this time, we also are using a query object to request plands in descending order
    /// of their sort date
    var plans = await service.getPlans(query: PlanningCenterApiQuery(order: '-sort_date'));
    if (plans.isNotEmpty) {
      var plan = plans.first;
      print('Found Plan: ${plan.seriesTitle} - ${plan.title} - ${plan.lastTimeAt}');
      var items = await plans.first.getItems();
      for (var item in items) {
        print('Plan Item: ${item.title}\n${item.description}\n');
        if (item.title == 'CHANGE ME') {
          print('attempting to update this item');
          item.title = 'CHANGED';
          var result = await item.save();
          print(result ? 'successful' : 'not successful');
        }
      }
    }
  }

  // to call the API directly, you can do this.
  var res = await PlanningCenter.instance.call('/services/v2/songs');
  print(pretty(res));
  exit(0);
}

目前尚未实现的功能

  • OAuth 身份验证
  • 支持 API “actions”
  • 支持“include”查询参数

GitHub

查看 Github