forked from hakimel/reveal.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththe_quickest_way_to_stable_API.html
More file actions
executable file
·1339 lines (1111 loc) · 43.2 KB
/
Copy paththe_quickest_way_to_stable_API.html
File metadata and controls
executable file
·1339 lines (1111 loc) · 43.2 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>The quickest way to stable API</title>
<meta name="description" content="The quickest way to stable API using Laravel framework">
<meta name="author" content="Milan Popovic">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/black.css" id="theme">
<link rel="stylesheet" href="css/presentation/the_quickest_way_to_stable_API.css">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>The quickest way to stable API</h1>
<h3>Using Laravel framework</h3>
<p>
<small>Presented by <a href="http://milanpopovic.me">Milan Popović</a> / <a href="http://twitter.com/komita1981">@komita1981</a></small>
</p>
</section>
<section>
<h2>Me</h2>
<p class="left_align">
PHP developer
</p>
<p class="left_align">
Work for Navus Consulting Gmbh
</p>
<p class="left_align">
I like to learn and share knowledge
</p>
<p class="left_align" >
Active member of PHP Srbija
</p>
</section>
<section>
<p class="left_align">
Why Laravel?
</p>
<p class="left_align fragment">
Enables rapid development
</p>
<p class="left_align fragment">
Stable and well tested
</p>
<p class="left_align fragment">
Community support
</p>
<p class="left_align fragment">
Documentation
</p>
<p class="left_align fragment">
Learning resources
</p>
<p class="left_align fragment">
Easy to recruit developers
</p>
<p class="left_align fragment">
LTS
</p>
<aside class="notes">
Why framework? A software framework is 80% of your application already written for you.
</aside>
</section>
<section>
<p class="left_align">
Why Laravel for quick API development?
</p>
<p class="left_align">
DB Migration
</p>
<p class="left_align">
DB seeding
</p>
<p class="left_align">
Model factory
</p>
<p class="left_align">
Form request
</p>
<p class="left_align">
Testing
</p>
<p class="left_align">
3rd Party Packages
</p>
</section>
<section>
<h2>
DB Migration
</h2>
<p class="left_align">
Version control for you DB schema
</p>
<p class="left_align">
Laravel Schema facade provides database agnostic support for creating and manipulating tables
</p>
<pre>
<code data-trim contenteditable>
// Create new migration
php artisan make:migration create_users_table
// Run migration
php artisan migrate
// Rollback migration
php artisan migrate
</code>
</pre>
</section>
<section>
<h2>
DB Seed
</h2>
<p class="left_align">
Sample/Test data for your DB
</p>
<pre>
<code data-trim contenteditable>
// Create new seeder
php artisan make:seeder UsersTableSeeder
// Run seeders
php artisan db:seed
// Run seeders v2
php artisan migrate --seed
// Run single seeder
php artisan db:seed --class=UsersTableSeeder
</code>
</pre>
</section>
<section>
<section>
<h2>
Model Factory
</h2>
<p class="left_align">
Default model attributes
</p>
<p class="left_align">
Uses Faker for generating values
</p>
<pre>
<code data-trim contenteditable>
$factory->define(App\User::class, function (Faker\Generator $faker) {
return [
'name' => $faker->name,
'email' => $faker->email,
'password' => bcrypt(str_random(10)),
'remember_token' => str_random(10),
];
});
</code>
</pre>
</section>
<section>
<p class="left_align">
Multiple Factory Types - multiple factories for the same Eloquent model class
</p>
<pre>
<code data-trim contenteditable>
$factory->defineAs(App\User::class, 'admin', function ($faker) use ($factory) {
$user = $factory->raw(App\User::class);
return array_merge($user, ['admin' => true]);
});
</code>
</pre>
</section>
<section>
<p class="left_align">
Attach relationships
</p>
<pre>
<code data-trim contenteditable>
$factory->define(App\Post::class, function ($faker) {
return [
'title' => $faker->title,
'content' => $faker->paragraph,
'user_id' => function () {
return factory(App\User::class)->create()->id;
},
'user_type' => function (array $post) {
return App\User::find($post['user_id'])->type;
}
];
});
</code>
</pre>
</section>
<section>
<p class="left_align">
Using factories
</p>
<pre>
<code data-trim contenteditable>
// Create three App\User instances...
$users = factory(App\User::class, 3)->make();
// Create three App\User "admin" instances...
$users = factory(App\User::class, 'admin', 3)->make();
// Create App\User user with given name...
$user = factory(App\User::class)->make([
'name' => 'Abigail',
]);
// Persisting Factory Models
$user = factory(App\User::class)->create();
</code>
</pre>
</section>
</section>
<section>
<section>
<h2>
Form Requests
</h2>
<p class="left_align">
Special class for validating request params and authorizing
</p>
<p class="left_align">
Each class has rules() (returns array) and authorize() (returns boolean) methods
</p>
<p class="left_align">
Type Hinting in Controller methods
</p>
</section>
<section>
<pre>
<code data-trim contenteditable>
public function rules()
{
return [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
];
}
public function authorize()
{
$commentId = $this->route('comment');
return Comment::where('id', $commentId)
->where('user_id', Auth::id())->exists();
}
</code>
</pre>
</section>
</section>
<section>
<section>
<h2>
Testing
</h2>
<p class="left_align">
PHPUnit is included out of the box
</p>
<p class="left_align">
Laravel is built with testing in mind
</p>
<p class="left_align">
It's also ships with convenient helper methods
</p>
<p class="left_align">
Uses Mockery as mock object framework
</p>
</section>
<section>
<h2>
Working with database
</h2>
<p class="left_align">
Using Migrations - DatabaseMigrations trait
</p>
<p class="left_align">
DatabaseTransactions - DatabaseTransactions trait
</p>
<p class="left_align">
Model Factories
</p>
</section>
<section>
<h2>
Working with database
</h2>
<p class="left_align">
SeeInDatabase helper <br/>
seeInDatabase($table, array $data, $connection = null)
</p>
<p class="left_align">
NotSeeInDatabase helper <br/>
notSeeInDatabase($table, array $data, $connection = null)
</p>
<p class="left_align">
Seed (seed($class = 'DatabaseSeeder')
</p>
</section>
<section>
<h2>
Creating HTTP request
</h2>
<p class="left_align">
<pre>
<code data-trim contenteditable>
// Main method
public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
// There are also methods for other http verbs
public function post($uri, array $data = [], array $headers = [])
// Visit the given URI with a JSON request.
public function json($method, $uri, array $data = [], array $headers = [])
{
// removed code from original method
$content = json_encode($data);
$headers = array_merge([
'CONTENT_LENGTH' => mb_strlen($content, '8bit'),
'CONTENT_TYPE' => 'application/json',
'Accept' => 'application/json',
], $headers);
}
// Call a named route and return the Response.
public function route($method, $name, $routeParameters = [], $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
</code>
</pre>
</p>
</section>
<section>
<h2>
PHPUnit Assertions for status code and headers
</h2>
<p class="left_align fragment">
assertResponseOk()
</p>
<p class="left_align fragment">
assertResponseStatus($code)
</p>
<p class="left_align fragment">
seeStatusCode($status)
</p>
<p class="left_align fragment">
seeHeader($headerName, $value)
</p>
</section>
<section>
<h2>
PHPUnit Assertions for json
</h2>
<p class="left_align fragment">
seeJsonEquals($data)
</p>
<p class="left_align fragment">
seeJsonStructure($structure, $responseData)
</p>
<p class="left_align fragment">
seeJsonContains($data, $negate)
</p>
<p class="left_align fragment">
seeJsonSubset($data)
</p>
</section>
</section>
<section>
<section>
<h2>
Packages & Tools
</h2>
<p class="left_align">
Dingo API
</p>
<p class="left_align">
Dredd
</p>
<p class="left_align">
Repositories
</p>
<p class="left_align">
CORS
</p>
</section>
</section>
<section>
<section>
<h2>
Dingo API
</h2>
<p class="left_align">
<pre>
<code data-trim contenteditable>
"require": {
"dingo/api": "1.0.*@dev"
}
</code>
</pre>
</p>
<p class="left_align">
Provides set of tools to easily and quickly build your own API
</p>
<p class="left_align">
Requires Laravel 5.1+ or Lumen 5.1+ and PHP 5.5.9+
</p>
</section>
<section>
<h3>
Configuration
</h3>
<p class="left_align">
Much of the package comes preconfigured
</p>
<p class="left_align">
Use your .env file to configure most of the package
</p>
<p class="left_align">
Finer tuning of the package will require publishing the configuration file
</p>
</section>
<section>
<p class="left_align">
Configure:
</p>
<p class="left_align">
Name
</p>
<p class="left_align">
Default Version
</p>
<p class="left_align">
Authentication Provider
</p>
<p class="left_align">
Throttling / Rate Limiting (Disabled by default)
</p>
<p class="left_align">
Response Transformer (Fractal is the default)
</p>
<p class="left_align">
Response Format (JSON and a JSON response format is registered by default)
</p>
<p class="left_align">
Error Format
</p>
</section>
<section>
<h3>
Creating API Endpoints
</h3>
<p class="left_align">
<pre>
<code data-trim contenteditable>
// To avoid complications with your main application routes this package utilizes its own router.
$api = app('Dingo\Api\Routing\Router');
$api->version(
'v1',
function ($api) {
$api->post('register', 'SampleApi\Http\Controllers\RegisterController@process');
$api->post('login', 'SampleApi\Http\Controllers\LoginController@login');
$api->get('blocks', 'SampleApi\Http\Controllers\BlocksController@getAll');
$api->get('blocks/{id}', 'SampleApi\Http\Controllers\BlocksController@get');
$api->post('blocks', 'SampleApi\Http\Controllers\BlocksController@create');
$api->put('blocks/{id}', 'SampleApi\Http\Controllers\BlocksController@update');
$api->delete('blocks/{id}', 'SampleApi\Http\Controllers\BlocksController@delete');
}
);
//protected with JWT
$api->version(
'v1',
['middleware' => 'api.auth'],
function ($api) {
$api->post('logout', 'Ematerials\Api\Http\Controllers\LoginController@logout');
}
);
</code>
</pre>
</p>
</section>
<section>
<h3>
Responses
</h3>
<p class="left_align">
There's a number of different ways to return responses
</p>
<p class="left_align">
Use Response Builder
</p>
<p class="left_align">
Provides a fluent interface to easily build a more customizable response
</p>
<p class="left_align">
Generally used in conjunction with transformers.
</p>
<p class="left_align">
Use the Dingo\Api\Routing\Helpers trait
</p>
</section>
<section>
<p class="left_align">
<pre>
<code data-trim contenteditable>
use Dingo\Api\Routing\Helpers;
use Illuminate\Routing\Controller;
class ApiController extends Controller
{
use Helpers;
}
</code>
</pre>
</p>
</section>
<section>
<h3>
Responding With An Array
</h3>
<p class="left_align">
<pre>
<code data-trim contenteditable>
public function show($id)
{
$block = $this->block->find($id);
return $this->response->array($block->toArray());
}
</code>
</pre>
</p>
</section>
<section>
<h3>
Responding With A Single Item
</h3>
<p class="left_align">
<pre>
<code data-trim contenteditable>
public function show($id)
{
$block = $this->block->find($id);
return $this->response->item($block, new BlockTransformer);
}
</code>
</pre>
</p>
</section>
<section>
<h3>
Responding With A Collection Of Items
</h3>
<p class="left_align">
<pre>
<code data-trim contenteditable>
public function show($id)
{
$blocks = $this->block->find($id);
return $this->response->collection($blocks, new BlockTransformer);
}
</code>
</pre>
</p>
</section>
<section>
<h3>
Responding With Paginated Items
</h3>
<p class="left_align">
<pre>
<code data-trim contenteditable>
public function show($id)
{
$blocks = $this->block->find($id);
return $this->response->pagination($blocks, new BlockTransformer);
}
</code>
</pre>
</p>
</section>
<section>
<h3>
Other responses
</h3>
<p class="left_align">
<pre>
<code data-trim contenteditable>
// Responding With No Content
return $this->response->noContent();
// Responding With Created Response
return $this->response->created();
// A generic error with custom message and status code.
return $this->response->error('This is an error.', 404);
// A not found error with an optional message as the first parameter.
return $this->response->errorNotFound();
// There are also errorBadRequest, errorForbidden, errorInternal, errorUnauthorized
</code>
</pre>
</p>
</section>
<section>
<p class="left_align">
Adding Additional Headers
<pre>
<code data-trim contenteditable>
return $this->response->item($user, new UserTransformer)->withHeader('X-Foo', 'Bar');
</code>
</pre>
</p>
<p class="left_align">
Adding Meta Data
<pre>
<code data-trim contenteditable>
return $this->response->item($user, new UserTransformer)->addMeta('foo', 'bar');
// set an array of meta data instead of chaining multiple method calls
return $this->response->item($user, new UserTransformer)->setMeta($meta)
</code>
</pre>
</p>
<p class="left_align">
Setting Response Status Code
<pre>
<code data-trim contenteditable>
return $this->response->item($user, new UserTransformer)->setStatusCode(200);
</code>
</pre>
</p>
</section>
<section>
<h3>
Transformers
</h3>
<p class="left_align">
Easily and consistently transform objects into an array
</p>
<p class="left_align">
Type-cast integers and booleans, include pagination results, and nest relationships
</p>
<p class="left_align">
Fractal is the default transformation layer used by Dingo
</p>
</section>
<section>
<p class="left_align">
<pre>
<code data-trim contenteditable>
public function __construct()
{
$this->availableIncludes = [
'presentations',
'block_settings',
];
$this->defaultIncludes = [
'block_settings',
];
}
public function transform(Block $block)
{
return [
'id' => (int)$block->id,
'name' => $block->name,
'start_at' => $block->start_at->toDateTimeString(),
'ends_at' => $block->ends_at->toDateTimeString(),
'code' => $block->code,
];
}
public function includePresentations(BlockModel $block)
{
return $this->collection($block->presentations, new PresentationTransformer());
}
public function includeBlockSettings(BlockModel $block)
{
return $this->item($block->blockSettings, new BlockSettingsTransformer());
}
</code>
</pre>
</p>
</section>
<section>
<p>
<img class="noframes" alt="Exceptions" src="img/the_quickest_way_to_stable_API/exceptions.png">
</p>
</section>
<section>
<h3>
Form Request end Error Responses
</h3>
<p class="left_align">
Extend the base API form request class
</p>
<p class="left_align">
Implement your own class
</p>
<p class="left_align">
Dingo\Api\Exception\ValidationHttpException should be thrown
</p>
</section>
<section>
<h3>
Authentication
</h3>
<p class="left_align">
HTTP Basic (Dingo\Api\Auth\Provider\Basic)
</p>
<p class="left_align">
JSON Web Tokens (Dingo\Api\Auth\Provider\JWT)
</p>
<p class="left_align">
OAuth 2.0 (Dingo\Api\Auth\Provider\OAuth2)
</p>
<p class="left_align">
Custom Authentication Providers
</p>
</section>
<section>
<h3>
Documentation
</h3>
<p class="left_align">
API Blueprint Documentation
</p>
<p class="left_align">
Annotate API controllers
</p>
<p class="left_align">
Generate documentation using the Artisan command
</p>
<p class="left_align">
But...
</p>
</section>
</section>
<section>
<section>
<h3>
Dredd — HTTP API Testing Framework
</h3>
<p>
<img class="noframes" alt="Exceptions" src="img/the_quickest_way_to_stable_API/dredd.png">
</p>
</section>
<section>
<p class="left_align">
“
<q>
Dredd is a language-agnostic command-line tool for validating API documentation written
in API Blueprint format against its backend implementation. </q>
”
</p>
<p class="left_align">
Dredd reads your API description
</p>
<p class="left_align">
Validates whether API replies with expected responses
</p>
<p class="left_align">
Continuous Integration Support
</p>
<p class="left_align">
Hooks — a glue code for each test setup and teardown
</p>
<p class="left_align">
Use of Gavel.js library
</p>
<p class="left_align">
Do not install dredd manually on Homestead - use after.sh
</p>
<aside class="notes">
Generates expectations on HTTP responses based on examples in the blueprint with use of Gavel.js library
</aside>
</section>
<section>
<h3>
Headers Expectations
</h3>
<ul>
<li>
All headers given in example must be present in the response
</li>
<li>
Only values of headers significant for content negotiation are validated
</li>
<li>
All other headers values can differ
</li>
</ul>
</section>
<section>
<h3>
Body Expectations
</h3>
<ul>
<li>
All JSON keys on any level given in the example must be present in the response JSON
</li>
<li>
Response JSON values must be of the same JSON primitive type
</li>
<li>
All JSON values can differ
</li>
<li>
Arrays can have additional items, type or structure is not validated.
</li>
<li>
Plain text must match perfectly
</li>
</ul>
</section>
<section>
<p class="left_align">
PHP Dredd hook handler
</p>
<p class="left_align">
Provides a bridge between the Dredd API Testing Framework and PHP environment
</p>
<p class="left_align">
<pre>
<code data-trim contenteditable>
"require-dev": {
"ddelnano/dredd-hooks-php": "~1.0.0",
}
</code>
</pre>
</p>
</section>
<section>
<h3>
Hooks usage