Exploring the HTTP module of Angular
Now, after we have developed both forms – for importing existing and adding new developers, it is time to implement the logic behind them in the controller of the component.
For this purpose, we will need to communicate with the GitHub API. Although we can do this directly from the component's controller, by approaching the problem this way, we would couple the component with the RESTful API of GitHub. In order to enforce better separation of concerns, we can extract the logic for communication with GitHub into a separate service called GitHubGateway
. Open the file called github_gateway.ts
, and enter the following content:
import {Injectable} from '@angular/core'; import {Http} from '@angular/http'; @Injectable() export class GitHubGateway { constructor(private http: Http) {} getUser(username: string) { return this.http .get(`https://api.github.com/users/${username}`); } ...