Created
July 22, 2022 00:40
-
-
Save Lucs1590/7fc093bbb7af80d0ff0531df8c926631 to your computer and use it in GitHub Desktop.
Service to connect with an API that provides the user's IP.
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 { 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