Add API service

This commit is contained in:
Maksym Pavlenko
2019-06-02 16:42:35 -07:00
parent fa9730de3e
commit 4d4f6d0508
3 changed files with 53 additions and 0 deletions
+12
View File
@@ -0,0 +1,12 @@
import { TestBed } from '@angular/core/testing';
import { APIService } from './api.service';
describe('APIService', () => {
beforeEach(() => TestBed.configureTestingModule({}));
it('should be created', () => {
const service: APIService = TestBed.get(APIService);
expect(service).toBeTruthy();
});
});
+39
View File
@@ -0,0 +1,39 @@
import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {retry} from 'rxjs/operators';
import {Observable} from 'rxjs';
export interface CreateRequest {
url: string;
format: string;
quality: string;
page_size: number;
}
export interface CreateResponse {
id: string;
}
export interface UserResponse {
user_id: string;
feature_level: number;
full_name: string;
}
@Injectable({
providedIn: 'root'
})
export class APIService {
constructor(private http: HttpClient) {}
createFeed(request: CreateRequest): Observable<CreateResponse> {
return this.http.post<CreateResponse>('/api/create', request);
}
getUser(): Observable<UserResponse> {
return this.http.get<UserResponse>('/api/user')
.pipe(
retry(3)
);
}
}
+2
View File
@@ -6,6 +6,7 @@ import { AppComponent } from './app.component';
import { IndexComponent } from './index/index.component';
import { InputComponent } from './input/input.component';
import {FormsModule} from '@angular/forms';
import {HttpClientModule} from '@angular/common/http';
@NgModule({
declarations: [
@@ -15,6 +16,7 @@ import {FormsModule} from '@angular/forms';
],
imports: [
BrowserModule,
HttpClientModule,
FormsModule,
AppRoutingModule
],