Skip to content

Commit fcfacc6

Browse files
authored
Merge pull request #15 from brunolm/feature/create-nasa-service
Feature/create nasa service
2 parents 222b418 + 66c5f84 commit fcfacc6

File tree

6 files changed

+92
-14
lines changed

6 files changed

+92
-14
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,7 @@ Each Pull Request will contain explanation and steps taken to complete a feature
5050
## Forms
5151

5252
- [Reactive form, hero-edit](https://github.com/brunolm/angular-how-to/pull/12)
53+
54+
## Services
55+
56+
- [Adding Nasa service, consume Nasa API](https://github.com/brunolm/angular-how-to/pull/15)

package-lock.json

Lines changed: 34 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"@angular/platform-browser-dynamic": "^6.0.5",
2929
"@angular/router": "^6.0.5",
3030
"bootstrap": "^4.0.0-beta.2",
31+
"camelcase-keys": "^4.2.0",
3132
"core-js": "^2.4.1",
3233
"ngx-redux-state-props": "0.0.3",
3334
"ngx-take-until-destroy": "^3.0.0",
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { TestBed, inject } from '@angular/core/testing';
2+
3+
import { NasaService } from './nasa.service';
4+
5+
describe('NasaService', () => {
6+
beforeEach(() => {
7+
TestBed.configureTestingModule({
8+
providers: [NasaService]
9+
});
10+
});
11+
12+
it('should be created', inject([NasaService], (service: NasaService) => {
13+
expect(service).toBeTruthy();
14+
}));
15+
});

src/app/shared/nasa.service.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { HttpClient } from '@angular/common/http';
2+
import { Injectable } from '@angular/core';
3+
import camelcaseKeys from 'camelcase-keys';
4+
import { map } from 'rxjs/operators';
5+
6+
interface Apod {
7+
date: string;
8+
explanation: string;
9+
hdurl: string;
10+
media_type: string;
11+
service_version: string;
12+
title: string;
13+
url: string;
14+
}
15+
16+
@Injectable({
17+
providedIn: 'root',
18+
})
19+
export class NasaService {
20+
static baseUrl = 'https://api.nasa.gov';
21+
22+
static params = {
23+
params: {
24+
api_key: 'DEMO_KEY',
25+
},
26+
};
27+
28+
constructor(private http: HttpClient) {}
29+
30+
async getApod() {
31+
return await this.http
32+
.get(`${NasaService.baseUrl}/planetary/apod`, NasaService.params)
33+
.pipe(map((response) => camelcaseKeys(response, { deep: true })))
34+
.toPromise<Apod>();
35+
}
36+
}

src/app/shared/shared.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { CommonModule } from '@angular/common';
2+
import { HttpClientModule } from '@angular/common/http';
23
import { NgModule } from '@angular/core';
34
import { RouterModule } from '@angular/router';
45

56
import { LayoutComponent } from './layout/layout.component';
67
import { TitleComponent } from './title/title.component';
78

89
@NgModule({
9-
imports: [CommonModule, RouterModule],
10+
imports: [CommonModule, RouterModule, HttpClientModule],
1011
exports: [LayoutComponent, TitleComponent],
1112
declarations: [LayoutComponent, TitleComponent],
1213
})

0 commit comments

Comments
 (0)