imsamurai/http-source

为使用Http协议的数据源提供基类

1.2.11 2015-06-02 11:21 UTC

README

Build Status Coverage Status Latest Stable Version Total Downloads Latest Unstable Version License

插件,使用HttpSource提供支持Http协议的数据源基类。基于ProLoser实现。我对HttpSource进行了一些重构,使其更接近DboSource,并移除了OAuth组件,因为我认为登录最好使用Opauth

对于现有的插件,请查看ProLoser的README。但它们不会与HttpSource一起工作。我稍后会适应这些插件。

注意

HttpSource是一个抽象类,必须由你希望支持的API扩展。如果你需要帮助创建自己的API或只想让我来做,请提交一个bug报告。添加新的API非常简单 - 查看列表

安装

步骤1:克隆或下载到Plugin/HttpSource

cd my_cake_app/app
git clone git://github.com/imsamurai/cakephp-httpsource-datasource.git Plugin/HttpSource

或如果你使用git将其作为子模块添加

cd my_cake_app
git submodule add "git://github.com/imsamurai/cakephp-httpsource-datasource.git" "app/Plugin/HttpSource"

然后更新子模块

git submodule init
git submodule update

步骤2:将你的配置添加到database.php并设置给模型

:: database.php ::
var $myapi = array(
	'datasource' => 'MyPlugin.Http/MyPlugin', // Example: 'Github.Http/Github'
        'host' => 'api.myplugin.com/v1',
        'port' => 80,
        'persistent' => false,
		'auth' => array(
			'name' => 'oauth',
			'version' => '1.0', //version 2 not tested, maybe don't work
			'oauth_consumer_key' => '--Your API Key--',
			'oauth_consumer_secret' => '--Your API Secret--'
		),
        //all other parameters that passed to config of http socket
        //...
);
:: MyModel.php ::
public $useDbConfig = 'myapi';
public $useTable = 'myapi_table';

步骤3:加载主插件和你的插件

:: bootstrap.php ::
CakePlugin::load('HttpSource', array('bootstrap' => true, 'routes' => true));
CakePlugin::load('MyPlugin');

步骤4:查询API

最好给出一个例子。我可以在运行时切换数据源,因为模型实际上是在数据库中的projects表。我倾向于从我的API查询,然后切换到默认并保存结果。

App::uses('HttpSourceModel', 'HttpSource.Model');

class Project extends HttpSourceModel {
	function findAuthedUserRepos() {
		$this->setDataSource('github');
		$projects = $this->find('all', array(
                        //used as repo name if useTable is empty, otherwise used as standart fields parameter
			'fields' => 'repos'
		));
		$this->setDataSource('default'); // if more queries are done later
		return $projects;
	}
}

配置

wiki