Skip to content

Commit

Permalink
Merge pull request #129 from yvasyliev/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
yvasyliev committed Sep 16, 2022
2 parents 3516b06 + c7e962c commit 60d6669
Show file tree
Hide file tree
Showing 23 changed files with 193 additions and 117 deletions.
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/receiving-vk-api-exeption.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ and I'm receiving (replace with response from your logs):
Here is the code example (replace with your code example):
```java
public void sendMessage() throws VkApiException {
Send.Response response = vk.messages.send()
Send.ResponseBody responseBody = vk.messages.send()
.setMessage("Sending you message")
.execute();

System.out.println("Response: " + response);
System.out.println("Response: " + responseBody);
}
```

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ This library uses the next third-party dependencies:
<dependency>
<groupId>com.github.yvasyliev</groupId>
<artifactId>java-vk-bots-longpoll-api</artifactId>
<version>3.4.5</version>
<version>3.4.6</version>
</dependency>
```
4. Extend `LongPollBot` class and override necessary methods:
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>com.github.yvasyliev</groupId>
<artifactId>java-vk-bots-longpoll-api</artifactId>
<packaging>jar</packaging>
<version>3.4.5</version>
<version>3.4.6</version>
<name>Java VK Bots Long Poll API</name>
<description>A Java library to create VK bots using Bots Long Poll API</description>
<url>https://github.com/yvasyliev/java-vk-bots-long-poll-api</url>
Expand Down Expand Up @@ -34,7 +34,7 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.0</version>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/api/longpoll/bots/LongPollBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void startPolling() throws VkApiException {
initialize();
while (polling) {
try {
GetUpdates.Response updates = getUpdates.execute();
GetUpdates.ResponseBody updates = getUpdates.execute();
getUpdates.setTs(updates.getTs());
handle(updates.getEvents());
} catch (VkApiHttpException | VkApiResponseException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import api.longpoll.bots.model.objects.additional.UploadedFile;

import java.io.InputStream;
import java.util.function.Supplier;

/**
* Uploads a document to a message using {@link InputStream}.
Expand All @@ -16,18 +15,18 @@ public class InputStreamUploadableMessageDoc extends UploadableMessageDoc {
private final InputStream inputStream;

/**
* Document extension.
* Document name.
*/
private final String extension;
private final String filename;

public InputStreamUploadableMessageDoc(InputStream inputStream, String extension, Supplier<Integer> peerIdSupplier, String accessToken) {
super(peerIdSupplier, accessToken);
public InputStreamUploadableMessageDoc(InputStream inputStream, String filename, Integer peerId, String accessToken) {
super(peerId, accessToken);
this.inputStream = inputStream;
this.extension = extension;
this.filename = filename;
}

@Override
public UploadedFile upload() throws VkApiException {
return uploadFile("Doc" + System.currentTimeMillis() + "." + extension, inputStream);
return uploadFile(filename, inputStream);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import api.longpoll.bots.model.objects.additional.UploadedFile;

import java.io.InputStream;
import java.util.function.Supplier;

/**
* Uploads a photo to massage using {@link InputStream}.
* Uploads a photo to a message using {@link InputStream}.
*/
public class InputStreamUploadableMessagePhoto extends UploadableMessagePhoto {
/**
Expand All @@ -16,18 +15,18 @@ public class InputStreamUploadableMessagePhoto extends UploadableMessagePhoto {
private final InputStream inputStream;

/**
* Photo extension.
* Photo name.
*/
private final String extension;
private final String filename;

public InputStreamUploadableMessagePhoto(InputStream inputStream, String extension, Supplier<Integer> peerIdSupplier, String accessToken) {
super(peerIdSupplier, accessToken);
public InputStreamUploadableMessagePhoto(InputStream inputStream, String filename, Integer peerId, String accessToken) {
super(peerId, accessToken);
this.inputStream = inputStream;
this.extension = extension;
this.filename = filename;
}

@Override
public UploadedFile upload() throws VkApiException {
return uploadFile("Photo" + System.currentTimeMillis() + "." + extension, inputStream);
return uploadFile(filename, inputStream);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.function.Supplier;

/**
* Uploads a document to message using {@link Path}.
Expand All @@ -18,8 +17,8 @@ public class PathUploadableMessageDoc extends UploadableMessageDoc {
*/
private final Path path;

public PathUploadableMessageDoc(Path path, Supplier<Integer> peerIdSupplier, String accessToken) {
super(peerIdSupplier, accessToken);
public PathUploadableMessageDoc(Path path, Integer peerId, String accessToken) {
super(peerId, accessToken);
this.path = path;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.function.Supplier;

/**
* Uploads a photo to message using {@link Path}.
Expand All @@ -18,8 +17,8 @@ public class PathUploadableMessagePhoto extends UploadableMessagePhoto {
*/
private final Path path;

public PathUploadableMessagePhoto(Path path, Supplier<Integer> peerIdSupplier, String accessToken) {
super(peerIdSupplier, accessToken);
public PathUploadableMessagePhoto(Path path, Integer peerId, String accessToken) {
super(peerId, accessToken);
this.path = path;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package api.longpoll.bots.helpers.attachments;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Supplies a list of {@link UploadableFile}.
*/
public class UploadableFilesSupplier implements Supplier<List<UploadableFile>> {
/**
* List of {@code peer_id}.
*/
private final List<Integer> peerIds = new ArrayList<>();

/**
* List of {@link UploadableFile} factories.
*/
private final List<Function<Integer, UploadableFile>> uploadableFileFactories = new ArrayList<>();

/**
* Adds {@code peer_id} to the list.
*
* @param peerId message {@code peer_id}.
*/
public void addPeerId(Integer peerId) {
this.peerIds.add(peerId);
}

/**
* Adds list of {@code peer_id} to the list.
*
* @param peerIds list of message {@code peer_id}.
*/
public void addPeerIds(List<Integer> peerIds) {
this.peerIds.addAll(peerIds);
}

/**
* Adds {@link UploadableFile} factory to the list.
*
* @param uploadableFileFactory {@link UploadableFile} factory.
*/
public void addUploadableFileFactory(Function<Integer, UploadableFile> uploadableFileFactory) {
this.uploadableFileFactories.add(uploadableFileFactory);
}

/**
* Gets {@link Stream<UploadableFile>}.
*
* @param peerId message {@code peer_id}.
* @return {@link Stream<UploadableFile>}.
*/
private Stream<UploadableFile> getUploadableFiles(Integer peerId) {
return uploadableFileFactories.stream()
.map(uploadableFileFactory -> uploadableFileFactory.apply(peerId));
}

@Override
public List<UploadableFile> get() {
return peerIds.stream()
.flatMap(this::getUploadableFiles)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import api.longpoll.bots.model.objects.media.Doc;

import java.io.InputStream;
import java.util.function.Supplier;

/**
* Uploads a document to message.
Expand All @@ -24,15 +23,15 @@ public abstract class UploadableMessageDoc extends AbstractUploadableFile {
*/
private final Save save;

public UploadableMessageDoc(Supplier<Integer> peerIdSupplier, String accessToken) {
this.getMessagesUploadServer = new GetMessagesUploadServer(accessToken).setPeerId(peerIdSupplier.get()).setType("doc");
public UploadableMessageDoc(Integer peerId, String accessToken) {
this.getMessagesUploadServer = new GetMessagesUploadServer(accessToken).setPeerId(peerId).setType("doc");
this.save = new Save(accessToken);
}

@Override
public UploadedFile uploadFile(String filename, InputStream inputStream) throws VkApiException {
GetMessagesUploadServer.ResponseBody uploadServer = getMessagesUploadServer.execute();
UploadDoc.Response uploadedDoc = new UploadDoc(
UploadDoc.ResponseBody uploadedDoc = new UploadDoc(
uploadServer.getResponse().getUploadUrl(),
filename,
inputStream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import api.longpoll.bots.model.objects.additional.UploadedFile;

import java.io.InputStream;
import java.util.function.Supplier;

/**
* Uploads a photo to message.
Expand All @@ -23,15 +22,15 @@ public abstract class UploadableMessagePhoto extends AbstractUploadableFile {
*/
private final SaveMessagesPhoto saveMessagesPhoto;

public UploadableMessagePhoto(Supplier<Integer> peerIdSupplier, String accessToken) {
this.getMessagesUploadServer = new GetMessagesUploadServer(accessToken).setPeerId(peerIdSupplier.get());
public UploadableMessagePhoto(Integer peerId, String accessToken) {
this.getMessagesUploadServer = new GetMessagesUploadServer(accessToken).setPeerId(peerId);
this.saveMessagesPhoto = new SaveMessagesPhoto(accessToken);
}

@Override
public UploadedFile uploadFile(String filename, InputStream inputStream) throws VkApiException {
GetMessagesUploadServer.ResponseBody uploadServer = getMessagesUploadServer.execute();
UploadPhoto.Response uploadedPhoto = new UploadPhoto(
UploadPhoto.ResponseBody uploadedPhoto = new UploadPhoto(
uploadServer.getResponse().getUploadUrl(),
filename,
inputStream
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/api/longpoll/bots/methods/impl/VkMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
/**
* Executes generic HTTP request to VK API.
*
* @param <Response> VK API response type.
* @param <ResponseBody> VK API response type.
*/
public abstract class VkMethod<Response> {
public abstract class VkMethod<ResponseBody> {
/**
* Path to VK methods list.
*/
Expand Down Expand Up @@ -105,7 +105,7 @@ public VkMethod() {
*
* @return VK API response wrapped to CompletableFuture
*/
public CompletableFuture<Response> executeAsync() {
public CompletableFuture<ResponseBody> executeAsync() {
return asyncCaller.call(this::execute);
}

Expand All @@ -115,7 +115,7 @@ public CompletableFuture<Response> executeAsync() {
* @return VK API response.
* @throws VkApiException if errors occur.
*/
public Response execute() throws VkApiException {
public ResponseBody execute() throws VkApiException {
try {
HttpRequest request = new PostRequest.Builder(getUri())
.setRequestBody(getRequestBody())
Expand All @@ -141,7 +141,7 @@ public Response execute() throws VkApiException {
*
* @return a class of VK API response.
*/
protected abstract Class<Response> getResponseClass();
protected abstract Class<ResponseBody> getResponseClass();

/**
* Gets request URI.
Expand All @@ -166,7 +166,7 @@ protected RequestBody getRequestBody() {
* @param value URL parameter value.
* @return current instance.
*/
public VkMethod<Response> addParam(String key, Object value) {
public VkMethod<ResponseBody> addParam(String key, Object value) {
params.put(key, String.valueOf(value));
return this;
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/api/longpoll/bots/methods/impl/docs/Search.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @see <a href="https://vk.com/dev/docs.search">https://vk.com/dev/docs.search</a>
*/
public class Search extends VkMethod<Search.Response> {
public class Search extends VkMethod<Search.ResponseBody> {

public Search(String accessToken) {
super(accessToken);
Expand All @@ -21,8 +21,8 @@ public String getUri() {
}

@Override
protected Class<Response> getResponseClass() {
return Response.class;
protected Class<ResponseBody> getResponseClass() {
return ResponseBody.class;
}

public Search setQ(String q) {
Expand All @@ -49,6 +49,6 @@ public Search addParam(String key, Object value) {
/**
* Response to <b>docs.search</b> request.
*/
public static class Response extends VkList<Doc> {
public static class ResponseBody extends VkList<Doc> {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* Gets update events from VK server.
*/
public class GetUpdates extends VkMethod<GetUpdates.Response> {
public class GetUpdates extends VkMethod<GetUpdates.ResponseBody> {
/**
* Server URL.
*/
Expand All @@ -21,8 +21,8 @@ public GetUpdates() {
}

@Override
protected Class<Response> getResponseClass() {
return Response.class;
protected Class<ResponseBody> getResponseClass() {
return ResponseBody.class;
}

@Override
Expand Down Expand Up @@ -51,7 +51,7 @@ public GetUpdates addParam(String key, Object value) {
/**
* Contains list of events that occur on VK server.
*/
public static class Response {
public static class ResponseBody {
/**
* The number of the last event.
*/
Expand Down
Loading

0 comments on commit 60d6669

Please sign in to comment.