Commit 1c066444 authored by Thomas Frion's avatar Thomas Frion

[CLEAN] Api client

We created a module for api client. In app.module just import pghm-api.module
parent 27732f8a
......@@ -12,13 +12,9 @@ import { MapComponent } from './map/map.component';
import { SidebarLeftComponent } from './sidebar-left/sidebar-left.component';
import { LogoutComponent } from './logout/logout.component';
import { AuthService } from './services/auth.service';
import { AuthGuard } from './services/auth-guard.service';
import { ServiceApi } from './services/api/clients/service.api';
import { TokenApi } from './services/api/clients/token.api';
import { UnitApi } from './services/api/clients/unit.api';
import { UserApi } from './services/api/clients/user.api';
import { WebcamApi } from './services/api/clients/webcam.api';
import { AuthService } from './services/auth.service';
import { AuthGuard } from './services/auth-guard.service';
import { PghmApiModule } from './pghm-api/pghm-api.module'
@NgModule({
......@@ -35,16 +31,12 @@ import { WebcamApi } from './services/api/clients/webcam.api';
BrowserModule,
AppRoutingModule,
ReactiveFormsModule,
HttpClientModule
HttpClientModule,
PghmApiModule
],
providers: [
AuthService,
AuthGuard,
ServiceApi,
TokenApi,
UnitApi,
UserApi,
WebcamApi
AuthGuard,
],
bootstrap: [AppComponent]
})
......
......@@ -2,8 +2,8 @@ import { Component, OnInit } from '@angular/core';
import { HttpClient } from "@angular/common/http";
import * as L from 'leaflet';
import * as models from '../services/api/models/models';
import { ServiceApi } from '../services/api/clients/service.api';
import * as models from 'src/app/pghm-api/services/models/models';
import { ServiceService } from 'src/app/pghm-api/services/clients/service.service';
import { AuthGuard } from '../services/auth-guard.service';
import { environment } from '../../environments/environment';
......@@ -37,7 +37,7 @@ export class MapComponent implements OnInit {
constructor(
private auth : AuthGuard,
private serviceApi : ServiceApi,
private serviceApi : ServiceService,
private http : HttpClient
)
{
......
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ServiceService } from './services/clients/service.service'
import { TokenService } from './services/clients/token.service'
import { UnitService } from './services/clients/unit.service'
import { UserService } from './services/clients/user.service'
import { WebcamService } from './services/clients/webcam.service'
@NgModule({
declarations: [],
imports: [
CommonModule
],
providers: [
ServiceService,
TokenService,
UnitService,
UserService,
WebcamService
]
})
export class PghmApiModule { }
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { AuthGuard } from '../auth-guard.service';
import { AuthGuard } from 'src/app/services/auth-guard.service';
import { environment } from 'src/environments/environment';
/**
......
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { HttpHeaders } from '@angular/common/http';
/**
* IApiService defines the behavior of an API client
*/
export interface IApiService<T> {
export interface IClient<T> {
/**
* Returns all registered T-type elements
* @param headers HttpHeaders a custom HTTP header
......
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import * as models from '../models/models';
import { ApiService } from '../api.service';
import { IApiService } from '../api-client.interface';
import { AuthGuard } from '../../auth-guard.service';;
@Injectable()
export class ServiceApi extends ApiService<models.IService> implements IApiService<models.IService>{
constructor(protected http: HttpClient, protected authGuard : AuthGuard) {
import { ApiService } from '../api';
import { IClient } from './iclient';
import { AuthGuard } from 'src/app/services/auth-guard.service';
@Injectable({
providedIn: 'root'
})
export class ServiceService extends ApiService<models.IService> implements IClient<models.IService> {
constructor(protected http: HttpClient, protected authGuard : AuthGuard) {
super(http, authGuard);
}
......@@ -36,4 +37,4 @@ export class ServiceApi extends ApiService<models.IService> implements IApiServi
public remove(elem: models.Service, headers?: HttpHeaders): boolean {
return this.delete('/services/' + elem.id, headers);
}
}
\ No newline at end of file
}
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import * as models from '../models/models';
import { ApiService } from '../api.service';
@Injectable()
export class TokenApi extends ApiService<models.Token> {
import { ApiService } from '../api';
constructor(protected http: HttpClient) {
@Injectable({
providedIn: 'root'
})
export class TokenService extends ApiService<models.Token> {
constructor(protected http: HttpClient) {
super(http, undefined);
this.defaultHeaders = new HttpHeaders({
......@@ -19,8 +20,12 @@ export class TokenApi extends ApiService<models.Token> {
if(user.username === undefined || user.password === undefined){
throw new Error("user must contains username and password properties.");
}
return this.create('/token/', JSON.stringify(user), headers);
return this.http.post<models.Token>(
this.baseUrl + '/token/',
user,
{headers: headers},
).toPromise();
}
public async refresh(token : models.Token, headers?:HttpHeaders) : Promise<models.TokenRefresh> {
......@@ -48,5 +53,4 @@ export class TokenApi extends ApiService<models.Token> {
{headers: this.defaultHeaders}
).toPromise();
}
}
\ No newline at end of file
}
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import * as models from '../models/models';
import { ApiService } from '../api.service';
import { IApiService } from '../api-client.interface';
import { AuthGuard } from '../../auth-guard.service';
@Injectable()
export class UnitApi extends ApiService<models.Unit> implements IApiService<models.Unit>{
constructor(protected http: HttpClient, protected authGuard : AuthGuard) {
import { ApiService } from '../api';
import { IClient } from './iclient';
import { AuthGuard } from 'src/app/services/auth-guard.service';
@Injectable({
providedIn: 'root'
})
export class UnitService extends ApiService<models.Unit> implements IClient<models.Unit> {
constructor(protected http: HttpClient, protected authGuard : AuthGuard) {
super(http, authGuard);
}
......@@ -37,5 +37,4 @@ export class UnitApi extends ApiService<models.Unit> implements IApiService<mode
public remove(elem: models.Unit, headers?: HttpHeaders): boolean {
return this.delete('/units/'+elem.id, headers);
}
}
\ No newline at end of file
}
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import * as models from '../models/models';
import { ApiService } from '../api.service';
import { IApiService } from '../api-client.interface';
import { AuthGuard } from '../../auth-guard.service';
@Injectable()
export class UserApi extends ApiService<models.PghmUser> implements IApiService<models.PghmUser>{
constructor(protected http: HttpClient, protected authGuard : AuthGuard) {
import { ApiService } from '../api';
import { IClient } from './iclient';
import { AuthGuard } from 'src/app/services/auth-guard.service';
@Injectable({
providedIn: 'root'
})
export class UserService extends ApiService<models.PghmUser> implements IClient<models.PghmUser>{
constructor(protected http: HttpClient, protected authGuard : AuthGuard) {
super(http, authGuard);
}
......@@ -37,4 +37,4 @@ export class UserApi extends ApiService<models.PghmUser> implements IApiService<
public remove(elem: models.PghmUser, headers?: HttpHeaders): boolean {
return this.delete('/users/' + elem.id, headers);
}
}
\ No newline at end of file
}
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import * as models from '../models/models';
import { ApiService } from '../api';
import { IClient } from './iclient';
import { AuthGuard } from 'src/app/services/auth-guard.service';
@Injectable({
providedIn: 'root'
})
export class WebcamService extends ApiService<models.Webcam> implements IClient<models.Webcam> {
constructor(protected http: HttpClient, protected authGuard : AuthGuard) {
super(http, authGuard);
}
public all(headers?: HttpHeaders): Promise<models.Webcam[]> {
return this.get('/webcams/', headers);
}
public one(selector: any, headers?: HttpHeaders): Promise<models.Webcam> {
return this.read('/webcams/'+selector, headers);
}
public save(elem: models.Webcam, headers?: HttpHeaders): Promise<models.Webcam> {
let res : Promise<models.Webcam>;
if(elem.id !== undefined){
res = this.update('/webcas/'+elem.id, JSON.stringify(elem), headers);
} else {
res = this.create('/webcas/', JSON.stringify(elem), headers);
}
return res;
}
public remove(elem: models.Webcam, headers?: HttpHeaders): boolean {
return this.delete('/webcams/' + elem.id, headers);
}
}
import { TestBed } from '@angular/core/testing';
import { ApiService } from './api.service';
describe('ApiService', () => {
let service: ApiService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(ApiService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import * as models from '../models/models';
import { ApiService } from '../api.service';
import { IApiService } from '../api-client.interface';
import { AuthGuard } from '../../auth-guard.service';
@Injectable()
export class WebcamApi extends ApiService<models.Webcam> implements IApiService<models.Webcam> {
constructor(protected http: HttpClient, protected authGuard : AuthGuard) {
super(http, authGuard);
}
public all(headers?: HttpHeaders): Promise<models.Webcam[]> {
return this.get('/webcams/', headers);
}
public one(selector: any, headers?: HttpHeaders): Promise<models.Webcam> {
return this.read('/webcams/'+selector, headers);
}
public save(elem: models.Webcam, headers?: HttpHeaders): Promise<models.Webcam> {
let res : Promise<models.Webcam>;
if(elem.id !== undefined){
res = this.update('/webcas/'+elem.id, JSON.stringify(elem), headers);
} else {
res = this.create('/webcas/', JSON.stringify(elem), headers);
}
return res;
}
public remove(elem: models.Webcam, headers?: HttpHeaders): boolean {
return this.delete('/webcams/' + elem.id, headers);
}
}
\ No newline at end of file
......@@ -3,9 +3,9 @@ import { Router } from '@angular/router';
import { AuthComponent } from '../auth-component/auth-component.component';
import { environment } from '../../environments/environment';
import { TokenApi } from './api/clients/token.api';
import { Token } from './api/models/token';
import { CookieService } from './cookie.service';
import { TokenService } from 'src/app/pghm-api/services/clients/token.service';
import { Token } from 'src/app/pghm-api/services/models/token';
/**
* @Service :
*
......@@ -27,7 +27,7 @@ export class AuthService {
constructor(
private router : Router,
private tokenApi : TokenApi,
private tokenApi : TokenService,
) {}
/**
......@@ -74,6 +74,8 @@ export class AuthService {
}
);
},(err) => {
console.log(err);
authComp.error = err.error.detail;
document.getElementById('loader').style.display = "none";
});
......
......@@ -2,8 +2,8 @@ import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import {Fonctionnalite} from './fonctionnalite';
import { Unit } from '../services/api/models/unit';
import { environment } from '../../environments/environment';
import { Unit } from 'src/app/pghm-api/services/models/unit';
/**
* @SidebarLeftComponent :
* Component for the side bar on the left of the screen
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment