chore(): remove Ionic's HTTP service

This commit is contained in:
Adam Bradley
2015-10-15 14:31:37 -05:00
parent 75e71de22b
commit dd15279227
12 changed files with 11 additions and 305 deletions

View File

@ -1,6 +1,6 @@
import {Component, Control, ControlGroup} from 'angular2/angular2'; import {Component, Control, ControlGroup} from 'angular2/angular2';
import {IonicApp, App, Http} from 'ionic/ionic'; import {IonicApp, App} from 'ionic/ionic';
import {Translate, TranslatePipe} from 'ionic/ionic'; import {Translate, TranslatePipe} from 'ionic/ionic';

View File

@ -1,6 +1,6 @@
import {Component, Control, ControlGroup} from 'angular2/angular2'; import {Component, Control, ControlGroup} from 'angular2/angular2';
import {IonicApp, App, Http} from 'ionic/ionic'; import {IonicApp, App} from 'ionic/ionic';
import {Camera, Geolocation, Vibration, Battery, Device, Events} from 'ionic/ionic'; import {Camera, Geolocation, Vibration, Battery, Device, Events} from 'ionic/ionic';

View File

@ -1,65 +0,0 @@
import {Component, Control, ControlGroup} from 'angular2/angular2';
import {App, Http} from 'ionic/ionic';
let testUrl = 'https://ionic-api-tester.herokuapp.com/json';
let testUrl404 = 'https://ionic-api-tester.herokuapp.com/404';
@App({
templateUrl: 'main.html'
})
class IonicApp {
constructor() {
this.form = new ControlGroup({
requestData: new Control('')
})
}
doGet() {
Http.get('http://swapi.co/api/people/1').then((resp) => {
this.resp = resp;
}, (err) => {
this.err = err;
})
}
doGet404() {
Http.get(testUrl404).then((resp) => {
this.resp = resp;
}, (err) => {
this.err = err;
})
}
doPost() {
let data = this.form.controls.requestData.value;
Http.post(testUrl, data).then((resp) => {
this.resp = resp;
}, (err) => {
this.err = err;
})
}
doPut() {
let data = this.form.controls.requestData.value;
Http.put(testUrl, data).then((resp) => {
this.resp = resp;
}, (err) => {
this.err = err;
})
}
doDelete() {
let data = this.form.controls.requestData.value;
Http.delete(testUrl, data).then((resp) => {
this.resp = resp;
}, (err) => {
this.err = err;
})
}
doPatch() {
let data = this.form.controls.requestData.value;
Http.patch(testUrl, data).then((resp) => {
this.resp = resp;
}, (err) => {
this.err = err;
})
}
}

View File

@ -1,21 +0,0 @@
<ion-view>
<div id="profile" #profile>
<div id="face">
</div>
</div>
<ion-content [parallax]="profile"><!-- [counter]="counter">-->
<button (click)="doGet()">GET</button>
<button (click)="doGet404()">GET (404)</button>
<button (click)="doPost()">POST</button>
<button (click)="doPut()">PUT</button>
<button (click)="doDelete()">DELETE</button>
<button (click)="doPatch()">PATCH</button>
<div [ng-form-model]="form">
<textarea ng-control="requestData" placeholder="JSON to send (optional)" style="border: 1px solid #ccc; width: 100%; height: 140px"></textarea>
</div>
<div style="margin: 15px">
{{resp}}
</div>
</ion-content>
</ion-view>

View File

