Skip to content

Commit 16d3cae

Browse files
committed
replacing http module with http-client
1 parent 5aaf0e4 commit 16d3cae

6 files changed

+17
-50
lines changed

Diff for: app/http/http-examples.module.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
22
import { NativeScriptRouterModule } from "nativescript-angular/router";
33
import { NativeScriptCommonModule } from "nativescript-angular/common";
44
import { NativeScriptFormsModule } from "nativescript-angular/forms";
5-
import { NativeScriptHttpModule } from "nativescript-angular/http";
5+
import { NativeScriptHttpClientModule } from "nativescript-angular/http-client";
66

77
import { HttpExamplesComponent } from "./http-examples.component";
88

@@ -29,7 +29,7 @@ export const routerConfig = [
2929
schemas: [NO_ERRORS_SCHEMA],
3030
imports: [
3131
TitleAndNavButtonModule,
32-
NativeScriptHttpModule,
32+
NativeScriptHttpClientModule,
3333
NativeScriptFormsModule,
3434
NativeScriptCommonModule,
3535
NativeScriptRouterModule,

Diff for: app/http/http-get/http-get.component.html

-10
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,10 @@
44
<Label class="p-15" row="1" col="0" text="User-Agent" textWrap="true"></Label>
55
<Label class="p-15" row="2" col="0" text="Origin" textWrap="true"></Label>
66
<Label class="p-15" row="3" col="0" text="Url" textWrap="true"></Label>
7-
<Label class="p-15" row="4" col="0" text="Status code" textWrap="true"></Label>
8-
<Label class="p-15" row="5" col="0" text="Status text" textWrap="true"></Label>
9-
<Label class="p-15" row="6" col="0" text="Date" textWrap="true"></Label>
10-
<Label class="p-15" row="7" col="0" text="ContentType" textWrap="true"></Label>
11-
<Label class="p-15" row="8" col="0" text="Server" textWrap="true"></Label>
127

138
<Label class="p-15" row="0" col="1" [text]="host" textWrap="true"></Label>
149
<Label class="p-15" row="1" col="1" [text]="userAgent" textWrap="true"></Label>
1510
<Label class="p-15" row="2" col="1" [text]="origin" textWrap="true"></Label>
1611
<Label class="p-15" row="3" col="1" [text]="url" textWrap="true"></Label>
17-
<Label class="p-15" row="4" col="1" [text]="status" textWrap="true"></Label>
18-
<Label class="p-15" row="5" col="1" [text]="statusText" textWrap="true"></Label>
19-
<Label class="p-15" row="6" col="1" [text]="dateHeader" textWrap="true"></Label>
20-
<Label class="p-15" row="7" col="1" [text]="typeHeader" textWrap="true"></Label>
21-
<Label class="p-15" row="8" col="1" [text]="serverHeader" textWrap="true"></Label>
2212
</GridLayout>
2313
</ScrollView>

Diff for: app/http/http-get/http-get.component.ts

-20
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,11 @@ export class HttpGetComponent implements OnInit {
1212
public userAgent: string;
1313
public origin: string;
1414
public url: string;
15-
public status: number;
16-
public statusText: string;
17-
public dateHeader: string;
18-
public typeHeader: string;
19-
public serverHeader: string;
2015

2116
constructor(private myService: MyHttpGetService) { }
2217

2318
ngOnInit() {
2419
this.extractData();
25-
this.extractResponseInfo();
2620
}
2721

2822
extractData() {
@@ -34,19 +28,6 @@ export class HttpGetComponent implements OnInit {
3428
});
3529
}
3630

37-
extractResponseInfo() {
38-
this.myService.getResponseInfo()
39-
.subscribe(res => {
40-
this.status = res.status;
41-
this.statusText = res.statusText;
42-
this.typeHeader = res.headers.get("Content-Type");
43-
this.dateHeader = res.headers.get("Date");
44-
this.serverHeader = res.headers.get("Server");
45-
}, (error) => {
46-
console.log("onGetResponseInfo" + error);
47-
});
48-
}
49-
5031
private onGetDataSuccess(res) {
5132
this.host = res.headers.Host;
5233
this.userAgent = res.headers["User-Agent"];
@@ -61,4 +42,3 @@ export class HttpGetComponent implements OnInit {
6142
}
6243
}
6344
// << http-get-component
64-

Diff for: app/http/http-get/http-get.services.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
// >> http-get-service
22
import { Injectable } from "@angular/core";
3-
import { Http, Headers, Response } from "@angular/http";
43
import { Observable as RxObservable } from "rxjs/Observable";
5-
4+
import { HttpClient, HttpHeaders, HttpResponse } from "@angular/common/http";
65
import "rxjs/add/operator/map";
76
import "rxjs/add/operator/do";
87

98
@Injectable()
109
export class MyHttpGetService {
1110
private serverUrl = "https://httpbin.org/get";
1211

13-
constructor(private http: Http) { }
12+
constructor(private http: HttpClient) { }
1413

1514
getData() {
1615
let headers = this.createRequestHeader();
1716
return this.http.get(this.serverUrl, { headers: headers })
18-
.map(res => res.json());
17+
.map(res => res);
1918
}
2019

2120
getResponseInfo() {
2221
let headers = this.createRequestHeader();
2322
return this.http.get(this.serverUrl, { headers: headers })
24-
.do(res => res);
23+
.do(res => res);
2524
}
2625

2726
private createRequestHeader() {
28-
let headers = new Headers();
27+
let headers = new HttpHeaders();
2928
// set headers here e.g.
3029
headers.append("AuthKey", "my-key");
3130
headers.append("AuthToken", "my-token");

Diff for: app/http/http-post/http-post.component.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export class HttpPostComponent {
1414
// >> (hide)
1515
public isItemVisible: boolean = false;
1616
// << (hide)
17-
1817
constructor(private myPostService: MyHttpPostService) { }
1918

2019
public submit() {
@@ -25,7 +24,7 @@ export class HttpPostComponent {
2524
this.myPostService
2625
.postData({ username: this.user, password: this.pass })
2726
.subscribe(res => {
28-
this.message = res.json.data.username;
27+
this.message = (<any>res).json.data.username;
2928
// >> (hide)
3029
this.isItemVisible = true;
3130
// << (hide)

Diff for: app/http/http-post/http-post.services.ts

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
// >> http-post-service
22
import { Injectable } from "@angular/core";
3-
import { Http, Headers, Response, RequestOptions } from "@angular/http";
43
import { Observable as RxObservable } from "rxjs/Observable";
54
import "rxjs/add/operator/map";
5+
import { HttpClient, HttpHeaders, HttpResponse } from "@angular/common/http";
66

77
@Injectable()
88
export class MyHttpPostService {
99
private serverUrl = "https://httpbin.org/post";
1010

11-
constructor(private http: Http) { }
11+
constructor(private http: HttpClient) { }
1212

1313
postData(data: any) {
1414
let options = this.createRequestOptions();
15-
return this.http.post(this.serverUrl, { data }, options)
16-
.map(res => res.json());
15+
return this.http.post(this.serverUrl, { data }, { headers: options })
16+
.map(res => res);
1717
}
1818

1919
private createRequestOptions() {
20-
let headers = new Headers();
21-
headers.append("AuthKey", "my-key");
22-
headers.append("AuthToken", "my-token");
23-
headers.append("Content-Type", "application/json");
24-
let options = new RequestOptions({ headers: headers });
25-
return options;
20+
let headers = new HttpHeaders();
21+
headers.set("AuthKey", "my-key");
22+
headers.set("AuthToken", "my-token");
23+
headers.set("Content-Type", "application/json");
24+
return headers;
2625
}
2726
}
2827
// << http-post-service

0 commit comments

Comments
 (0)