From d9368899219b8d67c9cb3cc68057eb4be469253e Mon Sep 17 00:00:00 2001 From: hgomez Date: Sat, 4 Nov 2017 17:43:04 -0400 Subject: [PATCH 01/72] first version --- libraries/pom.xml | 15 ++++ .../DailyMotionExample.java | 74 +++++++++++++++++++ .../googlehttpclientguide/DailyMotionUrl.java | 22 ++++++ .../baeldung/googlehttpclientguide/Video.java | 23 ++++++ .../googlehttpclientguide/VideoFeed.java | 20 +++++ .../googlehttpclientguide/logging.properties | 10 +++ 6 files changed, 164 insertions(+) create mode 100644 libraries/src/main/java/com/baeldung/googlehttpclientguide/DailyMotionExample.java create mode 100644 libraries/src/main/java/com/baeldung/googlehttpclientguide/DailyMotionUrl.java create mode 100644 libraries/src/main/java/com/baeldung/googlehttpclientguide/Video.java create mode 100644 libraries/src/main/java/com/baeldung/googlehttpclientguide/VideoFeed.java create mode 100644 libraries/src/main/java/com/baeldung/googlehttpclientguide/logging.properties diff --git a/libraries/pom.xml b/libraries/pom.xml index 25c1113fea..9384f58493 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -622,6 +622,21 @@ bcpkix-jdk15on 1.58 + + com.google.http-client + google-http-client + 1.23.0 + + + com.google.http-client + google-http-client-jackson2 + 1.23.0 + + + com.google.http-client + google-http-client-gson + 1.23.0 + diff --git a/libraries/src/main/java/com/baeldung/googlehttpclientguide/DailyMotionExample.java b/libraries/src/main/java/com/baeldung/googlehttpclientguide/DailyMotionExample.java new file mode 100644 index 0000000000..dd559f3aa2 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/googlehttpclientguide/DailyMotionExample.java @@ -0,0 +1,74 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.baeldung.googlehttpclientguide; + +import com.google.api.client.http.HttpRequest; +import com.google.api.client.http.HttpRequestFactory; +import com.google.api.client.http.HttpRequestInitializer; +import com.google.api.client.http.HttpResponseException; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.JsonObjectParser; +import com.google.api.client.json.gson.GsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; + +/** + * + * @author Hugo + */ +public class DailyMotionExample { + + static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); + static final JsonFactory JSON_FACTORY = new JacksonFactory(); + //static final JsonFactory JSON_FACTORY = new GsonFactory(); + + private static void run() throws Exception { + HttpRequestFactory requestFactory + = HTTP_TRANSPORT.createRequestFactory( + new HttpRequestInitializer() { + + public void initialize(HttpRequest request) { + request.setParser(new JsonObjectParser(JSON_FACTORY)); + } + }); + DailyMotionUrl url = new DailyMotionUrl("https://api.dailymotion.com/videos/"); + url.fields = "id,tags,title,url"; + HttpRequest request = requestFactory.buildGetRequest(url); + VideoFeed videoFeed = request.execute().parseAs(VideoFeed.class); + if (videoFeed.list.isEmpty()) { + System.out.println("No videos found."); + } else { + if (videoFeed.hasMore) { + System.out.print("First "); + } + System.out.println(videoFeed.list.size() + " videos found:"); + for (Video video : videoFeed.list) { + System.out.println(); + System.out.println("-----------------------------------------------"); + System.out.println("ID: " + video.id); + System.out.println("Title: " + video.title); + System.out.println("Tags: " + video.tags); + System.out.println("URL: " + video.url); + } + } + } + + public static void main(String[] args) { + try { + try { + run(); + return; + } catch (HttpResponseException e) { + System.err.println(e.getMessage()); + } + } catch (Throwable t) { + t.printStackTrace(); + } + System.exit(1); + } + +} diff --git a/libraries/src/main/java/com/baeldung/googlehttpclientguide/DailyMotionUrl.java b/libraries/src/main/java/com/baeldung/googlehttpclientguide/DailyMotionUrl.java new file mode 100644 index 0000000000..0f77cdd2f6 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/googlehttpclientguide/DailyMotionUrl.java @@ -0,0 +1,22 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.baeldung.googlehttpclientguide; + +import com.google.api.client.http.GenericUrl; +import com.google.api.client.util.Key; + +/** + * + * @author Hugo + */ + public class DailyMotionUrl extends GenericUrl { + + public DailyMotionUrl(String encodedUrl) { + super(encodedUrl); + } + + @Key public String fields; + } diff --git a/libraries/src/main/java/com/baeldung/googlehttpclientguide/Video.java b/libraries/src/main/java/com/baeldung/googlehttpclientguide/Video.java new file mode 100644 index 0000000000..62622deaf5 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/googlehttpclientguide/Video.java @@ -0,0 +1,23 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.baeldung.googlehttpclientguide; + +import com.google.api.client.util.Key; +import java.util.List; + +/** + * + * @author Hugo + */ + public class Video { + @Key public String id; + + @Key public List tags; + + @Key public String title; + + @Key public String url; + } \ No newline at end of file diff --git a/libraries/src/main/java/com/baeldung/googlehttpclientguide/VideoFeed.java b/libraries/src/main/java/com/baeldung/googlehttpclientguide/VideoFeed.java new file mode 100644 index 0000000000..6b62f61924 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/googlehttpclientguide/VideoFeed.java @@ -0,0 +1,20 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.baeldung.googlehttpclientguide; + +import com.google.api.client.util.Key; +import java.util.List; + +/** + * + * @author Hugo + */ + public class VideoFeed { + @Key public List