|
| 1 | +package com.howtoprogram.repository.okhttp; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.howtoprogram.domain.Book; |
| 5 | + |
| 6 | +import okhttp3.MediaType; |
| 7 | +import okhttp3.OkHttpClient; |
| 8 | +import okhttp3.Request; |
| 9 | +import okhttp3.RequestBody; |
| 10 | +import okhttp3.Response; |
| 11 | + |
| 12 | +public class BookRepositoryImplOkHttp { |
| 13 | + public static final MediaType MediaTypeJSON = MediaType.parse("application/json; charset=utf-8"); |
| 14 | + |
| 15 | + private static final String URI_BOOK = "http://localhost:8080/v1/books"; |
| 16 | + |
| 17 | + public void deleteBook(Long id) throws Exception { |
| 18 | + OkHttpClient httpclient = new OkHttpClient(); |
| 19 | + Request request = new Request.Builder().url(URI_BOOK + "/" + id).delete().build(); |
| 20 | + try (Response response = httpclient.newCall(request).execute()) { |
| 21 | + if (!response.isSuccessful()) { |
| 22 | + throw new RuntimeException("Failed to delete book with id:" + id); |
| 23 | + |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + public Book updateBook(Book book) throws Exception { |
| 29 | + |
| 30 | + Book updatedBook = null; |
| 31 | + OkHttpClient httpclient = new OkHttpClient(); |
| 32 | + ObjectMapper mapper = new ObjectMapper(); |
| 33 | + String jsonBook = mapper.writeValueAsString(book); |
| 34 | + Request request = new Request.Builder().url(URI_BOOK + "/" + book.getId()) |
| 35 | + .put(RequestBody.create(MediaTypeJSON, jsonBook)).build(); |
| 36 | + try (Response response = httpclient.newCall(request).execute()) { |
| 37 | + if (response.isSuccessful()) { |
| 38 | + updatedBook = mapper.readValue(response.body().bytes(), Book.class); |
| 39 | + |
| 40 | + } |
| 41 | + } |
| 42 | + return updatedBook; |
| 43 | + } |
| 44 | + |
| 45 | + public Book createBook(Book book) throws Exception { |
| 46 | + Book createdBook = null; |
| 47 | + OkHttpClient httpclient = new OkHttpClient(); |
| 48 | + ObjectMapper mapper = new ObjectMapper(); |
| 49 | + String jsonBook = mapper.writeValueAsString(book); |
| 50 | + Request request = new Request.Builder().url(URI_BOOK).post(RequestBody.create(MediaTypeJSON, jsonBook)).build(); |
| 51 | + try (Response response = httpclient.newCall(request).execute()) { |
| 52 | + |
| 53 | + if (response.isSuccessful()) { |
| 54 | + // Get back the created book |
| 55 | + createdBook = mapper.readValue(response.body().bytes(), Book.class); |
| 56 | + |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return createdBook; |
| 61 | + } |
| 62 | + |
| 63 | + public Book[] getAllBooks() throws Exception { |
| 64 | + Book[] books = null; |
| 65 | + OkHttpClient httpclient = new OkHttpClient(); |
| 66 | + Request request = new Request.Builder().url(URI_BOOK).get().build(); |
| 67 | + try (Response response = httpclient.newCall(request).execute()) { |
| 68 | + ObjectMapper mapper = new ObjectMapper(); |
| 69 | + books = mapper.readValue(response.body().bytes(), Book[].class); |
| 70 | + } |
| 71 | + return books; |
| 72 | + } |
| 73 | + |
| 74 | + public static void main(String[] args) throws Exception { |
| 75 | + BookRepositoryImplOkHttp bookRepository = new BookRepositoryImplOkHttp(); |
| 76 | + // Getting the first book from the RESTful service |
| 77 | + Book book = bookRepository.getAllBooks()[0]; |
| 78 | + bookRepository.deleteBook(book.getId()); |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + public Book findBookById(Long id) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments