Skip to content

Instantly share code, notes, and snippets.

@Lucs1590
Created July 22, 2022 00:40
Show Gist options
  • Save Lucs1590/7fc093bbb7af80d0ff0531df8c926631 to your computer and use it in GitHub Desktop.
Save Lucs1590/7fc093bbb7af80d0ff0531df8c926631 to your computer and use it in GitHub Desktop.
Service to connect with an API that provides the user's IP.
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, throwError } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class ApiService {
httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json', Accept: 'application/json' })
};
constructor(private httpService: HttpClient) { }
getIPInfo(): Observable<any> {
return this.httpService.get('https://ipapi.co/json/', this.httpOptions)
.pipe(
map(response => response),
catchError(() => throwError(() => 'Problem with IP info'))
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment