Commit 9810ca5f authored by Ali Saeed's avatar Ali Saeed

calling external API using restTemplete from invoicing-service

parent 9d5b9120
server:
port: 9090
spring:
application:
name: api-gateway
cloud:
gateway:
routes:
- id: invoicing-service
uri: lb://invoicing-service
predicates:
- Path=/invoices/**
- id: users-service
uri: lb://users-service
predicates:
- Path=/users/**
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
default-zone: http://localhost:8761/eureka/
# instance:
# hostname: localhost
# preferIpAddress: true
\ No newline at end of file
......@@ -3,6 +3,9 @@ package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
......@@ -10,4 +13,11 @@ public class InvoicingServiceApplication {
public static void main(String[] args) {
SpringApplication.run(InvoicingServiceApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
......@@ -3,6 +3,12 @@ package org.example.resources;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.example.models.Invoice;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
......@@ -37,11 +43,7 @@ public class ExternalAPI {
"}";
ArrayList<Invoice> invoices = new ArrayList<Invoice>();
String response = null;
try {
response = sendRequestToAPI(prompt);
} catch (IOException e) {
throw new RuntimeException(e);
}
response = sendRequestToAPI(prompt);
String jsonString = "";
int firstIndex = response.toString().indexOf("[");
......@@ -58,11 +60,7 @@ public class ExternalAPI {
"}";
ArrayList<Invoice> invoices = new ArrayList<Invoice>();
String response = null;
try {
response = sendRequestToAPI(prompt);
} catch (IOException e) {
throw new RuntimeException(e);
}
response = sendRequestToAPI(prompt);
String jsonString = "";
int firstIndex = response.toString().indexOf("[");
int endIndex = response.toString().indexOf("]");
......@@ -78,11 +76,7 @@ public class ExternalAPI {
"}";
ArrayList<Invoice> invoices = new ArrayList<Invoice>();
String response = null;
try {
response = sendRequestToAPI(prompt);
} catch (IOException e) {
throw new RuntimeException(e);
}
response = sendRequestToAPI(prompt);
String jsonString = "";
int firstIndex = response.toString().indexOf("[");
int endIndex = response.toString().indexOf("]");
......@@ -93,30 +87,42 @@ public class ExternalAPI {
return invoices;
}
public static String sendRequestToAPI(String prompt) throws IOException{
URL url = new URL("https://fumes-api.onrender.com/llama3");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);
String requestBody = prompt;
try (OutputStream outputStream = connection.getOutputStream()) {
byte[] input = requestBody.getBytes("utf-8");
outputStream.write(input, 0, input.length);
}
StringBuilder response = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"))) {
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
}
catch (IOException e){
System.out.println(e.getMessage());
}
connection.disconnect();
System.out.println(response.toString());
return response.toString();
// public static String sendRequestToAPI(String prompt) throws IOException{
// URL url = new URL("https://fumes-api.onrender.com/llama3");
// HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// connection.setRequestMethod("POST");
// connection.setRequestProperty("Content-Type", "application/json");
// connection.setDoOutput(true);
// String requestBody = prompt;
// try (OutputStream outputStream = connection.getOutputStream()) {
// byte[] input = requestBody.getBytes("utf-8");
// outputStream.write(input, 0, input.length);
// }
// StringBuilder response = new StringBuilder();
// try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"))) {
// String line;
// while ((line = reader.readLine()) != null) {
// response.append(line);
// }
// }
// catch (IOException e){
// System.out.println(e.getMessage());
// }
// connection.disconnect();
// System.out.println(response.toString());
// return response.toString();
//
// }
public static String sendRequestToAPI(String prompt) {
RestTemplate restTemplate = new RestTemplate();
String url = "https://fumes-api.onrender.com/llama3";
org.springframework.http.HttpHeaders headers = new org.springframework.http.HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> requestEntity = new HttpEntity<>(prompt, headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
String response = responseEntity.getBody();
System.out.println(response);
return response;
}
}
package org.example.resources;
import org.example.models.Invoice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.util.ArrayList;
......@@ -10,6 +12,8 @@ import java.util.List;
@RestController
@RequestMapping("/invoices")
public class InvoiceResource {
@Autowired
RestTemplate restTemplate;
@GetMapping("/getInvoiceDetails/{invoiceId}")
public Invoice getInvoiceDetails(@PathVariable("invoiceId") String invoiceId){
......
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