Skip to content

Instantly share code, notes, and snippets.

@cironunes
Last active August 19, 2017 15:23
Show Gist options
  • Save cironunes/3a1769ad0977ca80e4eadd9731945892 to your computer and use it in GitHub Desktop.
Save cironunes/3a1769ad0977ca80e4eadd9731945892 to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/throw';
export interface User {
login: string;
}
export interface Issue {}
export interface SearchResults<T> {
items: T[];
}
@Injectable()
export class GithubApiService {
readonly API_URL = 'https://api.github.com';
readonly WHAT = ['repositories', 'commits', 'code', 'issues', 'users'];
constructor(private http: HttpClient) { }
getUsers() {
return this.http.get<User[]>(`${this.API_URL}/users`);
}
search<T>(what: string, params: HttpParams): Observable<SearchResults<T>> {
if (this.WHAT.indexOf(what) === -1) {
return Observable.throw(`Searching for ${what} is not supported. The available types are: ${this.WHAT.join(', ')}.`);
}
return this.http.get<SearchResults<T>>(`${this.API_URL}/search/${what}`, { params });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment