-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: merge branch 'feature/okhttp3' into develop
- Loading branch information
Showing
55 changed files
with
2,797 additions
and
162 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
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
Empty file.
This file was deleted.
Oops, something went wrong.
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
Empty file.
This file was deleted.
Oops, something went wrong.
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
Empty file.
This file was deleted.
Oops, something went wrong.
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,2 @@ | ||
build | ||
.gradle |
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,43 @@ | ||
plugins { | ||
id("com.android.library") | ||
kotlin("android") | ||
id("maven-publish") | ||
} | ||
|
||
android { | ||
compileSdk = 33 | ||
buildToolsVersion = "33.0.0" | ||
compileOptions { | ||
sourceCompatibility = JavaVersion.VERSION_11 | ||
targetCompatibility = JavaVersion.VERSION_11 | ||
} | ||
kotlinOptions { | ||
apiVersion = "1.7" | ||
languageVersion = "1.7" | ||
jvmTarget = "11" | ||
} | ||
} | ||
|
||
dependencies { | ||
// define a BOM and its version | ||
implementation(platform("com.squareup.okhttp3:okhttp-bom:4.10.0")) | ||
|
||
// define any required OkHttp artifacts without version | ||
api("com.squareup.okhttp3:okhttp") | ||
api("com.squareup.okhttp3:logging-interceptor") | ||
testApi("com.squareup.okhttp3:mockwebserver") | ||
} | ||
|
||
afterEvaluate { | ||
publishing { | ||
publications { | ||
create<MavenPublication>("release") { | ||
groupId = "com.parsuomash.affogato" | ||
artifactId = "affogato-okhttp-android" | ||
version = "1.5.0" | ||
|
||
from(components["release"]) | ||
} | ||
} | ||
} | ||
} |
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,2 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest package="com.parsuomash.affogato.okhttp.android" /> |
111 changes: 111 additions & 0 deletions
111
affogato-okhttp-android/src/main/java/com/parsuomash/affogato/okhttp/android/OkHttp.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,111 @@ | ||
package com.parsuomash.affogato.okhttp.android | ||
|
||
import java.util.concurrent.ExecutorService | ||
import okhttp3.Cache | ||
import okhttp3.Call | ||
import okhttp3.ConnectionPool | ||
import okhttp3.OkHttpClient | ||
import okhttp3.Response | ||
|
||
/** | ||
* Factory for [calls][Call], which can be used to send HTTP requests and read their responses. | ||
* | ||
* ## OkHttpClients Should Be Shared | ||
* | ||
* OkHttp performs best when you create a single `OkHttpClient` instance and reuse it for all of | ||
* your HTTP calls. This is because each client holds its own connection pool and thread pools. | ||
* Reusing connections and threads reduces latency and saves memory. Conversely, creating a client | ||
* for each request wastes resources on idle pools. | ||
* | ||
* Use `OkHttpClient()` to create a shared instance with the default settings: | ||
* | ||
* ``` | ||
* // The singleton HTTP client. | ||
* val client = OkHttpClient() | ||
* ``` | ||
* | ||
* Or use `okHttp { }` to create a shared instance with custom settings: | ||
* | ||
* ``` | ||
* // The singleton HTTP client. | ||
* val client = okHttp { | ||
* addInterceptor(HttpLoggingInterceptor()) | ||
* cache(Cache(cacheDir, cacheSize)) | ||
* } | ||
* ``` | ||
* | ||
* ## Customize Your Client With newBuilder() | ||
* | ||
* You can customize a shared OkHttpClient instance with [new]. This builds a client that | ||
* shares the same connection pool, thread pools, and configuration. Use the builder methods to | ||
* configure the derived client for a specific purpose. | ||
* | ||
* This example shows a call with a short 500 millisecond timeout: | ||
* | ||
* ``` | ||
* val eagerClient = okHttp { | ||
* readTimeout(500, TimeUnit.MILLISECONDS) | ||
* } | ||
* val response = eagerClient.newCall(request).execute() | ||
* ``` | ||
* | ||
* ## Shutdown Isn't Necessary | ||
* | ||
* The threads and connections that are held will be released automatically if they remain idle. But | ||
* if you are writing an application that needs to aggressively release unused resources you may do | ||
* so. | ||
* | ||
* Shutdown the dispatcher's executor service with [shutdown()][ExecutorService.shutdown]. This will | ||
* also cause future calls to the client to be rejected. | ||
* | ||
* ``` | ||
* client.dispatcher().executorService().shutdown() | ||
* ``` | ||
* | ||
* Clear the connection pool with [evictAll()][ConnectionPool.evictAll]. Note that the connection | ||
* pool's daemon thread may not exit immediately. | ||
* | ||
* ``` | ||
* client.connectionPool().evictAll() | ||
* ``` | ||
* | ||
* If your client has a cache, call [close()][Cache.close]. Note that it is an error to create calls | ||
* against a cache that is closed, and doing so will cause the call to crash. | ||
* | ||
* ``` | ||
* client.cache().close() | ||
* ``` | ||
* | ||
* OkHttp also uses daemon threads for HTTP/2 connections. These will exit automatically if they | ||
* remain idle. | ||
* @since 1.5.0 | ||
* @see OkHttpClient | ||
*/ | ||
inline fun okHttp( | ||
init: OkHttpClient.Builder.() -> Unit | ||
): OkHttpClient { | ||
val builder: OkHttpClient.Builder = OkHttpClient.Builder().also { init(it) } | ||
return builder.build() | ||
} | ||
|
||
/** | ||
* @since 1.5.0 | ||
* @see OkHttpClient | ||
*/ | ||
inline fun OkHttpClient.new( | ||
init: OkHttpClient.Builder.() -> Unit | ||
): OkHttpClient { | ||
val builder: OkHttpClient.Builder = newBuilder().also { init(it) } | ||
return builder.build() | ||
} | ||
|
||
/** | ||
* @since 1.5.0 | ||
* @see Response | ||
*/ | ||
inline fun Response.new( | ||
init: Response.Builder.() -> Unit | ||
): Response { | ||
val builder: Response.Builder = newBuilder().also { init(it) } | ||
return builder.build() | ||
} |
Oops, something went wrong.