apache/usergrid

Apache Usergrid PHP SDK,用于与Usergrid管理和服务API进行交互的guzzle web服务客户端和服务描述符

1.0.3 2015-02-17 08:54 UTC

This package is auto-updated.

Last update: 2024-09-09 13:24:23 UTC


README

这是一个用于与Apache Usergrid管理和应用API交互的Guzzle Web服务客户端和服务描述符。

入门指南

通过将以下内容添加到您的composer.json文件中来作为composer包进行安装。

    "apache-usergrid" : "dev-master" 

现在它已上传到https://packagist.org.cn

导入类include autoload.php然后使用配置文件路径创建该类的实例,或者仅创建配置文件

use Apache\Usergrid\UsergridBootstrapper;
$config = [    
'usergrid' => [
                   'url' => 'https://api.usergrid.com',
                   'version' => '1.0.0',
                   'orgName' => '',
                   'appName' => '',
                   'manifestPath' => './src/Manifests',
                   'clientId' => '',
                   'clientSecret' => '',
                   'username' => '',
                   'password' => '',
                   /**
                    * The Auth Type setting is the Oauth 2 end point you want to get the OAuth 2
                    * Token from.  You have two options here one is 'application' the other is 'organization'
                    *
                    *  organization will get the the token from http://example.com/management/token using  client_credentials or password grant type
                    *  application will get the token from http://example.com/org_name/app_name/token using client_credentials or password grant type
                    */
                   'auth_type' => 'application',
                   /** The Grant Type to use
                    *
                    * This has to be set to one of the 2 grant types that Apache Usergrid
                    * supports which at the moment is client_credentials or password.
                    */
                   'grant_type' => 'client_credentials'
                    /**
                    * if you want to manage your own auth flow by calling the token api and setting the token your self just set this to false
                    * */
                   'enable_oauth2_plugin' => true
               ]
         ];
               
               $bootstrap = new UsergridBootstrapper($config);
               $usergrid = $bootstrap->createUsergrid();
               $collection =$usergrid->application()->getEntity(['collection' => 'shops']);

或者如果您喜欢静态外观

     $bootstrap = new UsergridBootstrapper($config);
     Usergrid::instance($bootstrap);
     $res = Usergrid::application()->EntityGet(['collection' => 'shops']);

Laravel

在Laravel中,一旦安装了composer包,就可以像下面这样发布配置文件:`php artisan config:publish apache/usergrid 这会将配置文件发布到app/config/packages/apache/usergrid/config.php,然后添加您的client_id和secret到配置文件,并在app/config.php的providers数组中设置服务提供者Apache\Usergrid\Laravel\ApacheUsergridServiceProvider并添加别名到aliases数组'Usergrid' => 'Apache\Usergrid\Laravel\Facades\Usergrid`以便可以通过外观访问类。例如,对于Laravel

    $collection = Usergrid::application()->EntityGet(['collection' => 'shops']);

它是如何工作的

您有一个名为Usergrid的主要客户端,所以如果我想调用管理API并且启用了外观,那么我会这样调用:`Usergrid::Management->OrgAppsGet();Usergrid::Application->EntityGet();`

有一个名为Manifest.php的顶级manifest文件,它只包含顶级Guzzle服务描述符属性和一个空的操作数组,所以当调用`php Usergrid::Management()->OrgAppsGet() 时,主Manifest文件的操作数组由管理Manifest文件的操作数组填充,并且它们由Usergrid Web服务客户端缓存。对Usergrid客户端的调用类似于魔术方法,因为php Usergrid::Management()-> method`调用不是由管理类或方法支持的,而是Manifest正在被选择。此外,通过使用外观,所有方法都类似于静态方法,并且与较新的PHP框架兼容,就像在使用AWS PHP SDK时在AWS工厂方法上调用enableFacades()一样。

所有响应都是Illuminate\Support\Collection类的子类,所以您可以对您的响应模型调用所有集合方法,例如:first(),get(),map(),fetch(),hasKey(),hasValue()等。这不要与Usergrid集合混淆,它类似于您的数据库表,它们也是集合对象,但如果您得到一个单个实体的响应,则它是一个包含1个计数器的集合。

错误处理

所有由Usergrid API返回的HTTP和服务器错误都附加了错误类到服务描述符,因此要处理您想要的错误,只需捕获正确的异常,例如资源未找到。

    try {
     $collection = Usergrid::Application()->GetEntity(['collection' => 'shops', 'uuid' => 'not found uuid']);
    } catch(NotFoundException $e) {
        // Handle resource not found
    }

身份验证

您可以通过将enable_oauth2_plugin配置设置设置为false来管理自己的Oauth 2流程,然后您需要调用令牌API或从其他地方获取令牌,然后将令牌设置在Usergrid实例上。默认情况下,它将为您管理Oauth2流程,我建议您将其保留为true。但如果您想自己处理,请将配置设置设置为false,然后进行如下操作。

    $res  =  Usergrid::management()->authPasswordGet($array);
    $token = $res->get('access_token');
    Usergrid::setToken($token)->users()->findById(['uuid' => '1234']);

Apache Usergrid的身份验证使用OAuth-2协议,并具有4级认证。

  • 组织令牌 顶级组织访问令牌是针对组织的。
  • 组织管理员令牌 顶级管理员用户此令牌是组织管理员用户的。
  • 应用程序令牌 每个应用程序的令牌是为应用程序的。
  • 应用程序用户令牌 用户级访问此令牌是为已登录用户的。

在使用client_credentials时,组织和应用令牌应仅用于服务器端应用程序,因为它可以完全访问所有资源。组织令牌可以访问所有应用程序并编辑组织详细信息。应用令牌可以完全访问所有应用资源。管理员用户令牌是组织管理员用户,因此它也有权访问所有应用程序和组织,最后是用户令牌,它是针对每个应用程序的用户,将有权访问该用户角色所能访问的所有资源。

因此,在配置中存在两个设置,用于控制您获取哪种类型的令牌。'auth_type' => 'application' 控制您获取组织或应用的级别,而 'grant_type' => 'client_credentials' 控制您使用的凭证类型,可以是 client_credentials 或 password,使用 client_id & client_secret 或用户名 & 密码。

结果迭代

Apache Usergrid有一个默认的分页大小为10条记录,所以如果您请求一个包含10个以上结果(实体)的集合,它将返回一个游标,您可以将其传递到下一个请求以获取下一批10个结果,所以我已经简化了这个过程。使用1.0.1及更高版本的manifest版本,您现在可以使用资源迭代器
它将一次性返回所有实体。因此,再次让SDK为您完成繁琐的工作。资源迭代器是延迟加载的,所以只有在需要数据时才会进行API调用,例如,当您第一次调用时,不会从网络请求数据,但是当您解引用数据时,就会请求第一页,第二页则不会请求,直到您请求数据,例如,如果我在foreach循环中使用它,它将只在循环遍历完当前页数后,才对下一页数据进行网络请求,如果取消foreach循环,则不会请求更多数据。资源迭代器的默认页面大小为20。

SDK将检查每个响应,看是否有游标,如果有,它将获取下一组数据,直到所有实体都被返回。

    $allDevices = Usergrid::DevicesIterator();
    foreach($allDevices as $device) {
    // this will have all devices. 
    }

新功能 - 属性

属性(关系)

Usergrid PHP SDK在应用程序服务上有一个API调用,用于获取相关模型,但它要求您进行两个API调用,一个用于获取实体,另一个用于获取关系,例如

$users = Usergrid::users()->all(['limit' => 1]);
$user_uuid = $users-entities->fetch('uuid');
$data = [ 'collection' => 'users', 'entity_id' => $user_uuid, 'relationship' => 'devices'];
$device = Usergrid::application()->GetRelationship($data);

现在,我们所需用于执行关系API调用数据已在SDK中,我们不应该强迫您进行第二次API调用并提供我们已拥有的数据。因此,现在您不再需要这样做。只需将默认关系作为响应模型的属性访问即可。

    $users = Usergrid::users()->all();
    $user = $users->entities->get(0);
    $device = $user->device;

就是这样,所以,要点是让SDK为您完成工作,如果您为自定义集合创建了自定义服务描述符和模型类,请查看User响应模型类中的deviceAttribute方法,以了解您如何将其添加到自定义集合中或向默认集合添加自定义关系属性。请记住,SDK旨在让您能够扩展Guzzle服务描述符(manifest文件)。

HTTP头和用户代理

当与http客户端和服务器系统一起工作时,您可能想要设置额外的HTTP头。我在Usergrid类中也使这变得容易,您可以在调用set方法时设置任何HTTP头或访问令牌或用户代理,它将追加新的头或替换已设置的头,因此,如果您在自己的Usergrid服务器版本上设置了某些客户分析,那么只需以流畅的方式传递您喜欢的头即可,例如

 


## Manifest Files (Guzzle & Swagger  Service Descriptors)

All the files in the manifest folder are just temp file the final Service Descriptors are versioned so
the real files are in the manifest/1.0.0 folder so as usergrid is updated new versions can be added like 1.1.0 etc.
Ill leave the other manifest file there for now but will cleanup when Apache Usergrid accepts this library.

## designs guidelines

The design of this is to make it easy to add to existing project and be able to map Model objects in your project 
to response models for example in my project I have a organization object that is saved in a mysql database and I can
call a Usergrid Api call on that model object just like using the Usergrid api class eg:
``` Usergrid::Management->putOrganization($data_array) ``` is the same as
``` Organization::put($data_array) ``` how to do this is beyond the scope of the SDK but its not hard to create 
Gateway Objects using php Traits

## PHPUnit Tests
Some unit tests require a internet connection and a usergrid install so they have been marked with a phpunit @group annotation so to run the tests without a usergrid install just pass the ```--exclude-group internet``` as a option on the command line or IDE setting
that will exclude the few tests that require access to usergrid which I've limited to only a few unit tests like testing if it can get a oauth2 access_token. 

## Javascript

There is a javascript api to go with this for example I have a site that uses the javascript sdk to login to Apache Usergrid then it send the token server side 
to be used in api calls on behalf of the logged in user. You can find this javascript sdk in my public git repo it requires one extra config setting and then you include
the javascript file in your page  It's set to works with Laravel as it posts the token to a route but it would not be hard to use else where just create uri it can post the token too. Its not part of this SDK so think
of it as a helper as some times it good to have access to both world server side calls for long running or large result sets and Ajax calls to update UI using the one login token for both type of calls.


### Contribution guidelines
* Please help if you like this.
* Writing tests
* Code review
* Code
* Manifest files