Advertisement
andersonalmada2

Untitled

Aug 12th, 2022
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. package br.ufc.mandacaru5.service;
  2.  
  3. import java.io.IOException;
  4. import java.util.List;
  5.  
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Service;
  8.  
  9. import com.google.gson.Gson;
  10.  
  11. import br.ufc.mandacaru5.model.Post;
  12. import br.ufc.mandacaru5.util.Properties;
  13. import okhttp3.Call;
  14. import okhttp3.MediaType;
  15. import okhttp3.OkHttpClient;
  16. import okhttp3.Request;
  17. import okhttp3.RequestBody;
  18. import okhttp3.Response;
  19.  
  20. @Service
  21. public class PostService {
  22.    
  23.     @Autowired
  24.     Properties properties;
  25.  
  26.     private static final String BASE_URL = "https://jsonplaceholder.typicode.com/posts";
  27.     private Gson gson;
  28.  
  29.     public PostService() {
  30.         gson = new Gson();
  31.     }
  32.  
  33.     public Post findPostByRequest(int id) {
  34.         OkHttpClient client = new OkHttpClient.Builder().build();
  35.  
  36.         Request request = new Request.Builder().url(BASE_URL + "/" + id).build();
  37.  
  38.         Call call = client.newCall(request);
  39.  
  40.         try {
  41.             Response response = call.execute();        
  42.             Post post = gson.fromJson(response.body().string(), Post.class);           
  43.             properties.setPost(post.getTitle());
  44.            
  45.             return post;
  46.         } catch (IOException e) {
  47.             e.printStackTrace();
  48.         }
  49.  
  50.         return null;
  51.     }
  52.  
  53.     public List<Post> findAllPostsByRequest() {
  54.         OkHttpClient client = new OkHttpClient.Builder().build();
  55.  
  56.         Request request = new Request.Builder().url(BASE_URL).build();
  57.  
  58.         Call call = client.newCall(request);
  59.  
  60.         try {
  61.             Response response = call.execute();
  62.             return gson.fromJson(response.body().string(), List.class);
  63.         } catch (IOException e) {
  64.             e.printStackTrace();
  65.         }
  66.  
  67.         return null;
  68.     }
  69.  
  70.     public Post saveByRequest(Post post) {
  71.         OkHttpClient client = new OkHttpClient.Builder().build();
  72.  
  73.         String postJson = gson.toJson(post);
  74.         MediaType mediaType = MediaType.parse("application/json");
  75.         RequestBody body = RequestBody.create(postJson, mediaType);
  76.  
  77.         Request request = new Request.Builder().url(BASE_URL).post(body).build();
  78.  
  79.         Call call = client.newCall(request);
  80.  
  81.         try {
  82.             Response response = call.execute();
  83.             return gson.fromJson(response.body().string(), Post.class);
  84.         } catch (IOException e) {
  85.             e.printStackTrace();
  86.         }
  87.  
  88.         return null;
  89.     }
  90.    
  91.     public Post updateByRequest(int id, Post post) {
  92.         OkHttpClient client = new OkHttpClient.Builder().build();
  93.  
  94.         String postJson = gson.toJson(post);
  95.         MediaType mediaType = MediaType.parse("application/json");
  96.         RequestBody body = RequestBody.create(postJson, mediaType);
  97.  
  98.         Request request = new Request.Builder().url(BASE_URL + "/" + id).put(body).build();
  99.  
  100.         Call call = client.newCall(request);
  101.  
  102.         try {
  103.             Response response = call.execute();;
  104.             return gson.fromJson(response.body().string(), Post.class);
  105.         } catch (IOException e) {
  106.             e.printStackTrace();
  107.         }
  108.  
  109.         return null;
  110.     }
  111.    
  112.     public void deleteByRequest(int id) {
  113.         OkHttpClient client = new OkHttpClient.Builder().build();
  114.  
  115.         Request request = new Request.Builder().url(BASE_URL + "/" + id).delete().build();
  116.  
  117.         Call call = client.newCall(request);
  118.  
  119.         try {
  120.             call.execute();
  121.         } catch (IOException e) {
  122.             e.printStackTrace();
  123.         }
  124.     }
  125. }
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement