Created
January 20, 2025 03:57
-
-
Save Danushka96/10103c4bb4044c68f0bfbe5e6b648333 to your computer and use it in GitHub Desktop.
Dyntube Service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package lk.klass.dyntube; | |
| import lk.klass.dyntube.dtos.CreateProjectRequest; | |
| import lk.klass.dyntube.dtos.GetProjectsResponse; | |
| import lk.klass.dyntube.dtos.GetVideosResponse; | |
| import lk.klass.dyntube.dtos.UploadDyntubeVideoResponse; | |
| import lk.klass.dyntube.models.Video; | |
| import lombok.extern.slf4j.Slf4j; | |
| import org.springframework.http.HttpEntity; | |
| import org.springframework.http.HttpHeaders; | |
| import org.springframework.http.HttpStatusCode; | |
| import org.springframework.http.MediaType; | |
| import org.springframework.http.client.MultipartBodyBuilder; | |
| import org.springframework.http.codec.multipart.FilePart; | |
| import org.springframework.util.MultiValueMap; | |
| import org.springframework.web.reactive.function.BodyInserters; | |
| import org.springframework.web.reactive.function.client.WebClient; | |
| import reactor.core.publisher.Mono; | |
| /** | |
| * @author danushka | |
| * 2023-11-26 | |
| */ | |
| @Slf4j | |
| public class DyntubeService { | |
| private final WebClient webClient; | |
| private final String apiKey; | |
| public DyntubeService(String apiKey) { | |
| String endPoint = "https://api.dyntube.com/v1/"; | |
| this.apiKey = apiKey; | |
| webClient = WebClient.builder() | |
| .baseUrl(endPoint) | |
| .defaultHeader(HttpHeaders.AUTHORIZATION, "bearer" + " " + apiKey) | |
| .build(); | |
| } | |
| public Mono<UploadDyntubeVideoResponse> uploadVideo(FilePart file) { | |
| return WebClient.builder() | |
| .baseUrl("https://upload.dyntube.com/v1/videos") | |
| .defaultHeader(HttpHeaders.AUTHORIZATION, "bearer" + " " + apiKey) | |
| .build() | |
| .post() | |
| .contentType(MediaType.MULTIPART_FORM_DATA) | |
| .body(BodyInserters.fromMultipartData(fromFile(file))) | |
| .retrieve() | |
| .onStatus(HttpStatusCode::isError, clientResponse -> | |
| clientResponse.bodyToMono(String.class).handle((error, sink) -> log.error(error))) | |
| .bodyToMono(UploadDyntubeVideoResponse.class); | |
| } | |
| public Mono<Video> getVideo(String videoId) { | |
| return webClient | |
| .get() | |
| .uri(String.format("videos/%s", videoId)) | |
| .retrieve() | |
| .bodyToMono(Video.class); | |
| } | |
| public Mono<GetVideosResponse> getVideos() { | |
| return webClient | |
| .get() | |
| .uri("videos") | |
| .retrieve() | |
| .bodyToMono(GetVideosResponse.class); | |
| } | |
| public Mono<GetProjectsResponse> getProjects() { | |
| return webClient | |
| .get() | |
| .uri("projects") | |
| .retrieve() | |
| .bodyToMono(GetProjectsResponse.class); | |
| } | |
| public Mono<String> createProject(CreateProjectRequest request) { | |
| return webClient | |
| .post() | |
| .uri("projects") | |
| .body(BodyInserters.fromValue(request)) | |
| .retrieve() | |
| .bodyToMono(String.class); | |
| } | |
| private MultiValueMap<String, HttpEntity<?>> fromFile(FilePart file) { | |
| MultipartBodyBuilder builder = new MultipartBodyBuilder(); | |
| builder.part("file", file); | |
| return builder.build(); | |
| } | |
| public Mono<Boolean> deleteVideo(String videoId) { | |
| return webClient | |
| .delete() | |
| .uri(String.format("videos/%s", videoId)) | |
| .retrieve() | |
| .toBodilessEntity() | |
| .map(r -> true) | |
| .defaultIfEmpty(false); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment