Sharing the API service call details code.
import { HttpClient, HttpHeaders, HttpParams, HttpErrorResponse } from '@angular/common/http';
import { ToastController, LoadingController } from '@ionic/angular';
import { Storage } from '@ionic/storage';
import { BalaEnv } from './bala-env-vars';
import { catchError } from 'rxjs/operators';
import { AlertController } from '@ionic/angular';
import { error } from 'util';
@Injectable({
providedIn: 'root'
})
export class BalaService {
auth_token: any;
httpOptions: any;
baseUrl: any;
hasInit: boolean;
userData: any;
constructor(
public http: HttpClient,
private storage: Storage,
private alertController: AlertController,
private toastController: ToastController,
private loadingCtrl: LoadingController,
) {
this.hasInit = false;
this.baseUrl = BalaEnv.BASE_API;
this.init();
}
handleError(error: HttpErrorResponse) {
let me = this;
this.loadingCtrl.dismiss();
if (error.error instanceof ErrorEvent) {
// A client-side or network error occurred. Handle it accordingly.
} else {
if (error.status == 401) {
this.presentToast(error.error.detail);
if (error.error.detail === 'Signature has expired.') {
// this.presentAlert(error.error.detail)
me.storage.get('user')
.then((user) => {
me.recallToken(user).subscribe((res: any) => {
me.storage.set('authToken', res.token);
me.init();
});
});
}
else if (error.error.message != undefined) {
me.presentToast(error.error.message)
} else {
return [];
};
} else if (error.status == 404) {
me.presentToast('Link is broken');
} else if (error.status == 400) {
Object.keys(error.error).forEach(function (key) {
me.presentToast(error.error[key][0])
});
return [];
}
else if (error.status == 500) {
this.presentToast('something went wrong');
}
}
return [];
}
async presentAlert(msg) {
const alert = await this.alertController.create({
header: 'Alert',
message: msg,
buttons: ['OK']
});
await alert.present();
}
async presentToast(msg) {
const toast = await this.toastController.create({
message: msg,
duration: 2000
});
toast.present();
}
hasAuthToken() {
return this.auth_token && true;
}
getAuthToken() {
return this.auth_token;
}
getUser() {
let t = this;
t.storage.get("userDetails")
.then((user) => {
});
}
setAuthToekn(authToken: string) {
this.auth_token = authToken;
}
setUserData(user: string) {
this.userData = user;
}
getUserData() {
return this.userData;
}
removeAuthToken() {
this.auth_token = null;
}
refreshToken(token) {
let t = this;
let httpOption = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Accept': 'application/json',
})
};
return this.http.post(this.baseUrl + 'auth/jwt/refresh/', token, httpOption)
.pipe(
catchError((error) => {
return t.handleError(error)
})
)
}
recallToken(payload) {
let t = this;
let httpOption = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Accept': 'application/json',
})
};
return this.http.post(this.baseUrl + 'auth/jwt/', payload, httpOption)
.pipe(
catchError((error) => {
return t.handleError(error)
})
)
}
init() {
let me = this;
me.storage.get('authToken')
.then((token) => {
if (token != null) {
me.hasInit = true;
me.setAuthToekn(token);
me.httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'JWT ' + token
})
};
}
else {
me.httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Accept': 'application/json',
})
};
}
});
}
setParams(params) {
let obj = new HttpParams();
for (let i in params) {
obj = obj.set(i, params[i]);
}
return obj;
}
loginService(payLoad: any) {
let me = this;
return this.http.post(this.baseUrl + 'auth/', payLoad, this.httpOptions)
.pipe(
catchError((error) => {
return me.handleError(error);
})
);
}
registrationService(payLoad: any) {
let t = this;
return this.http.post(this.baseUrl + 'auth/register/', payLoad, this.httpOptions)
.pipe(
catchError((error) => {
return t.handleError(error);
})
);
}
getLevel() {
let t = this;
return t.http.get(this.baseUrl + 'level', this.httpOptions)
.pipe(
catchError((error) => {
return t.handleError(error);
})
)
}
getLevelById(id) {
let t = this;
return t.http.get(this.baseUrl + 'level/' + id, this.httpOptions)
.pipe(
catchError((error) => {
return t.handleError(error);
})
)
}
//level/registration_submit/id/
levelRegistration(u_id, payLoad) {
let t = this;
let httpOptions = {
headers: new HttpHeaders({
'Authorization': 'JWT ' + t.getAuthToken(),
'Accept': 'application/json',
})
};
return t.http.put(this.baseUrl + 'level/registration_submit/' + u_id, payLoad, httpOptions)
.pipe(
catchError((error) => {
alert(JSON.parse(error));
return t.handleError(error);
})
)
}
//upload image / video in activity log
uploadEventMedia(payLoad){
let t = this;
let httpOptions = {
headers: new HttpHeaders({
'Authorization': 'JWT ' + t.getAuthToken(),
'Accept': 'application/json',
})
};
return t.http.post(this.baseUrl + 'activity_log/activityuploads/', payLoad, httpOptions)
.pipe(
catchError((error) => {
alert(JSON.parse(error));
return t.handleError(error);
})
)
}
uploadImage(u_id, payLoad) {
let t = this;
let httpOptions = {
headers: new HttpHeaders({
'Authorization': 'JWT ' + t.getAuthToken(),
'Content-Type': 'multipart/form-data',
'Accept': 'application/json',
})
};
return t.http.post(this.baseUrl + 'level/registration_submit_image/' + u_id, payLoad, httpOptions)
.pipe(
catchError((error) => {
alert(JSON.parse(error));
return t.handleError(error);
})
)
}
getState(payLoad) {
let t = this;
const requestparams = this.setParams(payLoad);
const options = Object.assign({}, this.httpOptions, { params: requestparams });
return t.http.get(this.baseUrl + 'location/states', options)
.pipe(
catchError((error) => {
return t.handleError(error);
})
)
}
getCity(stateid) {
let t = this;
return t.http.get(this.baseUrl + 'location/cities/' + stateid, this.httpOptions)
.pipe(
catchError((error) => {
return t.handleError(error);
})
)
}
getQuiz(lid) {
let t = this;
return t.http.get(this.baseUrl + 'level/quiz/' + lid, this.httpOptions)
.pipe(
catchError((error) => {
return t.handleError(error);
})
)
}
getCallSlot(payLoad) {
let t = this;
const requestparams = this.setParams(payLoad);
const options = Object.assign({}, this.httpOptions, { params: requestparams });
return t.http.get(this.baseUrl + 'level/call_slots/', options)
.pipe(
catchError((error) => {
return t.handleError(error);
})
)
}
scheduleSkypeCall(payload) {
let t = this;
return t.http.post(this.baseUrl + 'level/accept_tc_coc/', payload, this.httpOptions)
.pipe(
catchError((error) => {
return t.handleError(error);
})
)
}
_addStandardHeaders(header: HttpHeaders, userId) {
let t = this;
header = header.set('Content-Type', 'application/x-www-form-urlencoded');
header = header.set('Accept', 'application/json');
header = header.set('Authorization', 'JWT ' + t.getAuthToken());
header = header.set('User', userId);
return header;
}
_initializeReqOpts(reqOpts) {
if (!reqOpts) {
reqOpts = {
headers: new HttpHeaders(),
params: new HttpParams()
};
}
return reqOpts;
}
submitQuiz(payload) {
let t = this;
return t.http.post(this.baseUrl + 'level/quiz_result_submit/', payload, this.httpOptions)
.pipe(
catchError((error) => {
return t.handleError(error);
})
)
}
//upload video
uploadVideo(id, payLoad) {
let t = this;
let httpOptions = {
headers: new HttpHeaders({
'Authorization': 'JWT ' + t.getAuthToken(),
'Accept': 'application/json',
})
};
return t.http.post(this.baseUrl + 'level/video_link_save_video/' + id, payLoad, httpOptions)
.pipe(
catchError((error) => {
return t.handleError(error);
})
)
}
//upload video link
uploadVideoLink(id, payLoad) {
let t = this;
return t.http.post(this.baseUrl + 'level/video_link_save_video/' + id, payLoad, t.httpOptions)
.pipe(
catchError((error) => {
return t.handleError(error);
})
)
}
//getFaqs
getVideoLink(id) {
let t = this;
return t.http.get(this.baseUrl + 'level/video_link_save/' + id, t.httpOptions)
.pipe(
catchError((error) => {
return t.handleError(error);
})
)
}
getFaqs(payLoad) {
let t = this;
const requestparams = this.setParams(payLoad);
const options = Object.assign({}, this.httpOptions, { params: requestparams });
return t.http.get(this.baseUrl + 'faq/', options)
.pipe(
catchError((error) => {
return t.handleError(error);
})
)
}
//getSpalshMessage
getSpalshMessage() {
let t = this;
let httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Accept': 'application/json',
})
};
return t.http.get(this.baseUrl + 'general_settings/splash_msg/', httpOptions)
.pipe(
catchError((error) => {
return t.handleError(error);
})
)
}
//meet and greet
getMeetAndGreet() {
let t = this;
return t.http.get(this.baseUrl + 'team_member/', this.httpOptions)
.pipe(
catchError((error) => {
return t.handleError(error);
})
)
}
getTeamMember(id) {
let t = this;
return t.http.get(this.baseUrl + 'team_member/' + id, this.httpOptions)
.pipe(
catchError((error) => {
return t.handleError(error);
})
)
}
addActivityLog(payload){
let t=this;
return t.http.post(this.baseUrl+'activity_log/',payload,this.httpOptions)
.pipe(
catchError((error)=>{
return t.handleError(error);
})
)
}
getAllActivityLog() {
let t = this;
return t.http.get(this.baseUrl + 'activity_log/all/', this.httpOptions)
.pipe(
catchError((error) => {
return t.handleError(error);
})
)
}
getActivityLog(id) {
let t = this;
return t.http.get(this.baseUrl + 'activity_log/' + id, this.httpOptions)
.pipe(
catchError((error) => {
return t.handleError(error);
})
)
}
addweaverandrelease(payload) {
let t = this;
return t.http.post(this.baseUrl + 'level/weaverandrelease/', payload, this.httpOptions)
.pipe(
catchError((error) => {
return t.handleError(error);
})
)
}
addreleaseliability(payload) {
let t = this;
return t.http.post(this.baseUrl + 'level/releaseliability/', payload, this.httpOptions)
.pipe(
catchError((error) => {
return t.handleError(error);
})
)
}
checkisbaalaboss(id) {
let t = this;
return t.http.get(this.baseUrl + 'level/isbaalaboss/' + id, this.httpOptions)
.pipe(catchError((error) => {
return t.handleError(error);
})
)
}
}```
This format we used for header
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'JWT ' + token
})```