This repository has been archived by the owner on Jun 28, 2019. 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.
Merge pull request #10 from studyplus/feature/kotlin_coroutines
Kotlin Coroutinesの導入
- Loading branch information
Showing
12 changed files
with
79 additions
and
87 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#Sun Jul 01 16:03:41 JST 2018 | ||
#Tue Oct 30 15:28:34 JST 2018 | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip |
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
28 changes: 6 additions & 22 deletions
28
studyplus-android-sdk2/src/main/java/jp/studyplus/android/sdk/internal/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 |
---|---|---|
@@ -1,30 +1,14 @@ | ||
package jp.studyplus.android.sdk.internal.api | ||
|
||
import android.content.Context | ||
import io.reactivex.Observable | ||
import jp.studyplus.android.sdk.internal.api.response.PostStudyRecordsResponse | ||
import jp.studyplus.android.sdk.record.StudyRecord | ||
import retrofit2.Retrofit | ||
import kotlinx.coroutines.Deferred | ||
|
||
internal class ApiClient | ||
constructor(retrofit: Retrofit) { | ||
internal object ApiClient { | ||
private val apiService by lazy { ApiManager.retrofit.create(ApiService::class.java) } | ||
|
||
companion object { | ||
val apiClient by lazy { ApiClient(ApiManager.retrofit) } | ||
lateinit var apiService: ApiService | ||
|
||
private fun getOAuthAccessToken(context: Context): Observable<String> { | ||
return Observable.just(CertificationStore.create(context)) | ||
.map { it.apiCertification() } | ||
.map { "OAuth $it" } | ||
} | ||
} | ||
|
||
init { | ||
apiService = retrofit.create(ApiService::class.java) | ||
} | ||
|
||
fun postStudyRecords(context: Context, studyRecord: StudyRecord): Observable<PostStudyRecordsResponse> { | ||
return getOAuthAccessToken(context).flatMap { apiService.postStudyRecords(it, studyRecord) } | ||
fun postStudyRecords(context: Context, studyRecord: StudyRecord): Deferred<PostStudyRecordsResponse> { | ||
return apiService.postStudyRecords(CertificationStore.create(context).getOAuthAccessToken(), studyRecord) | ||
} | ||
} | ||
} |
10 changes: 5 additions & 5 deletions
10
studyplus-android-sdk2/src/main/java/jp/studyplus/android/sdk/internal/api/ApiManager.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 |
---|---|---|
@@ -1,28 +1,28 @@ | ||
package jp.studyplus.android.sdk.internal.api | ||
|
||
import com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory | ||
import jp.studyplus.android.sdk.BuildConfig | ||
import okhttp3.OkHttpClient | ||
import retrofit2.Retrofit | ||
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
import java.util.concurrent.TimeUnit | ||
|
||
internal object ApiManager { | ||
|
||
private val client by lazy { | ||
private val client: OkHttpClient by lazy { | ||
OkHttpClient.Builder() | ||
.connectTimeout(60, TimeUnit.SECONDS) | ||
.writeTimeout(60, TimeUnit.SECONDS) | ||
.readTimeout(60, TimeUnit.SECONDS) | ||
.build() | ||
} | ||
|
||
val retrofit by lazy { | ||
val retrofit: Retrofit by lazy { | ||
Retrofit.Builder() | ||
.client(client) | ||
.baseUrl(BuildConfig.API_ENDPOINT) | ||
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) | ||
.addCallAdapterFactory(CoroutineCallAdapterFactory()) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.build()!! | ||
.build() | ||
} | ||
} |
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
4 changes: 2 additions & 2 deletions
4
studyplus-android-sdk2/src/test/java/jp/studyplus/android/sdk/internal/api/MockApiService.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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
package jp.studyplus.android.sdk.internal.api | ||
|
||
import io.reactivex.Observable | ||
import jp.studyplus.android.sdk.internal.api.response.PostStudyRecordsResponse | ||
import jp.studyplus.android.sdk.record.StudyRecord | ||
import kotlinx.coroutines.Deferred | ||
import retrofit2.mock.BehaviorDelegate | ||
|
||
internal class MockApiService(private val delegate: BehaviorDelegate<ApiService>) : ApiService { | ||
override fun postStudyRecords(oauth: String, studyRecord: StudyRecord): Observable<PostStudyRecordsResponse> { | ||
override fun postStudyRecords(oauth: String, studyRecord: StudyRecord): Deferred<PostStudyRecordsResponse> { | ||
return delegate.returningResponse(PostStudyRecordsResponse(9999)).postStudyRecords(oauth, studyRecord) | ||
} | ||
} |