BETTERZON-33: Implementing API service class

This commit is contained in:
Patrick Müller 2020-12-02 09:57:03 +01:00
parent e91a0085f6
commit 24c64dd709
8 changed files with 99 additions and 39 deletions

View File

@ -2472,14 +2472,6 @@
"resolved": "https://registry.npmjs.org/aws4/-/aws4-1.10.1.tgz", "resolved": "https://registry.npmjs.org/aws4/-/aws4-1.10.1.tgz",
"integrity": "sha512-zg7Hz2k5lI8kb7U32998pRRFin7zJlkfezGJjUc2heaD4Pw2wObakCDVzkKztTm/Ln7eiVvYsjqak0Ed4LkMDA==" "integrity": "sha512-zg7Hz2k5lI8kb7U32998pRRFin7zJlkfezGJjUc2heaD4Pw2wObakCDVzkKztTm/Ln7eiVvYsjqak0Ed4LkMDA=="
}, },
"axios": {
"version": "0.21.0",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.0.tgz",
"integrity": "sha512-fmkJBknJKoZwem3/IKSSLpkdNXZeBu5Q7GA/aRsr2btgrptmSCxi2oFjZHqGdK9DoTil9PIHlPIZw2EcRJXRvw==",
"requires": {
"follow-redirects": "^1.10.0"
}
},
"axobject-query": { "axobject-query": {
"version": "2.0.2", "version": "2.0.2",
"resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-2.0.2.tgz", "resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-2.0.2.tgz",
@ -5333,7 +5325,8 @@
"follow-redirects": { "follow-redirects": {
"version": "1.13.0", "version": "1.13.0",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.0.tgz", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.0.tgz",
"integrity": "sha512-aq6gF1BEKje4a9i9+5jimNFIpq4Q1WiwBToeRK5NvZBd/TRsmW8BsJfOEGkr76TbOyPVD3OVDN910EcUNtRYEA==" "integrity": "sha512-aq6gF1BEKje4a9i9+5jimNFIpq4Q1WiwBToeRK5NvZBd/TRsmW8BsJfOEGkr76TbOyPVD3OVDN910EcUNtRYEA==",
"dev": true
}, },
"for-in": { "for-in": {
"version": "1.0.2", "version": "1.0.2",

View File

@ -20,7 +20,6 @@
"@angular/platform-browser": "^10.2.3", "@angular/platform-browser": "^10.2.3",
"@angular/platform-browser-dynamic": "^10.2.3", "@angular/platform-browser-dynamic": "^10.2.3",
"@angular/router": "^10.2.3", "@angular/router": "^10.2.3",
"axios": "^0.21.0",
"ng": "0.0.0", "ng": "0.0.0",
"rxjs": "~6.6.0", "rxjs": "~6.6.0",
"tslib": "^2.0.3", "tslib": "^2.0.3",
@ -42,8 +41,8 @@
"karma-jasmine": "~4.0.0", "karma-jasmine": "~4.0.0",
"karma-jasmine-html-reporter": "^1.5.0", "karma-jasmine-html-reporter": "^1.5.0",
"protractor": "~7.0.0", "protractor": "~7.0.0",
"typescript": "<4.1.0",
"ts-node": "~8.3.0", "ts-node": "~8.3.0",
"tslint": "~6.1.0" "tslint": "~6.1.0",
"typescript": "<4.1.0"
} }
} }

View File

@ -0,0 +1,16 @@
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();
});
});

View File

@ -0,0 +1,27 @@
import {Injectable} from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import process from 'process';
import {Product} from './models/product';
import {Observable, of} from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class ApiService {
apiUrl = 'https://backend.betterzon.xyz';
constructor(
private http: HttpClient
) {
}
getProducts(): Observable<Product[]> {
try {
const prods = this.http.get<Product[]>((this.apiUrl + '/products'));
console.log(prods);
return prods;
} catch (exception) {
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
}
}
}

View File

@ -1,22 +1,25 @@
import { BrowserModule } from '@angular/platform-browser'; import {BrowserModule} from '@angular/platform-browser';
import { NgModule } from '@angular/core'; import {HttpClientModule} from '@angular/common/http';
import {NgModule} from '@angular/core';
import { AppComponent } from './app.component'; import {AppComponent} from './app.component';
import { HelloWorldComponent } from './hello-world/hello-world.component'; import {HelloWorldComponent} from './hello-world/hello-world.component';
import { AppRoutingModule } from './app-routing.module'; import {AppRoutingModule} from './app-routing.module';
import { ProductListComponent } from './product-list/product-list.component'; import {ProductListComponent} from './product-list/product-list.component';
@NgModule({ @NgModule({
declarations: [ declarations: [
AppComponent, AppComponent,
HelloWorldComponent, HelloWorldComponent,
ProductListComponent ProductListComponent
], ],
imports: [ imports: [
BrowserModule, BrowserModule,
AppRoutingModule AppRoutingModule,
], HttpClientModule
providers: [], ],
bootstrap: [AppComponent] providers: [],
bootstrap: [AppComponent]
}) })
export class AppModule { } export class AppModule {
}

View File

@ -0,0 +1,14 @@
export interface Product {
product_id: number;
asin: string;
is_active: boolean;
name: string;
short_description: string;
long_description: string;
image_guid: string;
date_added: Date;
last_modified: Date;
manufacturer_id: number;
selling_rank: string;
category_id: number;
}

View File

@ -1,4 +1,4 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<p *ngFor="let person of testList"> <p *ngFor="let product of products">
{{person}} {{product.name}}
</p> </p>

View File

@ -1,18 +1,26 @@
import { Component, OnInit } from '@angular/core'; import {Component, OnInit} from '@angular/core';
import axios, {AxiosInterceptorManager, AxiosRequestConfig, AxiosResponse} from 'axios';
import process from 'process';
import {ApiService} from '../api.service';
import {Product} from '../models/product';
@Component({ @Component({
selector: 'app-product-list', selector: 'app-product-list',
templateUrl: './product-list.component.html', templateUrl: './product-list.component.html',
styleUrls: ['./product-list.component.css'] styleUrls: ['./product-list.component.css']
}) })
export class ProductListComponent implements OnInit { export class ProductListComponent implements OnInit {
testList: string[]; products: Product[];
constructor() { constructor(private apiService: ApiService) {
} }
ngOnInit(): void { ngOnInit(): void {
this.testList = ['Herbert', 'Sascha', 'Rolf']; this.getProducts();
}
getProducts(): void {
this.apiService.getProducts().subscribe(products => this.products = products);
} }
} }