Blame view

sources/3rdparty/rackspace/php-opencloud/docs/userguide/Clients.md 3.51 KB
6d9380f96   Cédric Dupont   Update sources OC...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  # Clients
  
  ## Overview
  
  A Client is the object responsibile for issuing HTTP requests and receiving responses from the API. In short, it forms the core of the SDK because it controls how functionality is executed. All services depend on the client to work.
  
  Users have access to two types of client: `OpenCloud\OpenStack` and `OpenCloud\Rackspace`. The latter extends the former, meaning that much of the functionality is shared between them. The OpenStack client extends functionality from other base classes, that trace all the way back to Guzzle's root class:
  
  1. `Guzzle\Http\Client`
  2. `OpenCloud\Common\Http\Client`
  3. `OpenCloud\OpenStack`
  4. `OpenCloud\Rackspace`
  
  ## Initializing a client
  
  ### Rackspace
  
  First, you need to select the Identity endpoint you want to authenticate against. If you're using Rackspace, you can either use the UK or US endpoints. There are class constants defined for your convenience:
  
  - `OpenCloud\Rackspace::US_IDENTITY_ENDPOINT` (https://identity.api.rackspacecloud.com/v2.0)
  - `OpenCloud\Rackspace::UK_IDENTITY_ENDPOINT` (https://lon.identity.api.rackspacecloud.com/v2.0)
  
  Then you need to find your username and apiKey. Your username will be visible at the top right of the Rackspace Control panel; and your API key can be retrieved by going to Account Settings. Once this is done:
  
  ```php
  use OpenCloud\OpenStack;
  
  $client = new Rackspace(Rackspace::US_IDENTITY_ENDPOINT, array(
      'username' => 'foo',
      'apiKey'   => 'bar'
  ));
  ```
  
  ### OpenStack
  
  To initialize an OpenStack client, the process is the same:
  
  ```php
  use OpenCloud\OpenStack;
  
  $client = new OpenStack('http://identity.my-openstack.com/v2.0', array(
  	'username' => 'foo',
      'password' => 'bar'
  ));
  ```
  
  ## Authentication
  
  The Client does not automatically authenticate against the API on object creation - it waits for an API call. When this happens, it checks whether the current "token" has expired, and (re-)authenticates if necessary.
  
  You can force authentication, by calling:
  
  ```php 
  $client->authenticate();
  ```
  
  If the credentials are incorrect, a `401` error will be returned. If credentials are correct, a `200` status is returned with your Service Catalog.
  
  ## Service Catalog
  
  The Service Catalog is returned on successful authentication, and is composed of all the different API services available to the current tenant. All of this functionality is encapsulated in the `Catalog` object, which allows you greater control and interactivity.
  
  ```php
  /** @var OpenCloud\Common\Service\Catalog */
  $catalog = $client->getCatalog();
  
  // Return a list of OpenCloud\Common\Service\CatalogItem objects
  foreach ($catalog->getItems() as $catalogItem) {
  	
      $name = $catalogItem->getName();
      $type = $catalogItem->getType();
      
      if ($name == 'cloudServersOpenStack' && $type == 'compute') {
      	break;
      }
      
      // Array of OpenCloud\Common\Service\Endpoint objects
      $endpoints = $catalogItem->getEndpoints();
      foreach ($endpoints as $endpoint) {
      	if ($endpoint->getRegion() == 'DFW') {
          	echo $endpoint->getPublicUrl();
          }
      }
  }
  ```
  
  As you can see, you have access to each Service's name, type and list of endpoints. Each endpoint provides access to the specific region, along with its public and private endpoint URLs.
  
  ## Default HTTP headers
  
  To set default HTTP headers:
  
  ```php
  $client->setDefaultOption('headers/X-Custom-Header', 'FooBar');
  ```
  
  ## Other functionality
  
  For a full list of functionality provided by Guzzle, please consult the [official documentation](http://docs.guzzlephp.org/en/latest/http-client/client.html).