Creating  the Vision API service

To upload the image to our server, we are using postFile() on VisionAPIService. Let's create this service now. Inside the clientappservices folder, create a file named vision.api.service.ts and update it as follows:

// SNIPP SNIPP
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class VisionAPIService {

constructor(private http: HttpClient) {}

postFile(threadId: string, fileToUpload: File): Observable < any > {
const formData: FormData = new FormData();
formData.append('image-reply', fileToUpload, fileToUpload.name);
return this.http.post < any > (`/api/upload-image/${threadId}`, formData);
}
}
// SNIPP SNIPP

We need to add this service as a provider in clientappapp.module.ts. First, we will import VisionAPIService into clientappapp.module.ts:

// SNIPP SNIPP
import { VisionAPIService } from './services/vision.api.service';
// SNIPP SNIPP

Update the providers as follows:

// SNIPP SNIPP
providers: [
AuthService,
AuthGuardLogin,
AuthGuardAdmin,
UserService,
ThreadService,
MessageService,
VisionAPIService,
{
provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptor,
multi: true
}
],
// SNIPP SNIPP

Save all the files to continue.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.109.8