Skip to content

Commit

Permalink
Consume backend example app in frontend (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Hebing committed Nov 19, 2019
1 parent 569e15f commit 745a762
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ export class HomeComponent implements OnInit {
constructor(private backend: BackendService) { }

ngOnInit() {
// This is just an example call to /api/whatever
// Note that the backend service doesn't call the backend yet
// and simply returns some mock data
this.backend.get('whatever').then(hoorays => {
// This is just an example call to /api/example/
this.backend.get('example').then(hoorays => {
if (hoorays.length) {
this.hooray = hoorays[0].message;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,23 @@ export class BackendService {

constructor(private config: ConfigService, private http: HttpClient) { }

/**
/**
* Collect JSON from an specific url.
* @param objectUrl The part of the URL after the backendUrl from config.json.
* (i.e. whatever comes after, for example, '/api/')
* (i.e. whatever comes after, for example, '/api/').
* Note that this method will add a '/' at the end of the url if it does not exist.
*/
get(objectUrl: string): Promise<any> {
return this.getApiUrl().then(baseUrl => {
if (!objectUrl.endsWith('/')) { objectUrl = `${objectUrl}/`; }
const url: string = encodeURI(baseUrl + objectUrl);

// TODO: remove this part and enable below to actually contact the backend
return Promise.resolve([{
message: 'https://media.giphy.com/media/yoJC2GnSClbPOkV0eA/source.gif'
}]);

// return this.http.get(url)
// .toPromise()
// .then(response => {
// return response;
// })
// .catch(this.handleError);
return this.http.get(url)
.toPromise()
.then(response => {
return response;
})
.catch(this.handleError);
});
}

Expand Down

0 comments on commit 745a762

Please sign in to comment.