Commit 81ccefd2 authored by Ivan Del Pino's avatar Ivan Del Pino Committed by Alexander Salas Bastidas
Browse files

feat(multiple): handle multiple request improve


Signed-off-by: default avatarIvan Del Pino <idelpino@teclib.com>
parent fdebcb0f
......@@ -646,13 +646,13 @@ public class MainActivity extends AppCompatActivity {
}
});
String[] urls = {url, url, url, url, url};
glpi.handleMultipleDownload(urls, 3, new GLPI.ResponseHandle<ResponseBody[], String>() {
glpi.handleMultipleDownload(urls, new GLPI.ResponseHandle<ArrayList<ResponseBody>, String>() {
@Override
public void onResponse(ResponseBody[] response) {
public void onResponse(ArrayList<ResponseBody> response) {
Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
for (int i = 0; i < response.length; i++) {
for (int i = 0; i < response.size(); i++) {
String pathname = getExternalFilesDir(null) + File.separator + i + "-test.md";
boolean inDisk = Helpers.writeResponseBodyToDisk(response[i], pathname);
boolean inDisk = Helpers.writeResponseBodyToDisk(response.get(i), pathname);
}
}
......
......@@ -45,14 +45,14 @@ import org.glpi.api.response.InitSession;
import org.glpi.api.utils.Helpers;
import org.json.JSONObject;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;
......@@ -556,40 +556,41 @@ public class GLPI extends ServiceGenerator {
});
}
public void handleMultipleDownload(String[] urls, int limit, final ResponseHandle<ResponseBody[], String> callback) {
public void handleMultipleDownload(String[] urls, final ResponseHandle<ArrayList<ResponseBody>, String> callback) {
HashMap<String, String> header = new HashMap<>();
header.put("Accept","application/octet-stream");
header.put("Content-Type","application/json");
header.put("Session-Token", sessionToken);
String[] sendUrls = Arrays.copyOfRange(urls, 0, limit);
for (int i = 0; i < limit; i++) {
Observable<ResponseBody> observableOne = interfaces.downloadFileRX(sendUrls[0], header)
ArrayList<Observable<ResponseBody>> list = new ArrayList<>();
for (String url : urls) {
Observable<ResponseBody> observableOne = interfaces.downloadFileRX(url, header)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
Observable<ResponseBody> observableTwo = interfaces.downloadFileRX(sendUrls[1], header)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
final Disposable subscribe = Observable.zip(observableOne, observableTwo, new BiFunction<ResponseBody, ResponseBody, ManagementFile>() {
@Override
public ManagementFile apply(ResponseBody responseBody, ResponseBody responseBody2) throws Exception {
return new ManagementFile(new ResponseBody[]{responseBody, responseBody2});
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
new Consumer<ManagementFile>() {
@Override
public void accept(ManagementFile o) throws Exception {
callback.onResponse(o.getFile());
}
},
new Consumer<Throwable>() {
@Override
public void accept(Throwable e) throws Exception {
callback.onFailure(e.getMessage());
}
});
list.add(observableOne);
}
final ArrayList<ResponseBody> listResponseBody = new ArrayList<>();
Disposable subscribe1 = Observable.merge(list)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnComplete(new Action() {
@Override
public void run() throws Exception {
Log.e("", "");
callback.onResponse(listResponseBody);
}
})
.subscribe(new Consumer<ResponseBody>() {
@Override
public void accept(ResponseBody responseBody) throws Exception {
listResponseBody.add(responseBody);
}
},
new Consumer<Throwable>() {
@Override
public void accept(Throwable e) throws Exception {
callback.onFailure(e.getMessage());
}
});
}
public class ManagementFile {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment