Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
A close look to Angular's new HttpClient
Search
Manfred Steyer
PRO
December 09, 2017
Programming
1
410
A close look to Angular's new HttpClient
Presentation from ng-be in December 2017.
Manfred Steyer
PRO
December 09, 2017
Tweet
Share
More Decks by Manfred Steyer
See All by Manfred Steyer
Native Federation: The Future of Micro Frontends in Angular
manfredsteyer
PRO
0
96
Architectures with Lightweight Stores: New Rules and Options
manfredsteyer
PRO
0
100
3 Effective Rules for Success with Signals in Angular
manfredsteyer
PRO
0
120
Modern Angular: Renovation for Your Applications
manfredsteyer
PRO
0
140
Where do we go from here? Strategies for Signals
manfredsteyer
PRO
0
170
Successful with Signals: 3 Effective Rules
manfredsteyer
PRO
0
95
Vertical Architectures for Scalable Angular Applications
manfredsteyer
PRO
0
270
Successful with Signals: 3 Effective Rules
manfredsteyer
PRO
0
100
Modern Angular Architectures With Nx and Lightweight Stores @ngBelgrade 2024
manfredsteyer
PRO
0
170
Other Decks in Programming
See All in Programming
Temporalを取り巻く仕様を整理する
sajikix
0
110
Exploring the Gradually Lost Technical Skills in the Cloud Native Era
hwchiu
2
3.9k
Harnessing Large Language Models for Training-free Video Anomaly Detection
tereka114
1
1.3k
最古の関数型言語「Lisp」ことはじめ / lisp_in_kamiyama
uhooi
1
190
Product Management LT会_クアンド新家
shinshin
0
210
CSC307 Lecture 05
javiergs
PRO
0
210
社内 LT 会を発足し、アウトプット文化を醸成させるために考えたこと・やったこと / Starting internal LT meetings and fostering an output culture
mackey0225
3
120
AHC035解説
terryu16
0
710
君たちはどうコードをレビューする (される) か / 大吉祥寺.pm
utgwkk
15
8.5k
I/O Extended Android in Korea 2024 ~ Whats new in Android development tools
pluu
0
250
ピグパーティにおけるMongoDB CommunityバージョンからAtlasへの移行事例
10969hotaka
0
130
みんなのオブザーバビリティプラットフォームを作ってるんだがパフォーマンスがやばい #mackerelio #srenext
ne_sachirou
0
370
Featured
See All Featured
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
105
6.8k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
155
14k
GraphQLとの向き合い方2022年版
quramy
36
13k
The Invisible Customer
myddelton
117
13k
Design by the Numbers
sachag
277
18k
The Illustrated Children's Guide to Kubernetes
chrisshort
39
47k
Typedesign – Prime Four
hannesfritz
37
2.2k
Designing the Hi-DPI Web
ddemaree
276
34k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
35
6.3k
Faster Mobile Websites
deanohume
303
30k
KATA
mclloyd
20
13k
Documentation Writing (for coders)
carmenintech
63
4.2k
Transcript
A close look to Angular's new HttpClient Manfred Steyer SOFTWAREarchitekt.at
About me … • Manfred Steyer • Angular Workshops and
Consultancy • SOFTWAREarchitekt.at • Google Developer Expert (GDE) Page ▪ 2 Manfred Steyer
In the old days, everything was better ;-)
Just a few examples …
Http in Angular Http (Service) • Angular 2+ • Most
Common Use Cases (80:20 principle) • Experimental • Deprecated with 5.0 HttpClient • Angular 4.3 • A lot of additional features • Full comfort we know from $http
Contents • Short overview of Basics • Custom Data Formats
(Binary, Text) • Progress Information • Interceptors for Extensions • Token-based Security
DEMO
angular-crud
None
Basics
HttpClient HttpClientModule and HttpClient from @angular/common/http let url = 'http://www.angular.at/api/hotel';
let headers = new HttpHeaders() .set('Accept', 'application/json'); let params = new HttpParams() .set('city', this.city); this .http .get<Hotel[]>(url, { headers, params }) .subscribe( hotels => console.debug('hotels', hotels), err => console.error('err', err) );
Getting the full HttpResponse save(entity: HotelBooking): Observable<string> { let url
= […] let headers = […] return this.http .post<HotelBooking>(url, entity, {headers, observe: 'response'}) […] }
Getting the full HttpResponse save(entity: HotelBooking): Observable<string> { let url
= […] let headers = […] return this.http .post<HotelBooking>(url, entity, {headers, observe: 'response'}) .pipe( map((response: HttpResponse<HotelBooking>) => { console.debug('status', response.status); console.debug('body', response.body); return response.headers.get('Location'); })); }
Testing
Main Idea: Mock the HttpBackend HttpClientTestingModule Your Code Test HttpClient
HttpBackend Server MockBackend
Testing it('can load hotels by city', () => { let
service: HotelService = TestBed.get(HotelService); let ctrl: HttpTestingController = TestBed.get(HttpTestingController); […] });
Testing it('can load hotels by city', () => { let
service: HotelService = TestBed.get(HotelService); let ctrl: HttpTestingController = TestBed.get(HttpTestingController); service.find({ city: 'Graz' }).subscribe( hotels => expect(hotels.length).toBe(2), err => fail(err) ); […] });
Testing it('can load hotels by city', () => { let
service: HotelService = TestBed.get(HotelService); let ctrl: HttpTestingController = TestBed.get(HttpTestingController); service.find({ city: 'Graz' }).subscribe( hotels => expect(hotels.length).toBe(2), err => fail(err) ); let req = ctrl.expectOne('/hotel?city=Graz'); expect(req.request.method).toBe('GET'); […] });
Testing it('can load hotels by city', () => { let
service: HotelService = TestBed.get(HotelService); let ctrl: HttpTestingController = TestBed.get(HttpTestingController); service.find({ city: 'Graz' }).subscribe( hotels => expect(hotels.length).toBe(2), err => fail(err) ); let req = ctrl.expectOne('/hotel?city=Graz'); expect(req.request.method).toBe('GET'); req.flush([{id: 1, name: 'Hotel Mama'}, {id: 2, name: 'Budget Hotel'}]); ctrl.verify(); });
Custom Data Formats (Text and Binary)
Using XML findById(id: string): Observable<HotelBooking> { […] return this.http .get(url,
{ headers, params, responseType: 'text'}) […] }
Using XML findById(id: string): Observable<HotelBooking> { […] return this.http .get(url,
{ headers, params, responseType: 'text'}) .pipe( switchMap(xmlString => { let observableFactory = bindCallback(parseString); return observableFactory(xmlString, parserOptions); }), map(js => js[1].hotelBooking) ); } import { parseString } from 'xml2js';
DEMO
Interceptors
Idea • Providing a hook • Modify requests and responses
globally • Including Headers, e. g. for Auth. • Parsing Data Formats, like XML • Error Handling • Caching • …
Chain of Responsibility Logic (HttpRequest) Interceptor Interceptor Request Response
DEMO Sending Access Token for Auth Global Error Handler for
Auth
Conclusion observe: response, events, … responseType: text, blob, … Interceptor
Chain The Dark Knight: Best Movie ever!
Contact und Downloads [mail]
[email protected]
[blog] SOFTWAREarchitekt.at [twitter] ManfredSteyer