Skip to content

Commit

Permalink
移除 Spring Boot
Browse files Browse the repository at this point in the history
  • Loading branch information
NotFound403 committed Jun 26, 2023
1 parent 798361f commit ca070a6
Show file tree
Hide file tree
Showing 16 changed files with 252 additions and 91 deletions.
4 changes: 0 additions & 4 deletions rx-wecom-sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,84 @@

package cn.felord.reactive.api;

import cn.felord.domain.wedrive.BufferSource;
import cn.felord.domain.wedrive.FileDownloadResponse;
import cn.felord.domain.wedrive.FileId;
import cn.felord.domain.wedrive.SelectedTicket;
import cn.felord.retrofit.RetrofitFactory;
import io.reactivex.rxjava3.core.Single;
import retrofit2.http.Body;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.http.Header;
import retrofit2.http.POST;
import retrofit2.http.Url;

/**
* The type File manager api.
*
* @author dax
* @since 2023 /3/17 15:02
* @author xiafang
* @since 2023 /6/26 17:07
*/
public interface FileManagerApi {
public class FileManagerApi {
private final InternalFileManagerApi internalFileManagerApi;
private final DownloadApi downloadApi;

/**
* Instantiates a new File manager api.
*
* @param retrofit the retrofit
*/
FileManagerApi(Retrofit retrofit) {
this.internalFileManagerApi = retrofit.create(InternalFileManagerApi.class);
this.downloadApi = RetrofitFactory.RETROFIT_.create(DownloadApi.class);
}

/**
* 下载文件(通过文件fileid)
*
* @param fileid the fileid
* @return the file download response
*/
@POST("wedrive/file_download")
Single<FileDownloadResponse> getFileUrlByFileId(@Body FileId fileid);
public Single<BufferSource> downloadByFileId(String fileid) {
return internalFileManagerApi.getFileUrlByFileId(new FileId(fileid))
.flatMap(this::download);
}

/**
* 下载文件(微盘和文件选择器jsapi返回的selectedTicket)
* <p>
* 返回的下载链接和cookie值,通过下载链接带上{@code cookieName=cookieValue}Cookie请求头下载
*
* @param selectedTicket the selected ticket
* @return the file download response
*/
@POST("wedrive/file_download")
Single<FileDownloadResponse> getFileUrlBySelectedTicket(@Body SelectedTicket selectedTicket);
public Single<BufferSource> downloadBySelectedTicket(String selectedTicket) {
return internalFileManagerApi.getFileUrlBySelectedTicket(new SelectedTicket(selectedTicket))
.flatMap(this::download);
}

/* *//**
* 下载微盘文件
*
* @param downloadUrl the download url
* @param cookie the cookie
* @return the single
* @see #getFileUrlByFileId(FileId)
* @see #getFileUrlBySelectedTicket(SelectedTicket)
*//*
@GET
Single<ResponseBody> download(@Url String downloadUrl, @Header("Cookie") String cookie);*/
private Single<BufferSource> download(FileDownloadResponse downloadResponse) {
String downloadUrl = downloadResponse.getDownloadUrl();
String cookie = downloadResponse.getCookieName()
.concat("=")
.concat(downloadResponse.getCookieValue());
return downloadApi.download(downloadUrl, cookie)
.map(body ->
new BufferSource(body.contentType(), body.contentLength(), body.source()));

}

/**
* 微盘文件下载
*/
interface DownloadApi {
/**
* 文件下载
*
* @param downloadUrl the download url
* @param cookie the cookie
* @return the response body
*/
@POST
Single<ResponseBody> download(@Url String downloadUrl, @Header("Cookie") String cookie);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2023. felord.cn
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
* Website:
* https://felord.cn
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.felord.reactive.api;

import cn.felord.domain.wedrive.FileDownloadResponse;
import cn.felord.domain.wedrive.FileId;
import cn.felord.domain.wedrive.SelectedTicket;
import io.reactivex.rxjava3.core.Single;
import retrofit2.http.Body;
import retrofit2.http.POST;

/**
* The type File manager api.
*
* @author dax
* @since 2023 /3/17 15:02
*/
interface InternalFileManagerApi {

/**
* 下载文件(通过文件fileid)
*
* @param fileid the fileid
* @return the file download response
*/
@POST("wedrive/file_download")
Single<FileDownloadResponse> getFileUrlByFileId(@Body FileId fileid);

/**
* 下载文件(微盘和文件选择器jsapi返回的selectedTicket)
* <p>
* 返回的下载链接和cookie值,通过下载链接带上{@code cookieName=cookieValue}Cookie请求头下载
*
* @param selectedTicket the selected ticket
* @return the file download response
*/
@POST("wedrive/file_download")
Single<FileDownloadResponse> getFileUrlBySelectedTicket(@Body SelectedTicket selectedTicket);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
import cn.felord.domain.GenericResponse;
import cn.felord.domain.media.MediaResponse;
import cn.felord.domain.media.MediaUploadRequest;
import cn.felord.enumeration.FileMediaType;
import cn.felord.enumeration.MediaAttachmentType;
import cn.felord.enumeration.MediaTypeEnum;
import cn.felord.utils.FileMediaType;
import io.reactivex.rxjava3.core.Single;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
Expand Down Expand Up @@ -127,7 +127,7 @@ public Single<GenericResponse<String>> uploadByUrl(MediaUploadRequest request) {
}

private MultipartBody toMultipartBody(File file) {
String mediaTypeStr = FileMediaType.fromFileName(file.getName());
String mediaTypeStr = FileMediaType.fromFileName(file.getName()).mediaType();
RequestBody requestBody = RequestBody.create(file, MediaType.parse(mediaTypeStr));
return new MultipartBody.Builder()
.setType(MultipartBody.FORM)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import cn.felord.domain.WeComResponse;
import cn.felord.domain.media.MediaResponse;
import cn.felord.domain.webhook.WebhookBody;
import cn.felord.utils.FileMediaType;
import cn.felord.enumeration.FileMediaType;
import io.reactivex.rxjava3.core.Single;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
Expand Down Expand Up @@ -65,7 +65,7 @@ public <B extends WebhookBody> Single<WeComResponse> send(String key, B body) {
* @return the media response
*/
public Single<MediaResponse> uploadMedia(String webhookKey, File file) {
String mediaTypeStr = FileMediaType.fromFileName(file.getName());
String mediaTypeStr = FileMediaType.fromFileName(file.getName()).mediaType();
RequestBody requestBody = RequestBody.create(file, MediaType.parse(mediaTypeStr));
MultipartBody media = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
Expand Down
2 changes: 1 addition & 1 deletion samples/spring-boot-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</parent>
<groupId>cn.felord.wecom</groupId>
<artifactId>spring-boot-sample</artifactId>
<version>1.0.12</version>
<version>1.0.14</version>
<name>wecom-sdk-spring-boot-sample</name>
<description>wecom-sdk-spring-boot-sample</description>
<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
/*
* Copyright (c) 2023. felord.cn
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
* Website:
* https://felord.cn
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.felord.wecom.api;

import cn.felord.AgentDetails;
import cn.felord.DefaultAgent;
import cn.felord.api.WorkWeChatApi;
import cn.felord.domain.approval.ApprovalDetail;
import cn.felord.domain.oa.ApprovalSpNo;
import cn.felord.enumeration.NativeAgent;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -32,7 +48,7 @@ public class ApprovalController {
@GetMapping("/{spNo}")
public ApprovalDetail approvalDetails(@PathVariable String spNo) {
AgentDetails nfsApproval = DefaultAgent.nativeAgent("企业ID", "审批密钥", NativeAgent.APPROVAL);
return workWeChatApi.approvalApi(nfsApproval).queryApprovalDetail(spNo).getData();
return workWeChatApi.approvalApi(nfsApproval).queryApprovalDetail(new ApprovalSpNo(spNo)).getData();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2023. felord.cn
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
* Website:
* https://felord.cn
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.felord.domain.wedrive;

import lombok.AllArgsConstructor;
import lombok.Getter;
import okhttp3.MediaType;
import okio.BufferedSource;

/**
* @author dax
* @since 2023/6/26
*/
@AllArgsConstructor
@Getter
public class BufferSource {
private final MediaType contentType;
private final long contentLength;
private final BufferedSource source;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
* limitations under the License.
*/

package cn.felord.utils;
package cn.felord.enumeration;

import cn.felord.utils.StringUtils;
import okhttp3.MediaType;

import java.util.Arrays;
import java.util.Objects;
Expand Down Expand Up @@ -124,18 +127,51 @@ public enum FileMediaType {


/**
* From file name media type.
* 根据文件扩展名检索对应的FileMediaType
*
* @param fileName the file name
* @return the media type
*/
public static String fromFileName(String fileName) {
public static FileMediaType fromFileName(String fileName) {
String filenameExtension = StringUtils.getFilenameExtension(fileName);
return Arrays.stream(FileMediaType.values())
.filter(fileMediaType ->
Objects.equals(fileMediaType.extension, filenameExtension))
.findAny()
.orElse(FileMediaType.ALL).mediaType;
.orElse(FileMediaType.ALL);

}

/**
* 根据MediaType查找FileMediaType
*
* @param mediaType the media type
* @return the file media type
*/
public static FileMediaType extension(MediaType mediaType) {
return Arrays.stream(FileMediaType.values())
.filter(fileMediaType ->
Objects.equals(fileMediaType.mediaType, mediaType.type()))
.findAny()
.orElse(FileMediaType.ALL);

}

/**
* Extension string.
*
* @return the string
*/
public String extension() {
return extension;
}

/**
* Media type string.
*
* @return the string
*/
public String mediaType() {
return mediaType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public final class RetrofitFactory {
.addConverterFactory(JACKSON_CONVERTER_FACTORY)
.build();

private RetrofitFactory() {
}

/**
* Create retrofit.
*
Expand Down
4 changes: 0 additions & 4 deletions wecom-sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Loading

0 comments on commit ca070a6

Please sign in to comment.