Skip to content

Commit

Permalink
Merge pull request #203 from superwall/custom-endpoint
Browse files Browse the repository at this point in the history
Adds custom endpoint to SuperwallOptions to facilitate easier local testing
  • Loading branch information
yusuftor committed Apr 5, 2024
2 parents e630a81 + aaff11b commit 869c7a5
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 41 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/tag-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,36 @@ jobs:
COCOAPODS_TRUNK_TOKEN: ${{ secrets.COCOAPODS_TRUNK_TOKEN }}
run: |
pod trunk push SuperwallKit.podspec --allow-warnings
slack:
runs-on: ubuntu-latest
needs: tag
steps:
- uses: actions/checkout@v3
- name: Parse version
id: version
run: |
VERSION=$(sed -n 21p ./Sources/SuperwallKit/Misc/Constants.swift)
echo "VERSION=$VERSION"
echo "::set-output name=prop::$VERSION"
- name: Determine prerelease status
id: prerelease
run: |
VERSION=${{steps.version.outputs.prop}}
if [[ "$VERSION" == *"-alpha"* || "$VERSION" == *"-beta"* || "$VERSION" == *"-rc"* ]]; then
echo "::set-output name=status::true"
else
echo "::set-output name=status::false"
fi
- name: slack-send
uses: slackapi/slack-github-action@v1.24.0
with:
payload: |
{
"text": "Please create a new iOS Release! https://github.com/superwall-me/Superwall-iOS/releases/new?tag=${{steps.version.outputs.prop}}&prerelease=${{steps.prerelease.outputs.status}}"
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK

3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ The changelog for `SuperwallKit`. Also see the [releases](https://github.com/sup

### Enhancements

- SW-2767: Adds `device.regionCode` and `device.preferredRegionCode`, which returns the `regionCode` of the locale. For example, if a locale is `en_GB`, the `regionCode` will be `GB`. You can use this in the filters of your campaign.
- SW-2767: Adds `device.regionCode` and `device.preferredRegionCode`, which returns the `regionCode` of the locale. For example, if a locale is `en_GB`,the `regionCode` will be `GB`. You can use this in the filters of your campaign.
- Adds ability to specify custom API endpoints using `SuperwallOptions` to facilitate local testing more easily.

### Fixes

Expand Down
10 changes: 8 additions & 2 deletions Sources/SuperwallKit/Analytics/SessionEventsQueue.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// File.swift
//
//
//
// Created by Yusuf Tör on 06/05/2022.
//
Expand Down Expand Up @@ -64,7 +64,13 @@ actor SessionEventsQueue: SessionEnqueuable {
}

private func setupTimer() {
let timeInterval = configManager.options.networkEnvironment == .release ? 20.0 : 1.0
let timeInterval: Double
switch configManager.options.networkEnvironment {
case .release:
timeInterval = 20.0
default:
timeInterval = 1.0
}
let timer = Timer(
timeInterval: timeInterval,
repeats: true
Expand Down
52 changes: 48 additions & 4 deletions Sources/SuperwallKit/Config/Options/SuperwallOptions.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// File.swift
//
//
//
// Created by Yusuf Tör on 11/07/2022.
//
Expand All @@ -20,13 +20,39 @@ public final class SuperwallOptions: NSObject {
/// **WARNING**: The different network environments that the SDK should use.
/// Only use this enum to set ``SuperwallOptions/networkEnvironment-swift.property``
/// if told so explicitly by the Superwall team.
public enum NetworkEnvironment: Int {
public enum NetworkEnvironment {
/// Default: Uses the standard latest environment.
case release
/// **WARNING**: Uses a release candidate environment. This is not meant for a production environment.
case releaseCandidate
/// **WARNING**: Uses the nightly build environment. This is not meant for a production environment.
case developer
/// **WARNING**: Uses a custom environment. This is not meant for a production environment.
case custom(String)

var scheme: String {
switch self {
case .custom(let domain):
if let url = URL(string: domain) {
return url.scheme ?? "https"
}
default:
return "https"
}
return "https"
}

var port: Int? {
switch self {
case .custom(let domain):
if let url = URL(string: domain) {
return url.port
}
default:
return nil
}
return nil
}

var hostDomain: String {
switch self {
Expand All @@ -36,15 +62,33 @@ public final class SuperwallOptions: NSObject {
return "superwallcanary.com"
case .developer:
return "superwall.dev"
case .custom(let domain):
if let url = URL(string: domain) {
if let host = url.host {
return host
}
}
return domain
}
}

var baseHost: String {
"api.\(hostDomain)"
switch self {
case .custom:

return hostDomain
default:
return "api.\(hostDomain)"
}
}

var collectorHost: String {
return "collector.\(hostDomain)"
switch self {
case .custom:
return hostDomain
default:
return "collector.\(hostDomain)"
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion Sources/SuperwallKit/Dependencies/DependencyContainer.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// File.swift
//
//
//
// Created by Yusuf Tör on 23/12/2022.
//
Expand Down Expand Up @@ -349,6 +349,10 @@ extension DependencyContainer: ApiFactory {

return headers
}

func makeDefaultComponents(host: EndpointHost) -> ApiHostConfig {
return self.api.getConfig(host: host)
}
}

// MARK: - Rule Params
Expand Down
6 changes: 6 additions & 0 deletions Sources/SuperwallKit/Dependencies/FactoryProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ protocol HasExternalPurchaseControllerFactory: AnyObject {
func makeHasExternalPurchaseController() -> Bool
}

struct DummyDecodable: Decodable {}

protocol ApiFactory: AnyObject {
// TODO: Think of an alternative way such that we don't need to do this:
// swiftlint:disable implicitly_unwrapped_optional
Expand All @@ -114,6 +116,10 @@ protocol ApiFactory: AnyObject {
isForDebugging: Bool,
requestId: String
) async -> [String: String]

func makeDefaultComponents(
host: EndpointHost
) -> ApiHostConfig
}

protocol StoreTransactionFactory: AnyObject {
Expand Down
44 changes: 38 additions & 6 deletions Sources/SuperwallKit/Network/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,71 @@

import Foundation

enum EndpointHost {
case base
case collector
}

protocol ApiHostConfig {
var port: Int? { get }
var scheme: String { get }
var host: String { get }
}

struct Api {
let hostDomain: String
let base: Base
let collector: Collector

static let version1 = "/api/v1/"
static let scheme = "https"

init(networkEnvironment: SuperwallOptions.NetworkEnvironment) {
self.base = Base(networkEnvironment: networkEnvironment)
self.collector = Collector(networkEnvironment: networkEnvironment)
self.hostDomain = networkEnvironment.hostDomain
}

struct Base {
func getConfig(host: EndpointHost) -> ApiHostConfig {
switch host {
case .base:
return base
case .collector:
return collector
}
}

struct Base: ApiHostConfig {
private let networkEnvironment: SuperwallOptions.NetworkEnvironment

init(networkEnvironment: SuperwallOptions.NetworkEnvironment) {
self.networkEnvironment = networkEnvironment
}

var port: Int? {
return networkEnvironment.port
}

var scheme: String {
return networkEnvironment.scheme
}

var host: String {
return networkEnvironment.baseHost
}
}

struct Collector {
struct Collector: ApiHostConfig {
private let networkEnvironment: SuperwallOptions.NetworkEnvironment

init(networkEnvironment: SuperwallOptions.NetworkEnvironment) {
self.networkEnvironment = networkEnvironment
}

var port: Int? {
return networkEnvironment.port
}

var scheme: String {
return networkEnvironment.scheme
}

var host: String {
return networkEnvironment.collectorHost
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ class DeviceHelper {
return build
}()


let interfaceType: String = {
#if compiler(>=5.9.2)
if #available(iOS 17.0, *) {
Expand Down Expand Up @@ -505,7 +504,7 @@ class DeviceHelper {
self.storage = storage
self.appInstalledAtString = appInstallDate?.isoString ?? ""
self.factory = factory
reachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, api.hostDomain)
reachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, api.base.host)
self.sdkVersionPadded = Self.makePaddedSdkVersion(using: sdkVersion)
}
}

0 comments on commit 869c7a5

Please sign in to comment.