Skip to content

Commit

Permalink
Merge pull request #227 from bounswe/nasaapi
Browse files Browse the repository at this point in the history
Nasaapi
  • Loading branch information
alperenDagi authored May 12, 2023
2 parents 493e831 + f473d95 commit 30366e4
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 0 deletions.
124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.a1.disasterresponse.controller;

import com.a1.disasterresponse.service.NASAImageService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/nasa")
public class NASAImageController {

@Autowired
private NASAImageService nasaImageService;

@GetMapping("/search")
public String searchImages(@RequestParam String text) {
return nasaImageService.searchImages(text);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.a1.disasterresponse.service;


import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class NASAImageService {

public String searchImages(String text) {
String endpoint = "https://images-api.nasa.gov/search";
String searchUrl = endpoint + "?q=" + text + "&media_type=image";


HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity<String> entity = new HttpEntity<>(headers);

RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.exchange(searchUrl, HttpMethod.GET, entity, String.class);

return response.getBody();
}
}
3 changes: 3 additions & 0 deletions practice-app/frontend/disasterresponse/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import GoogleGeocodePage from './pages/GoogleGeocodeApi/GoogleGeocodePage';
import ImdbPage from './pages/ImdbApi/ImdbPage';
import GeoJsPage from './pages/GeoJsApi/GeoJsPage';
import GoogleTranslationPage from './pages/TranslationApi/GoogleTranslationPage';
import NasaImage from './pages/NASAImageApi/NasaImage';
import WikidataPage from "./pages/WikidataApi/WikidataPage";

const navLinks = [
Expand All @@ -17,7 +18,9 @@ const navLinks = [
{ path: '/ImdbPage', label: ' IMDB API', component: ImdbPage, icon: <FaImdb /> },
{ path: '/geoJsPage', label: ' GeoJs API', component: GeoJsPage, icon: <FaWifi /> },
{ path: '/googleTranslationPage', label: ' Google Translation API', component: GoogleTranslationPage, icon: <FaLanguage /> },
{ path: '/nasaImage', label: ' NASA Image API', component: NasaImage, icon: <FaHome /> },
{ path: '/wikidataPage', label: ' Wikidata Api', component: WikidataPage, icon: <FaWikipediaW /> },

// add the rest of APIs
];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React, { useState } from 'react';

export default function Kubra() {
const [inputText, setInputText] = useState('moon');
const [responseText, setResponseText] = useState('');

const handleInputChange = (event) => {
setInputText(event.target.value);
};

const [text, setText] = useState('moon');

const handleRequest = () => {
fetch(`api/nasa/search?text=${inputText}`, {
method: 'GET',

})
.then((response) => {
console.log('Success:', response);
return response.json();
})
.then((data) => {
setResponseText(data);
console.log('Success: sadfasd', data);
})
.catch((error) => {
console.error('Error:', error);
});
};

return (
<div>
<input
type="text"
placeholder="Input Text"
value={inputText}
onChange={handleInputChange}
/>
<button onClick={handleRequest}>
Send Request
</button>
{
responseText?.collection?.items?.map((item) => {
return <div key={item.data[0].nasa_id}>
<div>{item.data[0].title}</div>
<img src={item.links[0].href} alt={item.data[0].title}></img>
</div>
})
}
</div>
);
};

0 comments on commit 30366e4

Please sign in to comment.