Skip to content

Commit 155dacb

Browse files
authored
Merge pull request #120 from jolicode/better-documentation
Improve the documentation
2 parents 8c2a4b3 + 6309725 commit 155dacb

File tree

14 files changed

+273
-105
lines changed

14 files changed

+273
-105
lines changed

.php_cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ return PhpCsFixer\Config::create()
3636
))
3737
->setFinder(PhpCsFixer\Finder::create()
3838
->in(__DIR__)
39-
->exclude('doc')
39+
->exclude('docs')
4040
->exclude('ci_generated')
4141
)
4242
;

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* Add a new Client to ease iteration over Slack paginated endpoints thanks to `iterateXxxx` methods (where `xxxx` is the endpoint name to iterate over)
66
* **Specification override** Add missing `locale` for `objs_conversation` model
7+
* Improve the documentation
78

89
## 4.1.1 (2021-03-16)
910

README.md

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
# A PHP client for Slack's API
22

3-
There is a bunch of PHP clients for Slack. But some are not up to date or miss
4-
features, some only cover a small part of the API and most are simply no longer
5-
maintained.
3+
There is a bunch of existing PHP clients for Slack. But some are not up to date
4+
or miss features, some only cover a small part of the API and most are simply
5+
no longer maintained.
66

77
This SDK is generated automatically with [JanePHP](https://github.com/janephp/janephp)
88
from the [official Slack API specs](https://github.com/slackapi/slack-api-specs).
99

10-
It provides a **full object oriented interface** for all the endpoints, requests and responses of the Slack Web API.
10+
It also provides a **full object oriented interface** for all the endpoints,
11+
requests and responses of the Slack Web API.
1112

1213
## Installation
1314

1415
This library is built atop of [PSR-7](https://www.php-fig.org/psr/psr-7/) and
1516
[PSR-18](https://www.php-fig.org/psr/psr-18/). So you will need to install some
16-
implementations for those interfaces.
17+
implementations for those standard interfaces.
1718

1819
If no PSR-18 client or PSR-7 message factory is available yet in your project
1920
or you don't know or don't care which one to use, just install some default:
@@ -28,35 +29,18 @@ You can now install the Slack client:
2829
composer require jolicode/slack-php-api
2930
```
3031

31-
## Usage
32-
33-
First, you need to retrieve a token from Slack.
34-
35-
Checkout Slack's documentation about [all different kind of tokens](https://api.slack.com/authentication/token-types).
36-
A good starting point is the [Authentication Basics documentation](https://api.slack.com/authentication/basics).
37-
38-
Then, use the factory that is provided to create the client:
32+
## Quick start
3933

4034
```php
4135
// $client contains all the methods to interact with the API
42-
$client = JoliCode\Slack\ClientFactory::create($yourToken);
36+
$client = JoliCode\Slack\ClientFactory::create($yourSlackToken);
4337

4438
$user = $client->usersInfo(['user' => 'U123AZER'])->getUser();
45-
46-
dump($user);
4739
```
4840

49-
<p align="center">
50-
<img src="doc/images/model-sample.png" alt="Sample user object" />
51-
<p>
52-
53-
Want more example or documentation? See the [full documentation here](doc/index.md).
54-
55-
## Troubleshoots
41+
## Documentation
5642

57-
Got some problems using this library? Need a missing feature?
58-
Do not hesitate to [open an issue](https://github.com/jolicode/slack-php-api/issues)
59-
and share it with us.
43+
Want more documentation or examples? See the [full documentation here](https://jolicode.github.io/slack-php-api/).
6044

6145
## Further documentation
6246

doc/index.md

Lines changed: 0 additions & 78 deletions
This file was deleted.

docs/1-get-started.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Getting started
2+
3+
## Installation
4+
5+
This library is built atop of [PSR-7](https://www.php-fig.org/psr/psr-7/) and
6+
[PSR-18](https://www.php-fig.org/psr/psr-18/). So you will need to install some
7+
implementations for those standard interfaces.
8+
9+
If no PSR-18 client or PSR-7 message factory is available yet in your project
10+
or you don't know or don't care which one to use, just install some default:
11+
12+
```bash
13+
composer require symfony/http-client nyholm/psr7
14+
```
15+
16+
You can now install the Slack client:
17+
18+
```bash
19+
composer require jolicode/slack-php-api
20+
```
21+
22+
## Slack token
23+
24+
Before you can use this client, you need to retrieve a token from Slack.
25+
26+
Checkout Slack's documentation about [all different kind of tokens](https://api.slack.com/authentication/token-types).
27+
A good starting point is the [Authentication Basics documentation](https://api.slack.com/authentication/basics).
28+
29+
## Quick start
30+
31+
```php
32+
// $client contains all the methods to interact with the API
33+
$client = JoliCode\Slack\ClientFactory::create($yourSlackToken);
34+
35+
$user = $client->usersInfo(['user' => 'U123AZER'])->getUser();
36+
```
37+
38+
***
39+
40+
Read more:
41+
- Next page: [Usage](2-usage.md)
42+
- Previous page: [Introduction](index.md)

docs/2-usage.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Usage
2+
3+
## Basic usage of the client
4+
5+
Use the `ClientFactory` to generate a `Client` configured with your token:
6+
7+
```php
8+
<?php
9+
10+
use JoliCode\Slack\Api\Client;
11+
use JoliCode\Slack\ClientFactory;
12+
13+
/** @var Client $client */
14+
$client = ClientFactory::create($yourSlackToken);
15+
```
16+
17+
The client contains all the methods to communicate with Slack API. Checkout its
18+
PHPdoc to know which option you can provide to each method:
19+
20+
```php
21+
<?php
22+
//...
23+
24+
$user = $client->usersInfo(['user' => 'U123AZER'])->getUser();
25+
26+
dump($user);
27+
```
28+
29+
Here is what a User object looks like:
30+
31+
<p align="center">
32+
<img src="images/model-sample.png" alt="Sample user object" />
33+
<p>
34+
35+
To summarize - the whole SDK is built with objects. Each endpoints, responses
36+
and models have their own PHP classes with correct typehinting and PHPDoc.
37+
38+
Along with [official Slack documentation](https://api.slack.com/methods), your
39+
favorite PHP editor should be a good ally to explore the whole API.
40+
41+
## Iterating over pagination
42+
43+
Some Slack API endpoints use a cursor based pagination.
44+
45+
In addition to classical methods, our client provides additional virtual methods
46+
to ease the iteration over those endpoints. Those methods can be called by
47+
prefixing the classic method by `iterate`. Here is an example with `usersList()`:
48+
49+
```php
50+
$userNames = [];
51+
52+
/** @var JoliCode\Slack\Api\Model\ObjsUser $user */
53+
for ($client->iterateUsersList() as $user) {
54+
$userNames[] = $user->getName();
55+
}
56+
```
57+
58+
The previous code hide the complexity of working with pagination's cursor. Here
59+
is the equivalent of the previous code:
60+
61+
```php
62+
$userNames = [];
63+
$cursor = '';
64+
65+
do {
66+
$response = $client->usersList([
67+
'limit' => 200,
68+
'cursor' => $cursor,
69+
]);
70+
71+
/** @var JoliCode\Slack\Api\Model\ObjsUser $user */
72+
foreach ($response->getUsers() as $user) {
73+
$userNames[] = $user->getName();
74+
}
75+
76+
$cursor = $response->getResponseMetadata() ? $response->getResponseMetadata()->getNextCursor() : '';
77+
} while (!empty($cursor));
78+
```
79+
80+
>Note: the virtual methods are not yet documented with PHPDoc so your IDE will
81+
not autocomplete them.
82+
83+
## Concrete examples
84+
85+
Here are some real-life examples of interacting with the SDK:
86+
87+
- [Posting a message in a Slack channel](https://github.com/jolicode/slack-php-api/tree/main/docs/examples/posting-message.php);
88+
- [Retrieving the users in a Slack workspace](https://github.com/jolicode/slack-php-api/tree/main/docs/examples/retrieve-users.php);
89+
90+
## Under the hood
91+
92+
This library mainly contains automatically generated code from the official
93+
[Slack OpenAPI spec](https://github.com/slackapi/slack-api-specs).
94+
95+
Four kinds of PHP classes are included:
96+
- [endpoints](https://github.com/jolicode/slack-php-api/tree/main/generated/Endpoint) represent the requests made to API methods;
97+
- [models](https://github.com/jolicode/slack-php-api/tree/main/generated/Model) represent data exchanged with the API;
98+
- [normalizers](https://github.com/jolicode/slack-php-api/tree/main/generated/Normalizer) transform JSON from the API to PHP models;
99+
- [runtime](https://github.com/jolicode/slack-php-api/tree/main/generated/Runtime) are classes needed to make Jane work.
100+
101+
If you use the `JoliCode\Slack\ClientFactory` to create the Client (which you
102+
should), you don't have to understand how the library works internally.
103+
Calling one of its method will make the HTTP request to the API and return the
104+
corresponding PHP object.
105+
106+
***
107+
108+
Read more:
109+
- Next page: [Troubleshoots](3-troubleshoots.md)
110+
- Previous page: [Get started](1-get-started.md)

docs/3-troubleshoots.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Troubleshoots
2+
3+
Got some problems using this library? Need a missing feature?
4+
Here is some common troubleshoots.
5+
6+
## Bypassing the incomplete API specification
7+
8+
Slack's OpenAPI spec is not 100% complete yet, hence our generated client may
9+
miss some features.
10+
11+
If you miss an option in a method, until Slack add it to their spec, we can
12+
manually add it to our [versioned spec](https://github.com/jolicode/slack-php-api/tree/main/resources/slack-openapi-patched.json) then
13+
[regenerate](updating-sdk.md) and release a new version of the library.
14+
15+
## Missing data in the DTO?
16+
17+
The Slack specification is not up to date and miss some critical parts. We do
18+
build a [better one on top of the official](4-updating-sdk.md) but it can't
19+
be perfect.
20+
21+
What's good is that some models use `\ArrayObject` as base classes so if the
22+
API returns data we don't have in the OpenAPI mapping, you can still access it via
23+
Array like syntax:
24+
25+
```php
26+
$results = $client->searchMessages([
27+
'query' => 'test'
28+
]);
29+
30+
var_dump($results->getOk()); // ok property is mapped
31+
var_dump($results['messages']); // messages property is not mapped but still readable
32+
```
33+
34+
Feel free to open issues if you think a model miss some fields.
35+
36+
## Other problems?
37+
38+
The above documentation did not answer your question? Do not hesitate to open
39+
an issue and share your issue with us.
40+
41+
***
42+
43+
Read more:
44+
- Next page: [Updating the SDK](4-updating-sdk.md)
45+
- Previous page: [Usage](2-usage.md)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,9 @@ changes, you can check that the patch is correctly re-applied:
5656
```bash
5757
./bin/slack-api-client-generator spec:update
5858
```
59+
60+
***
61+
62+
Read more:
63+
- Next page: [Changelog](/changelog)
64+
- Previous page: [Troubleshoots](3-troubleshoots.md)

0 commit comments

Comments
 (0)