Skip to content

Commit

Permalink
Setup mock youtube response when on testing
Browse files Browse the repository at this point in the history
  • Loading branch information
64ArthurAraujo committed Apr 10, 2024
1 parent 5ff1ee4 commit e54f6fa
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 10 deletions.
1 change: 1 addition & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.nullAnnotationInferenceConflict=warning
org.eclipse.jdt.core.compiler.problem.nullReference=warning
org.eclipse.jdt.core.compiler.problem.nullSpecViolation=warning
org.eclipse.jdt.core.compiler.problem.nullUncheckedConversion=ignore
org.eclipse.jdt.core.compiler.problem.potentialNullReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.problem.syntacticNullAnalysisForFields=enabled
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>cc</groupId>
<artifactId>wordview</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.0.3-SNAPSHOT</version>
<name>WordViewAPI</name>

<repositories>
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/cc/wordview/api/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,38 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import cc.wordview.api.config.WordViewConfig;

@SpringBootApplication
@ComponentScan(basePackages = { "cc.wordview.api" })
public class Application implements ApplicationRunner {

private static final Logger logger = LoggerFactory.getLogger(Application.class);

@Autowired
private WordViewConfig config;

public static void main(String... args) throws Exception {
SpringApplication.run(Application.class, args);
}

@Override
public void run(ApplicationArguments args) throws Exception {
if (args.containsOption("production")) {
config.setProduction(args.containsOption("production"));

if (config.isProduction()) {
if (CORS_ORIGIN == CORS_ORIGIN_ALL) {
logger.warn("CORS_ORIGIN is set to all.");
}
} else {
logger.info("All requests to YouTube's API are being mocked");
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/cc/wordview/api/config/WordViewConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cc.wordview.api.config;

import org.springframework.stereotype.Component;

@Component
public class WordViewConfig {
private boolean production;

public boolean isProduction() {
return production;
}

public void setProduction(boolean production) {
this.production = production;
}
}
27 changes: 19 additions & 8 deletions src/main/java/cc/wordview/api/controller/MusicController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
Expand All @@ -27,6 +28,7 @@
import com.sapher.youtubedl.YoutubeDLResponse;

import cc.wordview.api.Constants;
import cc.wordview.api.config.WordViewConfig;
import cc.wordview.api.util.FileReader;
import cc.wordview.api.util.StringUtil;
import cc.wordview.ytm.YoutubeApi;
Expand All @@ -46,21 +48,30 @@ public class MusicController {
@Value("${wordview.ytm.api-key}")
private String API_KEY;

@Autowired
private WordViewConfig config;

// History is just being a placeholder here it does not provide the inteded
// functionality yet
@GetMapping("/history")
public ResponseEntity<?> history() {
return response(() -> {
ytapi.setApiKey(API_KEY);

SearchResult result = ytapi.search("ano-yume-wo-nazotte", 1).get(0);

Video video = new Video();

video.setId(result.getId().getVideoId());
video.setTitle(result.getSnippet().getTitle());
video.setArtist(result.getSnippet().getChannelTitle());
video.setCover(result.getSnippet().getThumbnails().getHigh().getUrl());
if (config.isProduction()) {
ytapi.setApiKey(API_KEY);
SearchResult result = ytapi.search("ano-yume-wo-nazotte", 1).get(0);

video.setId(result.getId().getVideoId());
video.setTitle(result.getSnippet().getTitle());
video.setArtist(result.getSnippet().getChannelTitle());
video.setCover(result.getSnippet().getThumbnails().getHigh().getUrl());
} else {
video.setId("sAuEeM_6zpk");
video.setTitle("YOASOBI「あの夢をなぞって」 Official Music Video");
video.setArtist("YOASOBI");
video.setCover("https://img.youtube.com/vi/sAuEeM_6zpk/0.jpg");
}

return ok(video);
});
Expand Down

0 comments on commit e54f6fa

Please sign in to comment.