debug CommunityHandler and return first result
This commit is contained in:
		
							parent
							
								
									760b73261a
								
							
						
					
					
						commit
						3c2a1946ed
					
				@ -1,18 +1,34 @@
 | 
			
		||||
package sh.adb.RandomRedditMemesAPI;
 | 
			
		||||
 | 
			
		||||
import com.github.jreddit.parser.entity.Submission;
 | 
			
		||||
import com.github.jreddit.parser.exception.RedditParseException;
 | 
			
		||||
import com.sun.net.httpserver.HttpExchange;
 | 
			
		||||
import com.sun.net.httpserver.HttpHandler;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.io.InputStream;
 | 
			
		||||
import java.io.OutputStream;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
class CommunityHandler implements HttpHandler {
 | 
			
		||||
    RedditAPI api;
 | 
			
		||||
    public CommunityHandler(RedditAPI redditClient) {
 | 
			
		||||
        this.api = redditClient;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void handle(HttpExchange t) throws IOException {
 | 
			
		||||
        InputStream is = t.getRequestBody();
 | 
			
		||||
        //InputStream is = t.getRequestBody();
 | 
			
		||||
        //is.read();
 | 
			
		||||
        String response = "This is the response";
 | 
			
		||||
        List<Submission> submissions = null;
 | 
			
		||||
        try {
 | 
			
		||||
            submissions = this.api.getSub("ProgrammerHumor");
 | 
			
		||||
        } catch (RedditParseException e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
        }
 | 
			
		||||
        assert submissions != null;
 | 
			
		||||
        String response = submissions.get(0).getURL();
 | 
			
		||||
        System.out.println("response > "+response);
 | 
			
		||||
        t.sendResponseHeaders(200, response.length());
 | 
			
		||||
        OutputStream os = t.getResponseBody();
 | 
			
		||||
        os.write(response.getBytes());
 | 
			
		||||
 | 
			
		||||
@ -13,17 +13,18 @@ import java.net.InetSocketAddress;
 | 
			
		||||
 | 
			
		||||
public class Main {
 | 
			
		||||
    public static void main(String[] args) throws IOException, RedditOAuthException, ParseException {
 | 
			
		||||
        //start RedditAPI
 | 
			
		||||
        RedditAPI redditClient = new RedditAPI();
 | 
			
		||||
        System.out.println("RedditAPI started - the token is");
 | 
			
		||||
        System.out.println(redditClient.token.getAccessToken());
 | 
			
		||||
        //start httpServer
 | 
			
		||||
        HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
 | 
			
		||||
        server.createContext("/", new FallbackHandler());
 | 
			
		||||
        //server.createContext("/api/v1/", new StandardHandler());
 | 
			
		||||
        server.createContext("/api/v1/r/", new CommunityHandler());
 | 
			
		||||
        server.createContext("/api/v1/r/", new CommunityHandler(redditClient));
 | 
			
		||||
        server.setExecutor(null);
 | 
			
		||||
        server.start();
 | 
			
		||||
        System.out.println("server started");
 | 
			
		||||
 | 
			
		||||
        RedditAPI client = new RedditAPI();
 | 
			
		||||
        System.out.println("the token is");
 | 
			
		||||
        System.out.println(client.token.getAccessToken());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,5 +1,13 @@
 | 
			
		||||
package sh.adb.RandomRedditMemesAPI;
 | 
			
		||||
 | 
			
		||||
import com.github.jreddit.oauth.client.RedditClient;
 | 
			
		||||
import com.github.jreddit.oauth.client.RedditHttpClient;
 | 
			
		||||
import com.github.jreddit.parser.entity.Submission;
 | 
			
		||||
import com.github.jreddit.parser.exception.RedditParseException;
 | 
			
		||||
import com.github.jreddit.parser.listing.SubmissionsListingParser;
 | 
			
		||||
import com.github.jreddit.request.retrieval.param.SubmissionSort;
 | 
			
		||||
import com.github.jreddit.request.retrieval.submissions.SubmissionsOfSubredditRequest;
 | 
			
		||||
import org.apache.http.impl.client.HttpClientBuilder;
 | 
			
		||||
import org.json.simple.parser.ParseException;
 | 
			
		||||
 | 
			
		||||
import com.github.jreddit.oauth.RedditOAuthAgent;
 | 
			
		||||
@ -8,8 +16,11 @@ import com.github.jreddit.oauth.app.RedditApp;
 | 
			
		||||
import com.github.jreddit.oauth.app.RedditInstalledApp;
 | 
			
		||||
import com.github.jreddit.oauth.exception.RedditOAuthException;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
public class RedditAPI {
 | 
			
		||||
    private final RedditOAuthAgent agent;
 | 
			
		||||
    private final RedditClient client;
 | 
			
		||||
    protected RedditToken token;
 | 
			
		||||
 | 
			
		||||
    public RedditAPI() throws RedditOAuthException, ParseException  {
 | 
			
		||||
@ -24,6 +35,9 @@ public class RedditAPI {
 | 
			
		||||
        // Create OAuth agent
 | 
			
		||||
        agent = new RedditOAuthAgent(userAgent, redditApp);
 | 
			
		||||
 | 
			
		||||
        // Create request executor
 | 
			
		||||
        client = new RedditHttpClient(userAgent, HttpClientBuilder.create().build());
 | 
			
		||||
 | 
			
		||||
        // Create token (will be valid for 1 hour)
 | 
			
		||||
        token = agent.tokenAppOnly(false);
 | 
			
		||||
        System.out.println("Access Token: " + token.getAccessToken());
 | 
			
		||||
@ -37,4 +51,10 @@ public class RedditAPI {
 | 
			
		||||
    public void newToken() throws RedditOAuthException {
 | 
			
		||||
        this.token = this.agent.tokenAppOnly(false);
 | 
			
		||||
    }
 | 
			
		||||
    public List<Submission> getSub(String subreddit) throws RedditParseException {
 | 
			
		||||
        SubmissionsListingParser parser = new SubmissionsListingParser();
 | 
			
		||||
        SubmissionsOfSubredditRequest request = (SubmissionsOfSubredditRequest) new SubmissionsOfSubredditRequest(subreddit, SubmissionSort.HOT).setLimit(100);
 | 
			
		||||
        List<Submission> submissions = parser.parse(client.get(token, request));
 | 
			
		||||
        return submissions;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user