53 lines
1.4 KiB
TypeScript
53 lines
1.4 KiB
TypeScript
import {Injectable} from '@angular/core';
|
|
import {Observable} from 'rxjs';
|
|
import {HttpClient} from '@angular/common/http';
|
|
import {Token} from './token';
|
|
import {Host} from './host';
|
|
import {ChatMessage} from './chat.message';
|
|
import {ChatTokenResponse} from './chat.token';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class ApiService {
|
|
public static userToken: string;
|
|
|
|
constructor(private client: HttpClient) {
|
|
}
|
|
|
|
storeData(key: string, value: string): void
|
|
{
|
|
sessionStorage.setItem(key, value);
|
|
}
|
|
|
|
getFromStorage(key: string): string {
|
|
return sessionStorage.getItem(key);
|
|
}
|
|
|
|
getAuthToken(username: string, password: string): Observable<Token> {
|
|
return this.client.post<Token>(Host.URL + '/token', {username, password});
|
|
}
|
|
|
|
getChatToken(authToken: string): Observable<ChatTokenResponse> {
|
|
return this.client.get<ChatTokenResponse>(
|
|
Host.URL + '/session/chat',
|
|
{headers: {Authorization: 'Bearer ' + authToken}}
|
|
);
|
|
}
|
|
|
|
getChatHistory(token: string, offset: number, limit: number): Observable<ChatMessage[]> {
|
|
return this.client.get<ChatMessage[]>(
|
|
Host.URL + '/session/chat/history?limit=' + limit + '&offset=' + offset,
|
|
{headers: {Authorization: 'Bearer ' + token}}
|
|
);
|
|
}
|
|
|
|
deleteAuthToken(token: string): Observable<string>
|
|
{
|
|
return this.client.delete<string>(
|
|
Host.URL + '/token/' + token,
|
|
{headers: {Authorization: 'Bearer ' + token}}
|
|
);
|
|
}
|
|
}
|