api.service.ts (1767B)
1 import {Inject, Injectable} from '@angular/core'; 2 import { HttpClient } from '@angular/common/http'; 3 import {DOCUMENT} from '@angular/common'; 4 import {Observable} from 'rxjs'; 5 6 export interface Tweet { 7 tweets?: (TweetsEntity)[]; 8 } 9 export interface TweetsEntity { 10 username: string; 11 user_id: string; 12 display_name: string; 13 userprofile_link: string; 14 retweetby_username?: string; 15 retweetby_user_id?: string; 16 retweetby_display_name?: string; 17 retweetby_userprofile_link?: string; 18 tweet_link?: string; 19 text: string; 20 image_urls?: (string)[]; 21 created_at: string; 22 favorites: string; 23 retweets: string; 24 retweet: boolean; 25 } 26 27 export interface Paper { 28 name?: string; 29 uuid?: string; 30 description?: string; 31 paper_image?: string; 32 author?: Author; 33 } 34 35 export interface Author { 36 uuid?: string; 37 username?: string; 38 profile_image_url?: string; 39 twitter_profile?: string; 40 google_profile?: string; 41 github_profile?: string; 42 } 43 44 @Injectable() 45 export class ApiService { 46 url: string; 47 userUUID: string; 48 inCallback: boolean; 49 50 constructor(@Inject(DOCUMENT) private document, private http: HttpClient) { 51 this.url = document.location.protocol + '//' + document.location.hostname + '/api'; 52 } 53 54 // Uses http.get() to load data from a single API endpoint 55 getYesterday(uuid: string): Observable<Tweet> { 56 return this.http.get<Tweet>(`${this.url}/paper/${uuid}/yesterday`); 57 } 58 59 getPapers(): Observable<(Paper)[]> { 60 return this.http.get<(Paper)[]>(`${this.url}/papers`); 61 } 62 63 getPaper(uuid: string): Observable<Paper> { 64 return this.http.get<Paper>(`${this.url}/paper/${uuid}`); 65 } 66 67 checkIfUserIsAuthenticated(): Observable<any> { 68 return this.http.get(`${this.url}/login/twitter/check`, { observe: 'response' }); 69 } 70 }