-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuser.service.ts
50 lines (39 loc) · 1.32 KB
/
user.service.ts
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
import { Comments } from './Model/comments';
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { Post } from './Model/post';
@Injectable({
providedIn: 'root'
})
export class UserService {
private url = 'https://jsonplaceholder.typicode.com/'
constructor(private http: HttpClient) { }
public getPosts(): Observable<Post[]> {
const head = new HttpHeaders({
'Accept': 'application/json',
'Content-Type': 'application/json',
// 'Authorization': 'Bearer '+token
})
return this.http.get<Post[]>(this.url + 'posts', { headers: head })
}
public comments(): Observable<Comments[]> {
const head = new HttpHeaders({
'Accept': 'application/json',
'Content-Type': 'application/json',
// 'Authorization': 'Bearer '+token
})
return this.http.get<Comments[]>(this.url + 'comments', { headers: head })
}
public getpost(id): Observable<Post[]>{
const head = new HttpHeaders({
'Accept': 'application/json',
'Content-Type': 'application/json',
// 'Authorization': 'Bearer '+token
})
return this.http.get<Post[]>(this.url + 'posts/'+id, { headers: head }).pipe(
catchError(err => of(null))
)
}
}