I'm new to Angular4 from a Python background and trying to understand the best pattern for creating a HTTPClient Service then consuming that service in a component.
My service looks like this:
@Injectable()
export class DatasetService {
constructor(private http: HttpClient) { }
getDatasets(): Observable<Array<Dataset>> {
// Get list of all datastes
return this.http.get<Array<Dataset>>('http://localhost:5000/api/v2/admin/dataset/list');
}
}
My component consumes the service as follows:
export class DataManagementComponent implements OnInit {
constructor(private datasetService: DatasetService) { }
ngOnInit() {
this.datasetService.getDatasets()
.subscribe(
data => {
console.log(data['datasets']);
},
error => {
if (error.status === 404) {console.log('No records'); }
});
}
}
This works but it feels like the service is leaky as the consumer has to map the data and handle the errors. Whereas if I was doing this in Python I'd have something like:
try:
mydata = DatasetService.getDatasets()
except as e:
print(e)