Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ Each Pull Request will contain explanation and steps taken to complete a feature
## Forms

- [Reactive form, hero-edit](https://github.com/brunolm/angular-how-to/pull/12)

## Services

- [Adding Nasa service, consume Nasa API](https://github.com/brunolm/angular-how-to/pull/15)
47 changes: 34 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@angular/platform-browser-dynamic": "^6.0.5",
"@angular/router": "^6.0.5",
"bootstrap": "^4.0.0-beta.2",
"camelcase-keys": "^4.2.0",
"core-js": "^2.4.1",
"ngx-redux-state-props": "0.0.3",
"ngx-take-until-destroy": "^3.0.0",
Expand Down
15 changes: 15 additions & 0 deletions src/app/shared/nasa.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { TestBed, inject } from '@angular/core/testing';

import { NasaService } from './nasa.service';

describe('NasaService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [NasaService]
});
});

it('should be created', inject([NasaService], (service: NasaService) => {
expect(service).toBeTruthy();
}));
});
36 changes: 36 additions & 0 deletions src/app/shared/nasa.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import camelcaseKeys from 'camelcase-keys';
import { map } from 'rxjs/operators';

interface Apod {
date: string;
explanation: string;
hdurl: string;
media_type: string;
service_version: string;
title: string;
url: string;
}

@Injectable({
providedIn: 'root',
})
export class NasaService {
static baseUrl = 'https://api.nasa.gov';

static params = {
params: {
api_key: 'DEMO_KEY',
},
};

constructor(private http: HttpClient) {}

async getApod() {
return await this.http
.get(`${NasaService.baseUrl}/planetary/apod`, NasaService.params)
.pipe(map((response) => camelcaseKeys(response, { deep: true })))
.toPromise<Apod>();
}
}
3 changes: 2 additions & 1 deletion src/app/shared/shared.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { CommonModule } from '@angular/common';
import { HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';

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

@NgModule({
imports: [CommonModule, RouterModule],
imports: [CommonModule, RouterModule, HttpClientModule],
exports: [LayoutComponent, TitleComponent],
declarations: [LayoutComponent, TitleComponent],
})
Expand Down