This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
133 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
app/src/main/java/dk/scheduling/schedulingfrontend/API/ApiClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package dk.scheduling.schedulingfrontend.API | ||
|
||
import com.google.gson.GsonBuilder | ||
import okhttp3.OkHttpClient | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
import java.security.SecureRandom | ||
import java.security.cert.X509Certificate | ||
import javax.net.ssl.SSLContext | ||
import javax.net.ssl.TrustManager | ||
import javax.net.ssl.X509TrustManager | ||
|
||
object RetrofitClient { | ||
/** | ||
* The base URL of the API | ||
* 10.0.2.2 is a special alias to your host loopback interface (127.0.0.1 on your development machine) | ||
* 2222 is the port where the server is running | ||
*/ | ||
private const val BASE_URL = "http://10.0.2.2:2222/" | ||
private val gson = GsonBuilder().setLenient().create() | ||
private val okHttpClient: OkHttpClient by lazy { | ||
val trustAllCertificates = arrayOf<TrustManager>(object : X509TrustManager { | ||
override fun checkClientTrusted(chain: Array<out X509Certificate>?, authType: String?) {} | ||
override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?) {} | ||
override fun getAcceptedIssuers(): Array<X509Certificate> = arrayOf() | ||
}) | ||
|
||
val sslContext = SSLContext.getInstance("SSL") | ||
sslContext.init(null, trustAllCertificates, SecureRandom()) | ||
|
||
val sslSocketFactory = sslContext.socketFactory | ||
|
||
OkHttpClient.Builder() | ||
.sslSocketFactory(sslSocketFactory, trustAllCertificates[0] as X509TrustManager) | ||
.hostnameVerifier { _, _ -> true } // Allow all hostnames | ||
.build() | ||
} | ||
val retrofit: Retrofit by lazy { | ||
Retrofit.Builder() | ||
.baseUrl(BASE_URL) | ||
.client(okHttpClient) | ||
.addConverterFactory(GsonConverterFactory.create(gson)) | ||
.build() | ||
} | ||
} | ||
|
||
|
||
|
||
object ApiClient { | ||
val apiService: ApiService by lazy { | ||
RetrofitClient.retrofit.create(ApiService::class.java) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/dk/scheduling/schedulingfrontend/API/ApiService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package dk.scheduling.schedulingfrontend.API | ||
import retrofit2.Call | ||
import retrofit2.http.GET | ||
|
||
interface ApiService { | ||
@GET("test") | ||
fun test() : Call<String> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters