Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
[![Release](https://jitpack.io/v/stanwood/framework-network-android.svg?style=flat-square)](https://jitpack.io/#stanwood/framework-network-android)
[![Build Status](https://app.bitrise.io/app/983e6342cc5e0e24/status.svg?token=QtXUf2lbVhJrANROaTkluQ&branch=develop)](https://app.bitrise.io/app/983e6342cc5e0e24)
[![Release](https://jitpack.io/v/whisp-internet/framework-network-android.svg?style=flat-square)](https://jitpack.io/#whisp-internet/framework-network-android)
[![API](https://img.shields.io/badge/API-16%2B-blue.svg?style=flat)](https://android-arsenal.com/api?level=16)

# stanwood Network Utilities (Android)
# whisp Internet Network Utilities (Android)

A set of hopefully useful classes for common networking use cases.

## Import

The stanwood Network Utilities are hosted on JitPack. Therefore you can simply
import them by adding
The whisp Internet Network Utilities (based on the [stanwood Network Utilities](https://github.com/stanwood/framework-network-android/)) are hosted on JitPack. Therefore you can simply import them by adding

```groovy
allprojects {
Expand All @@ -26,9 +24,9 @@ Then add this to you app's `build.gradle`:

```groovy
dependencies {
implementation 'com.github.stanwood.framework-network-android:arch:<insert latest version here>' // aar version available as well
implementation 'com.github.stanwood.framework-network-android:cache:<insert latest version here>' // aar version available as well
implementation 'com.github.stanwood.framework-network-android:core:<insert latest version here>' // aar version available as well, automatically included when pulling in one of the other libraries
implementation 'com.github.whisp-internet.framework-network-android:auth:<insert latest version here>' // aar version available as well
implementation 'com.github.whisp-internet.framework-network-android:cache:<insert latest version here>' // aar version available as well
implementation 'com.github.whisp-internet.framework-network-android:core:<insert latest version here>' // aar version available as well, automatically included when pulling in one of the other libraries
}
```

Expand Down Expand Up @@ -258,4 +256,4 @@ Make sure to check the _Run Git hooks_ checkbox if you commit via Android Studio

Last thing to do is setting line length to 140 (Preferences -> Editor -> Code Style) - unfortunately `ktlintApplyToIdea` can't do that for you.

The CI will check formatting as well and won't allow non style conform commits to be merged.
The CI will check formatting as well and won't allow non style conform commits to be merged.
18 changes: 9 additions & 9 deletions auth/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 stanwood GmbH
* Copyright (c) 2019 stanwood GmbH, 2020 whisp Internet GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,15 +24,15 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 28
compileSdkVersion 29

defaultConfig {
minSdkVersion 16
targetSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'

}

Expand All @@ -52,17 +52,17 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
api project(":core")

implementation 'com.android.support:support-annotations:28.0.0'
testImplementation 'junit:junit:4.12'
implementation 'androidx.annotation:annotation:1.1.0'
testImplementation 'junit:junit:4.13.1'

compileOnly 'com.squareup.retrofit2:retrofit:2.5.0'
compileOnly 'com.squareup.retrofit2:retrofit:2.9.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

def retrofit_version = '2.5.0'
def retrofit_version = '2.9.0'
testImplementation "com.squareup.retrofit2:retrofit:$retrofit_version"
testImplementation "com.squareup.retrofit2:converter-gson:$retrofit_version"

testImplementation 'com.google.code.gson:gson:2.8.5'
testImplementation 'com.google.code.gson:gson:2.8.6'

testImplementation "com.squareup.okhttp3:logging-interceptor:$okhttp_version"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ open class Authenticator(
private val tokenReaderWriter: TokenReaderWriter,
private val onAuthenticationFailedListener: OnAuthenticationFailedListener?
) : okhttp3.Authenticator {
override fun authenticate(route: Route?, response: Response): Request? = response.request().let { request ->
override fun authenticate(route: Route?, response: Response): Request? = response.request.let { request ->
tokenReaderWriter.read(request)?.let { oldToken ->
synchronized(authenticationProvider.lock) {
if (request.header(AuthHeaderKeys.RETRY_WITH_REFRESH_HEADER_KEY) != null) {
Expand Down Expand Up @@ -98,7 +98,7 @@ open class Authenticator(
all tokens
*/
response.newBuilder().request(
response.request().newBuilder()
response.request.newBuilder()
.header(AuthHeaderKeys.RETRY_WITH_REFRESH_HEADER_KEY, "true")
.build()
).build()
Expand Down
10 changes: 5 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018 stanwood GmbH
* Copyright (c) 2018 stanwood GmbH, 2020 whisp Internet GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -23,8 +23,8 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.31'
ext.okhttp_version = '3.13.1'
ext.kotlin_version = '1.4.20'
ext.okhttp_version = '4.9.0'
repositories {
google()
jcenter()
Expand All @@ -33,9 +33,9 @@ buildscript {
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0-rc03'
classpath 'com.android.tools.build:gradle:4.2.0-alpha15'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jlleitschuh.gradle:ktlint-gradle:7.3.0"
classpath "org.jlleitschuh.gradle:ktlint-gradle:9.3.0"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
10 changes: 5 additions & 5 deletions cache/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 stanwood GmbH
* Copyright (c) 2019 stanwood GmbH, 2020 whisp Internet GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,15 +24,15 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 28
compileSdkVersion 29

defaultConfig {
minSdkVersion 16
targetSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'

}

Expand All @@ -52,6 +52,6 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
api project(":core")

implementation 'com.android.support:support-annotations:28.0.0'
implementation 'androidx.annotation:annotation:1.1.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
package io.stanwood.framework.network.cache;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

Expand Down Expand Up @@ -78,11 +80,12 @@ public CacheInterceptor(
this.errorCallback = errorCallback;
}

@NotNull
@Override
public Response intercept(@NonNull Chain chain) throws IOException {
Request request = chain.request();
String offlineCacheHeader = request.header(CacheHeaderKeys.APPLY_OFFLINE_CACHE);
if (Boolean.valueOf(offlineCacheHeader) && !connectionState.isConnected()) {
if (Boolean.parseBoolean(offlineCacheHeader) && !connectionState.isConnected()) {
Request.Builder builder = request.newBuilder();
if (queryAuthParameterKey != null) {
builder.url(request
Expand All @@ -100,7 +103,7 @@ public Response intercept(@NonNull Chain chain) throws IOException {
}

String responseCacheHeader = request.header(CacheHeaderKeys.REFRESH);
if (Boolean.valueOf(responseCacheHeader)) {
if (Boolean.parseBoolean(responseCacheHeader)) {
try {
return chain.proceed(request.newBuilder().cacheControl(CacheControl.FORCE_NETWORK).build());
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@

package io.stanwood.framework.network.cache;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

Expand Down Expand Up @@ -53,13 +55,22 @@
* this library. In the future this might be added to the provided interceptors by means of
* accepting a list of parameter keys instead of just the one of the auth parameter.
*
* <p>Configuration of the interceptor classes is mainly done by means of request headers. Check
* out {@link CacheHeaderKeys} for more details on what the different headers do.
* <p>Configuration of the interceptor classes is mainly done via request headers. Check
* out {@link CacheHeaderKeys} for more details on what the different headers do. Add them to your
* requests like so:
* <pre>
* {@code @Headers({ CacheHeaderKeys.APPLY_RESPONSE_CACHE + ": true" })}
* </pre>
*
* <p>Add it as a network interceptor to your OkHttpClient Builder like so:
* <p>Add instances of this interceptor as a network interceptor to your OkHttpClient Builder like so:
* <pre>
* {@code okHttpClientBuilder.addNetworkInterceptor(new CacheNetworkInterceptor("auth", 3600))}
* </pre>
*
* Don't forget to add a cache to the builder as well:
* <pre>
* {@code okHttpClientBuilder.cache(new Cache(new File(app.cacheDir, "okhttp-storyly-rest-api-cache"), 10 * 1024))}
* </pre>
*/
public class CacheNetworkInterceptor implements Interceptor {

Expand All @@ -80,13 +91,14 @@ public CacheNetworkInterceptor(@Nullable String queryAuthParameterKey, long cach
this.cacheForSeconds = cacheForSeconds;
}

@NotNull
@Override
public Response intercept(@NonNull Chain chain) throws IOException {
Request request = chain.request();
String responseCacheHeader = request.header(CacheHeaderKeys.APPLY_RESPONSE_CACHE);
String offlineCacheHeader = request.header(CacheHeaderKeys.APPLY_OFFLINE_CACHE);
boolean isGeneralCache = Boolean.valueOf(responseCacheHeader);
boolean isOfflineCache = Boolean.valueOf(offlineCacheHeader);
boolean isGeneralCache = Boolean.parseBoolean(responseCacheHeader);
boolean isOfflineCache = Boolean.parseBoolean(offlineCacheHeader);
if (isGeneralCache || isOfflineCache) {
Response originalResponse = chain.proceed(request);
Response.Builder builder = originalResponse.newBuilder();
Expand Down
16 changes: 8 additions & 8 deletions core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 stanwood GmbH
* Copyright (c) 2019 stanwood GmbH, 2020 whisp Internet GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,15 +24,15 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 28
compileSdkVersion 29

defaultConfig {
minSdkVersion 16
targetSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'

}

Expand All @@ -51,13 +51,13 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'com.android.support:support-annotations:28.0.0'
testImplementation 'junit:junit:4.12'
implementation 'androidx.annotation:annotation:1.1.0'
testImplementation 'junit:junit:4.13.1'

api "com.squareup.okhttp3:okhttp:$okhttp_version"
compileOnly 'com.squareup.retrofit2:retrofit:2.5.0'
compileOnly 'com.squareup.retrofit2:retrofit:2.9.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
api "org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.11.0"
api "org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.1"
api 'com.nytimes.android:store3:3.1.1'
api 'com.nytimes.android:filesystem3:3.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.3' // same version as defined within store
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
package io.stanwood.framework.network.core.store

import com.nytimes.android.external.store3.base.Parser
import kotlinx.io.Reader
import kotlinx.serialization.KSerializer
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonConfiguration
import okio.BufferedSource
import java.io.Reader

object SerializationParserFactory {
private val stable = Json(JsonConfiguration.Stable)
private val stable = Json {
allowStructuredMapKeys = true
}
fun <T> createReaderParser(
deserializer: KSerializer<T>,
json: Json = stable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ package io.stanwood.framework.network.core.store

import com.nytimes.android.external.store3.base.Parser
import com.nytimes.android.external.store3.util.ParserException
import kotlinx.io.Reader
import kotlinx.serialization.KSerializer
import kotlinx.serialization.json.Json
import java.io.Reader

class SerializationReaderParser<Parsed>(
private val deserializer: KSerializer<Parsed>,
val json: Json
private val json: Json
) : Parser<Reader, Parsed> {
@Throws(ParserException::class)
override fun apply(reader: Reader): Parsed =
json.parse(deserializer, reader.use { it.readText() })
json.decodeFromString(deserializer, reader.use { it.readText() })
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import kotlinx.serialization.json.Json
import okio.BufferedSource

class SerializationSourceParser<Parsed>(
private val deserializer: KSerializer<Parsed>,
val json: Json
private val deserializer: KSerializer<Parsed>,
private val json: Json
) : Parser<BufferedSource, Parsed> {
@Throws(ParserException::class)
override fun apply(bufferedSource: BufferedSource): Parsed =
json.parse(deserializer, bufferedSource.use { it.readUtf8() })
json.decodeFromString(deserializer, bufferedSource.use { it.readUtf8() })
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ class SerializationStringParser<Parsed>(
) : Parser<String, Parsed> {
@Throws(ParserException::class)
override fun apply(string: String): Parsed =
json.parse(deserializer, string)
json.decodeFromString(deserializer, string)
}
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
android.enableJetifier=true
android.useAndroidX=true
org.gradle.jvmargs=-Xmx1536m

# When configured, Gradle will run in incubating parallel mode.
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
distributionUrl=https://services.gradle.org/distributions/gradle-6.7-bin.zip
Loading