Skip to content

Commit

Permalink
feat(live-update): add configuration option httpTimeout (#277)
Browse files Browse the repository at this point in the history
* feat(live-update): add configuration option `httpTimeout`

* style: format

* fixes

* style: format
  • Loading branch information
robingenz authored Aug 30, 2024
1 parent f4df3ff commit 4e4fcc0
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-mirrors-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@capawesome/capacitor-live-update': minor
---

feat: add configuration option `httpTimeout`
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import net.lingala.zip4j.ZipFile;
import okhttp3.Call;
import okhttp3.Callback;
Expand Down Expand Up @@ -425,7 +426,12 @@ else if (expectedChecksum != null) {
}

private void downloadFile(@NonNull String url, @NonNull NonEmptyCallback<File> callback) {
OkHttpClient okHttpClient = new OkHttpClient();
int httpTimeout = config.getHttpTimeout();
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(httpTimeout, TimeUnit.MILLISECONDS)
.readTimeout(httpTimeout, TimeUnit.MILLISECONDS)
.writeTimeout(httpTimeout, TimeUnit.MILLISECONDS)
.build();
Request request = new Request.Builder().url(url).build();
okHttpClient
.newCall(request)
Expand Down Expand Up @@ -495,7 +501,13 @@ private void fetchLatestBundle(@NonNull NonEmptyCallback<GetLatestBundleResponse
.addQueryParameter("pluginVersion", LiveUpdatePlugin.VERSION)
.build();
Logger.debug(LiveUpdatePlugin.TAG, "Fetching latest bundle from " + url);
OkHttpClient client = new OkHttpClient();
int httpTimeout = config.getHttpTimeout();
OkHttpClient client = new OkHttpClient()
.newBuilder()
.connectTimeout(httpTimeout, TimeUnit.MILLISECONDS)
.readTimeout(httpTimeout, TimeUnit.MILLISECONDS)
.writeTimeout(httpTimeout, TimeUnit.MILLISECONDS)
.build();
okhttp3.Request request = new okhttp3.Request.Builder().url(url).build();
client
.newCall(request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class LiveUpdateConfig {
private String defaultChannel = null;

private boolean enabled = true;
private int httpTimeout = 60000;

@Nullable
private String location = null;
Expand Down Expand Up @@ -42,6 +43,10 @@ public boolean getEnabled() {
return enabled;
}

public int getHttpTimeout() {
return httpTimeout;
}

@Nullable
public String getLocation() {
return location;
Expand Down Expand Up @@ -76,6 +81,10 @@ public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public void setHttpTimeout(int httpTimeout) {
this.httpTimeout = httpTimeout;
}

public void setLocation(@Nullable String location) {
this.location = location;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class LiveUpdatePlugin extends Plugin {

public static final String TAG = "LiveUpdate";
public static final String VERSION = "6.3.0";
public static final String VERSION = "6.4.0";
public static final String SHARED_PREFERENCES_NAME = "CapawesomeLiveUpdate"; // DO NOT CHANGE
public static final String ERROR_APP_ID_MISSING = "appId must be configured.";
public static final String ERROR_BUNDLE_EXISTS = "bundle already exists.";
Expand Down Expand Up @@ -448,6 +448,8 @@ private LiveUpdateConfig getLiveUpdateConfig() {
config.setDefaultChannel(defaultChannel);
boolean enabled = getConfig().getBoolean("enabled", config.getEnabled());
config.setEnabled(enabled);
int httpTimeout = getConfig().getInt("httpTimeout", config.getHttpTimeout());
config.setHttpTimeout(httpTimeout);
String location = getConfig().getString("location", config.getLocation());
config.setLocation(location);
String publicKey = getConfig().getString("publicKey", config.getPublicKey());
Expand Down
14 changes: 10 additions & 4 deletions packages/live-update/ios/Plugin/LiveUpdate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,10 @@ import CommonCrypto
let temporaryZipFileUrl = self.cachesDirectoryUrl.appendingPathComponent(timestamp + ".zip")
return (temporaryZipFileUrl, [.createIntermediateDirectories])
}

AF.download(url, to: destination).validate().response { response in
var request = URLRequest(url: URL(string: url)!)
request.httpMethod = HTTPMethod.get.rawValue
request.timeoutInterval = Double(config.httpTimeout) / 1000.0
AF.download(request, to: destination).validate().response { response in
if let error = response.error {
CAPLog.print("[", LiveUpdatePlugin.tag, "] ", error)
completion(nil, CustomError.downloadFailed)
Expand All @@ -384,8 +386,12 @@ import CommonCrypto
host = "api.cloud.capawesome.eu"
}
}
let url = URL(string: "https://\(host)/v1/apps/\(config.appId ?? "")/bundles/latest")!
AF.request(url, method: .get, parameters: parameters).validate().responseDecodable(of: GetLatestBundleResponse.self) { response in
var urlComponents = URLComponents(string: "https://\(host)/v1/apps/\(config.appId ?? "")/bundles/latest")!
urlComponents.queryItems = parameters.map { URLQueryItem(name: $0.key, value: $0.value) }
var request = URLRequest(url: urlComponents.url!)
request.httpMethod = HTTPMethod.get.rawValue
request.timeoutInterval = Double(config.httpTimeout) / 1000.0
AF.request(request).validate().responseDecodable(of: GetLatestBundleResponse.self) { response in
CAPLog.print("[", LiveUpdatePlugin.tag, "] Fetching latest bundle from ", response.request?.url?.absoluteString ?? "")
if let error = response.error {
CAPLog.print("[", LiveUpdatePlugin.tag, "] ", error)
Expand Down
1 change: 1 addition & 0 deletions packages/live-update/ios/Plugin/LiveUpdateConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ public struct LiveUpdateConfig {
var autoDeleteBundles = false
var defaultChannel: String?
var enabled = true
var httpTimeout = 60000
var location: String?
var publicKey: String?
var readyTimeout = 10000
Expand Down
7 changes: 4 additions & 3 deletions packages/live-update/ios/Plugin/LiveUpdatePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Capacitor
@objc(LiveUpdatePlugin)
public class LiveUpdatePlugin: CAPPlugin {
public static let tag = "LiveUpdate"
public static let version = "6.3.0"
public static let version = "6.4.0"
public static let userDefaultsPrefix = "CapawesomeLiveUpdate" // DO NOT CHANGE

private var config: LiveUpdateConfig?
Expand Down Expand Up @@ -223,13 +223,13 @@ public class LiveUpdatePlugin: CAPPlugin {
call.reject(CustomError.appIdMissing.localizedDescription)
return
}

guard !syncInProgress else {
call.reject(CustomError.syncInProgress.localizedDescription)
return
}
syncInProgress = true

implementation?.sync(completion: { result, error in
self.syncInProgress = false
if let error = error {
Expand All @@ -250,6 +250,7 @@ public class LiveUpdatePlugin: CAPPlugin {
config.autoDeleteBundles = getConfig().getBoolean("autoDeleteBundles", config.autoDeleteBundles)
config.defaultChannel = getConfig().getString("defaultChannel", config.defaultChannel)
config.enabled = getConfig().getBoolean("enabled", config.enabled)
config.httpTimeout = getConfig().getInt("httpTimeout", config.httpTimeout)
config.location = getConfig().getString("location", config.location)
config.publicKey = getConfig().getString("publicKey", config.publicKey)
config.readyTimeout = getConfig().getInt("readyTimeout", config.readyTimeout)
Expand Down
7 changes: 7 additions & 0 deletions packages/live-update/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ declare module '@capacitor/cli' {
* @default true
*/
enabled?: boolean;
/**
* The timeout in milliseconds for HTTP requests.
*
* @since 6.4.0
* @default 60000
*/
httpTimeout?: number;
/**
* The location of the server to use when using [Capawesome Cloud](https://capawesome.io/cloud).
*
Expand Down

0 comments on commit 4e4fcc0

Please sign in to comment.