Skip to content

Commit

Permalink
Add http client module to use for prototyping
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Sep 22, 2023
1 parent b0ea3c0 commit 71bd7e5
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 1 deletion.
34 changes: 34 additions & 0 deletions android/lib/http-client/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
plugins {
id(Dependencies.Plugin.androidLibraryId)
id(Dependencies.Plugin.kotlinAndroidId)
}

android {
namespace = "net.mullvad.mullvadvpn.lib.http"
compileSdk = Versions.Android.compileSdkVersion

defaultConfig {
minSdk = Versions.Android.minSdkVersion
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

kotlinOptions {
jvmTarget = Versions.jvmTarget
}

lint {
lintConfig = file("${rootProject.projectDir}/config/lint.xml")
abortOnError = true
warningsAsErrors = true
}
}

dependencies {
implementation(Dependencies.Kotlin.stdlib)
implementation(Dependencies.KotlinX.coroutinesAndroid)
implementation(Dependencies.androidVolley)
}
4 changes: 4 additions & 0 deletions android/lib/http-client/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package net.mullvad.mullvadvpn.lib.http

import android.content.Context
import android.util.Log
import com.android.volley.Request
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.RequestFuture
import com.android.volley.toolbox.Volley
import org.json.JSONObject

/**
* This class should primarily be used to make calls to the api during the early stages of
* implementing a new endpoint. These calls should then be migrated to the daemon and this class
* should not be used outside of this narrow scope.
*/
class MullvadHttpClient(context: Context) {
private val queue = Volley.newRequestQueue(context)

fun newPurchase(accountToken: String): String? {
val authToken = login(accountToken)
val response =
sendSimpleSynchronousRequest(
method = Request.Method.POST,
url = NEW_PURCHASE_URL,
token = authToken
)
return response?.getString("obfuscated_external_account_id")
}

fun acknowledgePurchase(
accountToken: String,
productId: String,
purchaseToken: String
): Boolean {
val authToken = login(accountToken)
val response =
sendSimpleSynchronousRequest(
method = Request.Method.POST,
url = ACKNOWLEDGE_PURCHASE_URL,
token = authToken,
body =
JSONObject().apply {
put("product_id", productId)
put("token", purchaseToken)
}
)
return response != null
}

private fun login(accountToken: String): String {
val json = JSONObject().apply { put("account_number", accountToken) }
return sendSimpleSynchronousRequest(Request.Method.POST, AUTH_URL, json)
?.getString("access_token")
?: ""
}

private fun sendSimpleSynchronousRequest(
method: Int,
url: String,
body: JSONObject? = null,
token: String? = null
): JSONObject? {
val future = RequestFuture.newFuture<JSONObject>()
val request =
object : JsonObjectRequest(method, url, body, future, future) {
override fun getHeaders(): MutableMap<String, String> {
val headers = HashMap<String, String>()
if (body != null) {
headers.put("Content-Type", "application/json")
}
if (token != null) {
headers.put("Authorization", "Bearer $token")
}
return headers
}
}
queue.add(request)
return try {
future.get()
} catch (e: Exception) {
Log.e("Error", "Could not login", e)
null
}
}

companion object {
private const val API_BASE_URL = "https://api.mullvad.net"
private const val API_VERSION = "v1"
private const val AUTH_URL = "$API_BASE_URL/auth/$API_VERSION/token"
private const val NEW_PURCHASE_URL = "$API_BASE_URL/payments/google-play/new"
private const val ACKNOWLEDGE_PURCHASE_URL =
"$API_BASE_URL/payments/google-play/acknowledge"
}
}
3 changes: 2 additions & 1 deletion android/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ include(
":lib:resource",
":lib:talpid",
":lib:theme",
":lib:common-test"
":lib:common-test",
":lib:http-client"
)

include(":test", ":test:common", ":test:e2e", ":test:mockapi")

0 comments on commit 71bd7e5

Please sign in to comment.