@ -1,4 +1,4 @@
import {App, IonicApp, Http} from 'ionic/ionic'; import {App, IonicApp} from 'ionic/ionic';
@App({ @App({
@ -17,6 +17,7 @@ class MyApp {
let baseUrl = 'https://api.flickr.com/services/rest/'; let baseUrl = 'https://api.flickr.com/services/rest/';
// TODO: update to use angular2's HTTP Service
Http.get(baseUrl + '?method=flickr.groups.pools.getPhotos&group_id=1463451@N25&safe_search=1&api_key=' + FLICKR_API_KEY + '&jsoncallback=JSON_CALLBACK&format=json&tags=' + tags, { Http.get(baseUrl + '?method=flickr.groups.pools.getPhotos&group_id=1463451@N25&safe_search=1&api_key=' + FLICKR_API_KEY + '&jsoncallback=JSON_CALLBACK&format=json&tags=' + tags, {
method: 'jsonp' method: 'jsonp'
}).then((val) => { }).then((val) => {

View File

@ -1,6 +1,6 @@
import {Component, Control, ControlGroup} from 'angular2/angular2'; import {Component, Control, ControlGroup} from 'angular2/angular2';
import {App, Http, Storage, LocalStorage, SqlStorage} from 'ionic/ionic'; import {App, Storage, LocalStorage, SqlStorage} from 'ionic/ionic';
let testUrl = 'https://ionic-api-tester.herokuapp.com/json'; let testUrl = 'https://ionic-api-tester.herokuapp.com/json';
let testUrl404 = 'https://ionic-api-tester.herokuapp.com/404'; let testUrl404 = 'https://ionic-api-tester.herokuapp.com/404';

View File

@ -1,5 +1,3 @@
import {Http} from 'ionic/net/http';
let FLICKR_API_KEY = '504fd7414f6275eb5b657ddbfba80a2c'; let FLICKR_API_KEY = '504fd7414f6275eb5b657ddbfba80a2c';
let baseUrl = 'https://api.flickr.com/services/rest/'; let baseUrl = 'https://api.flickr.com/services/rest/';
@ -24,6 +22,7 @@ export class Flickr {
} }
static search(tags, lat, lng) { static search(tags, lat, lng) {
// TODO: update to angular2 HTTP service
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
Http.get(baseUrl + '?method=flickr.groups.pools.getPhotos&group_id=1463451@N25&safe_search=1&api_key=' + FLICKR_API_KEY + '&jsoncallback=JSON_CALLBACK&format=json&tags=' + tags + '&lat=' + lat + '&lng=' + lng, { Http.get(baseUrl + '?method=flickr.groups.pools.getPhotos&group_id=1463451@N25&safe_search=1&api_key=' + FLICKR_API_KEY + '&jsoncallback=JSON_CALLBACK&format=json&tags=' + tags + '&lat=' + lat + '&lng=' + lng, {
method: 'jsonp' method: 'jsonp'

View File

@ -1,11 +1,10 @@
import {Http} from 'ionic/net/http';
let WUNDERGROUND_API_KEY = '1cc2d3de40fa5af0'; let WUNDERGROUND_API_KEY = '1cc2d3de40fa5af0';
let FORECASTIO_KEY = '4cd3c5673825a361eb5ce108103ee84a'; let FORECASTIO_KEY = '4cd3c5673825a361eb5ce108103ee84a';
export class Weather { export class Weather {
static getAtLocation(lat, lng) { static getAtLocation(lat, lng) {
// TODO: update to angular2 HTTP service
let url = 'https://api.forecast.io/forecast/' + FORECASTIO_KEY + '/'; let url = 'https://api.forecast.io/forecast/' + FORECASTIO_KEY + '/';
return Http.get(url + lat + ',' + lng + '?callback=JSON_CALLBACK', { return Http.get(url + lat + ',' + lng + '?callback=JSON_CALLBACK', {
method: 'jsonp' method: 'jsonp'

View File

@ -1,4 +1,4 @@
import {App, IonicApp, Http} from 'ionic/ionic'; import {App, IonicApp} from 'ionic/ionic';
@App({ @App({
@ -17,6 +17,7 @@ class MyApp {
let baseUrl = 'https://api.flickr.com/services/rest/'; let baseUrl = 'https://api.flickr.com/services/rest/';
// TODO: update to use angular2's HTTP Service
Http.get(baseUrl + '?method=flickr.groups.pools.getPhotos&group_id=1463451@N25&safe_search=1&api_key=' + FLICKR_API_KEY + '&jsoncallback=JSON_CALLBACK&format=json&tags=' + tags, { Http.get(baseUrl + '?method=flickr.groups.pools.getPhotos&group_id=1463451@N25&safe_search=1&api_key=' + FLICKR_API_KEY + '&jsoncallback=JSON_CALLBACK&format=json&tags=' + tags, {
method: 'jsonp' method: 'jsonp'
}).then((val) => { }).then((val) => {

View File

@ -1,10 +1,7 @@
import {App, IonicApp, Http} from 'ionic/ionic'; import {App} from 'ionic/ionic';
@App({ @App({
templateUrl: 'main.html' templateUrl: 'main.html'
}) })
class MyApp { class MyApp {}
constructor(private app: IonicApp) {
}
}

View File

@ -4,8 +4,6 @@ export * from './config/config'
export * from './config/modes' export * from './config/modes'
export * from './config/decorators' export * from './config/decorators'
export * from './net/http'
export * from './components' export * from './components'
export * from './platform/platform' export * from './platform/platform'

View File

@ -1,203 +0,0 @@
import * as util from 'ionic/util';
//TODO(mlynch): surely, there must be another way, sir?
window._jsonpcallbacks = {
counter: 0
};
/**
* The Http class makes it easy to send GET/POST/PUT/DELETE/PATCH requests
* and send/receive JSON (or anything else) through a simple API.
*
* Http uses the `fetch()` API underneath, or a polyfill if it's not natively supported.
*/
export class Http {
/**
* The raw fetch() operation.
*
* Generally, you want to use one of get()/post()/put()/delete() but
* this is useful if you want to do something crazy.
*
* @param url the URL to pass to fetch
* @param options the options to configure the fetch
* @return es6 promise from the fetch.
*/
static fetch(url, options) {
return window.fetch(url, options).then((response) => {
// status "0" to handle local files fetching (e.g. Cordova/Phonegap etc.)
if (response.status === 200 || response.status === 0) {
// We have a good response, let's check the response headers and return
// deserialized JSON or return the text from the response.
if (response.headers.get('Content-Type') === 'application/json') {
return response.json();
}
return response.text();
} else {
return Promise.reject(response, new Error(response.statusText));
}
}).catch((err) => {
return Promise.reject(err);
});
}
static jsonp(url, callbackId, options) {
return new Promise((resolve, reject) => {
var script = document.createElement('script');
script.src = url;
script.async = true;
script.type = 'text/javascript';
var callback = (event) => {
script.removeEventListener('load', callback);
script.removeEventListener('error', callback);
document.body.removeChild(script);
let text, status;
if(event) {
if(event.type === "load" && !window._jsonpcallbacks[callbackId].called) {
event = { type: "error" };
}
text = event.type;
status = event.type === "error" ? 404 : 200;
resolve(window._jsonpcallbacks[callbackId].data, status, text);
} else {
reject();
}
/*
var jsonpDone = jsonpReq(url.replace('JSON_CALLBACK', 'angular.callbacks.' + callbackId),
callbackId, function(status, text) {
completeRequest(callback, status, callbacks[callbackId].data, "", text);
callbacks[callbackId] = noop;
});
*/
};
script.addEventListener('load', callback);
script.addEventListener('error', callback);
document.body.appendChild(script);
return callback;
});
}
static _method(method, url, data, options, sendsJson) {
options = util.defaults(options, {
method: method,
headers: {
'Accept': 'application/json,text/plain,*/*',
},
});
if(options.body) {
options.body = (typeof data === 'string') ? data : JSON.stringify(data)
}
if(sendsJson) {
options.headers['Content-Type'] = 'application/json';
}
if(options.method == 'jsonp') {
// Adopted from Angular 1
let callbacks = window._jsonpcallbacks;
var callbackId = '_' + (callbacks.counter++).toString(36);
callbacks[callbackId] = function(data) {
callbacks[callbackId].data = data;
callbacks[callbackId].called = true;
};
/*
var jsonpDone = jsonpReq(url.replace('JSON_CALLBACK', 'angular.callbacks.' + callbackId),
callbackId, function(status, text) {
completeRequest(callback, status, callbacks[callbackId].data, "", text);
callbacks[callbackId] = noop;
});
*/
url = url.replace('JSON_CALLBACK', '_jsonpcallbacks.' + callbackId);
return Http.jsonp(url, callbackId, options);
} else {
return Http.fetch(url, options);
}
}
/**
* Send a GET request to the given URL.
*
* By default, options sends the `Accept` header as `application/json,text/plain,* / *`,
*
* @param url the URL to POST to
* @param options the options to configure the post with.
* @return promise
*/
static get(url, options = {}) {
return Http._method('get', url, {}, options);
}
/**
* Send a POST request to the given URL.
*
* By default, options sends the `Accept` header as `application/json,text/plain,* / *`,
* and the `Content-Type` header as `application/json`
*
* @param url the URL to POST to
* @param options the options to configure the post with.
* @return promise
*/
static post(url, data = {}, options = {}) {
return Http._method('post', url, data, options, true);
}
/**
* Send a PUT request to the given URL.
*
* By default, options sends the `Accept` header as `application/json,text/plain,* / *`,
* and the `Content-Type` header as `application/json`
*
* @param url the URL to PUT to
* @param data the JSON data to send
* @param options the options to configure the post with.
* @return promise
*/
static put(url, data = {}, options = {}) {
return Http._method('put', url, data, options, true);
}
/**
* Send a DELETE request to the given URL.
*
* By default, options sends the `Accept` header as `application/json,text/plain,* / *`,
* and the `Content-Type` header as `application/json`
*
* @param url the URL to DELETE to
* @param data the JSON data to send
* @param options the options to configure the post with.
* @return promise
*/
static delete(url, data = {}, options = {}) {
return Http._method('delete', url, data, options, true);
}
/**
* Send a PATH request to the given URL.
*
* By default, options sends the `Accept` header as `application/json,text/plain,* / *`,
* and the `Content-Type` header as `application/json`
*
* @param url the URL to PATH to
* @param options the options to configure the post with.
* @return promise
*/
static patch(url, data = {}, options = {}) {
return Http._method('patch', url, data, options, true);
}
}