Commit 835a937c authored by Darrick Yong's avatar Darrick Yong

add fetch service

parent d0603b01
......@@ -33,7 +33,8 @@ dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-config'
implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap:3.0.2'
implementation "io.springfox:springfox-boot-starter:3.0.0"
implementation 'io.springfox:springfox-boot-starter:3.0.0'
implementation group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
}
dependencyManagement {
......
......@@ -3,10 +3,10 @@ package com.student.details.controllers;
import com.student.details.exceptions.ResourceNotFoundException;
import com.student.details.models.Student;
import com.student.details.services.StudentService;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
......@@ -18,6 +18,12 @@ public class DetailsController {
@Autowired
StudentService studentService;
@GetMapping("/students/demo")
public JSONObject fetchDemo() {
return studentService.fetchService("https://opentdb.com/api.php?amount=10");
}
@GetMapping("/students")
public List<Student> getAllStudents() {
// System.out.println("This is the students index route.");
......
......@@ -2,13 +2,13 @@ package com.student.details.services;
import com.student.details.exceptions.ResourceNotFoundException;
import com.student.details.models.Student;
import com.student.details.repositories.StudentRepository;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
@Service
public class StudentService {
......@@ -16,12 +16,41 @@ public class StudentService {
@Autowired
StudentRepository studentRepository;
public JSONObject fetchService(String stringUrl) {
System.out.println("demo api");
try {
URL url = new URL(stringUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
int responseCode = conn.getResponseCode();
if (responseCode != 200) {
throw new RuntimeException("" + responseCode);
} else {
String res = "";
Scanner scanner = new Scanner(url.openStream());
while (scanner.hasNext()) {
String nextVal = scanner.nextLine();
res += nextVal;
}
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(res);
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return new JSONObject();
}
public List<Student> findAllStudents() {
return studentRepository.findAll();
}
public Student addStudent(Student student) {
return studentRepository.save(student);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment