Skip to content

Instantly share code, notes, and snippets.

@euwbah
Created October 7, 2016 14:53
Show Gist options
  • Save euwbah/cb07d08c32f26db7f31c9bf005988e21 to your computer and use it in GitHub Desktop.
Save euwbah/cb07d08c32f26db7f31c9bf005988e21 to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { Flight } from '../models/flight';
import { Geocoder } from './geocoder';
/*
Generated class for the AmadeusFlightService provider.
See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular 2 DI.
*/
@Injectable()
export class AmadeusFlightService {
amadeusApiUrl = 'https://api.sandbox.amadeus.com/v1.2/';
amadeusInspiration = 'flights/inspiration-search';
amadeusExtensive = 'flights/extensive-search';
amadeusLowFare = 'flights/low-fare-search';
amadeusAffiliate = 'flights/affiliate-search';
amadeusNearestAirport = 'airports/nearest-relevant';
amadeusApiKey = 'tK2mKUg7rM6WwsU47sfL8T5NrxS3euNG';
<<<<<<< HEAD
constructor(public http: Http) {
console.log('Hello AmadeusFlightService Provider');
}
getInspirationSearch(params?: [string, string][]): Observable<Flight[]> {
let url = this.amadeusApiUrl + this.amadeusInspiration + '?apikey=' + this.amadeusApiKey;
=======
constructor(public http: Http, public geo: Geocoder) {
console.log('Hello AmadeusFlightService Provider');
}
>>>>>>> e7baee039f086f6ceb9279a4581174c706598a29
for (let key_value of params) {
url += "&" + key_value[0] + "=" + encodeURIComponent(key_value[1]);
}
console.log('Getting inspiration search from ' + url);
return this.http.get(url)
.map(this.extractData)
.catch(this.handleError);
}
getExtensiveSearch(params?: [string, string][]): Observable<Flight[]> {
let url = this.amadeusApiUrl + this.amadeusInspiration + '?apikey=' + this.amadeusApiKey;
for (let key_value of params) {
url += "&" + key_value[0] + "=" + encodeURIComponent(key_value[1]);
}
return this.http.get(url)
.map(this.extractData)
.catch(this.handleError);
}
getLowFareSearch(params?: [string, string][]): Observable<Flight[]> {
let url = this.amadeusApiUrl + this.amadeusInspiration + '?apikey=' + this.amadeusApiKey;
for (let key_value of params) {
url += "&" + key_value[0] + "=" + encodeURIComponent(key_value[1]);
}
return this.http.get(url)
.map(this.extractData)
.catch(this.handleError);
}
getAffiliateSearch(params?: [string, string][]): Observable<Flight[]> {
let url = this.amadeusApiUrl + this.amadeusInspiration + '?apikey=' + this.amadeusApiKey;
for (let key_value of params) {
url += "&" + key_value[0] + "=" + encodeURIComponent(key_value[1]);
}
return this.http.get(url)
.map(this.extractData)
.catch(this.handleError);
}
getNearestAirport(params?: [string, string][]): Observable<Flight[]> {
let url = this.amadeusApiUrl + this.amadeusInspiration + '?apikey=' + this.amadeusApiKey;
for (let key_value of params) {
url += "&" + key_value[0] + "=" + encodeURIComponent(key_value[1]);
}
return this.http.get(url)
.map(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body || {};
}
<<<<<<< HEAD
private handleError(error: any) {
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg); // log to console instead
return Observable.throw(errMsg);
}
=======
return this.http.get(url)
.map(this.extractData)
.catch(this.handleError);
}
getNearestAirport(params?: [string, string][]): Observable<Flight[]>{
let lat = null;
let lng = null;
this.geo.getLocationData(params[0][1]).subscribe(
res => {
var lat = res.results[0].geometry.location.lat;
var lng = res.results[0].geometry.location.lng;
}
);
let url = this.amadeusApiUrl + this.amadeusNearestAirport +'?apikey=' + this.amadeusApiKey + '&latitude=' + lat + '&longitude=' + lng;
return this.http.get(url)
.map(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body || { };
}
private handleError (error: any) {
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg); // log to console instead
return Observable.throw(errMsg);
}
>>>>>>> e7baee039f086f6ceb9279a4581174c706598a29
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment