Skip to content

Instantly share code, notes, and snippets.

@saimon24
Created January 24, 2019 04:15
Show Gist options
  • Save saimon24/3323d3002f085c0bc26fbf3d0b39db17 to your computer and use it in GitHub Desktop.
Save saimon24/3323d3002f085c0bc26fbf3d0b39db17 to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
// Typescript custom enum for search types (optional)
export enum SearchType {
all = '',
movie = 'movie',
series = 'series',
episode = 'episode'
}
@Injectable({
providedIn: 'root'
})
export class MovieService {
url = 'http://www.omdbapi.com/';
apiKey = ''; // <-- Enter your own key here!
/**
* Constructor of the Service with Dependency Injection
* @param http The standard Angular HttpClient to make requests
*/
constructor(private http: HttpClient) { }
/**
* Get data from the OmdbApi
* map the result to return only the results that we need
*
* @param {string} title Search Term
* @param {SearchType} type movie, series, episode or empty
* @returns Observable with the search results
*/
searchData(title: string, type: SearchType): Observable<any> {
return this.http.get(`${this.url}?s=${encodeURI(title)}&type=${type}&apikey=${this.apiKey}`).pipe(
map(results => results['Search'])
);
}
/**
* Get the detailed information for an ID using the "i" parameter
*
* @param {string} id imdbID to retrieve information
* @returns Observable with detailed information
*/
getDetails(id) {
return this.http.get(`${this.url}?i=${id}&plot=full&apikey=${this.apiKey}`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment