This repository has been archived on 2024-11-11. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
mastodon-apps/projects/mastodon-api/src/lib/services/mastodon-api.service.ts
2022-12-23 15:15:18 +01:00

60 lines
2.0 KiB
TypeScript

import {Injectable} from "@angular/core";
import {Observable} from "rxjs";
import {HttpClient, HttpHeaders, HttpResponse} from "@angular/common/http";
@Injectable({
providedIn: 'root'
})
export class MastodonApiService {
constructor(private httpClient: HttpClient) {
}
get<T>(url: string) {
return this.httpClient.get<T>(url);
}
getAuthenticated<T>(url: string, accessToken: string): Observable<T> {
const reqHeader = new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + accessToken
});
return this.httpClient.get<T>(url, {headers: reqHeader});
}
getAuthenticatedWithResponseHeaders<T>(url: string, accessToken: string): Observable<HttpResponse<T>> {
const reqHeader = new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + accessToken
});
return this.httpClient.get<T>(url, {headers: reqHeader, observe: 'response'});
}
post<T>(url: string, parameters: object) {
return this.httpClient.post<T>(url, parameters);
}
postAuthenticated(url: string, body: {}, accessToken: string) {
const reqHeader = new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + accessToken
});
return this.httpClient.post(url, body, {headers: reqHeader});
}
putAuthenticated(url: string, body: {}, accessToken: string) {
const reqHeader = new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + accessToken
});
return this.httpClient.put(url, body, {headers: reqHeader});
}
deleteAuthenticated(url: string, body: { account_ids: string[] }, accessToken: string) {
const reqHeader = new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + accessToken
});
return this.httpClient.delete(url, {headers: reqHeader, body});
}
}