Created
July 31, 2021 16:35
Ionic http calls demo - Ionic Native Advanced
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
import { Platform } from '@ionic/angular'; | |
import { HttpClient } from '@angular/common/http'; | |
@Component({ | |
selector: 'app-ionic-native-adv', | |
templateUrl: './ionic-native-adv.page.html', | |
styleUrls: ['./ionic-native-adv.page.scss'], | |
}) | |
export class IonicNativeAdvPage{ | |
plt: string; | |
localhost:string = 'localhost'; | |
web_get_unrestricted = 1; | |
web_post_unrestricted = 1; | |
web_put_unrestricted = 1; | |
web_delete_unrestricted = 1; | |
android_get_unrestricted = 1; | |
android_post_unrestricted = 1; | |
android_put_unrestricted = 1; | |
android_delete_unrestricted = 1; | |
ios_get_unrestricted = 1; | |
ios_post_unrestricted = 1; | |
ios_put_unrestricted = 1; | |
ios_delete_unrestricted = 1; | |
// More variables for restricted API calls | |
constructor(private platform: Platform, private http: HttpClient) { | |
this.plt = this.platform.is('mobileweb') ? 'web' : | |
this.platform.is('ios') ? 'ios' : 'android'; | |
this.localhost ="192.168.0.7" | |
} | |
get() { | |
this.http.get(`http://${this.localhost}:5000/ionic4fullapp/us-central1/getData?data=fromApp`, {}) | |
.subscribe(response => { | |
console.log(response); | |
this.changeStatus('get', 'unrestricted', 1); | |
}, error => { | |
console.log(error) | |
this.changeStatus('get', 'unrestricted', 2); | |
}) | |
} | |
post() { | |
this.http.post(`http://${this.localhost}:5000/ionic4fullapp/us-central1/postData`, { data: 'postData' }) | |
.subscribe(response => { | |
console.log(response); | |
this.changeStatus('post', 'unrestricted', 1); | |
}, error => { | |
console.log(error) | |
this.changeStatus('post', 'unrestricted', 2); | |
}) | |
} | |
put() { | |
this.http.put(`http://${this.localhost}:5000/ionic4fullapp/us-central1/putData`, { data: 'putData' }) | |
.subscribe(response => { | |
console.log(response); | |
this.changeStatus('put', 'unrestricted', 1); | |
}, error => { | |
console.log(error) | |
this.changeStatus('put', 'unrestricted', 2); | |
}) | |
} | |
delete() { | |
this.http.delete(`http://${this.localhost}:5000/ionic4fullapp/us-central1/deleteData`, {}) | |
.subscribe(response => { | |
console.log(response); | |
this.changeStatus('delete', 'unrestricted', 1); | |
}, error => { | |
console.log(error) | |
this.changeStatus('delete', 'unrestricted', 2); | |
}) | |
} | |
changeStatus(type, restriction, status) { | |
this[`${this.plt}_${type}_${restriction}`] = status | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment