main.phraseapp.network.PhraseAppNetworkDataSource.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-client Show documentation
Show all versions of gradle-client Show documentation
JVM client to interact with Phrase via its API.
The newest version!
package phraseapp.network
import com.google.gson.FieldNamingPolicy
import com.google.gson.GsonBuilder
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
const val DEFAULT_REGEX = ".+_([a-z]{2}-[A-Z]{2})"
const val DEFAULT_PLACEHOLDER = false
val DEFAULT_EXCEPTIONS: Map = emptyMap()
const val DEFAULT_OVERRIDE_DEFAULT_FILE = false
val DEFAULT_ALLOWED_LOCALE_CODES: List = emptyList()
const val DEFAULT_DONT_DELETE_KEYS = false
const val PHRASEAPP_BASEURL = "https://api.phrase.com/api/"
data class LocaleContent(val content: String, val isDefault: Boolean)
interface PhraseAppNetworkDataSource {
suspend fun downloadAllLocales(
overrideDefaultFile: Boolean = DEFAULT_OVERRIDE_DEFAULT_FILE,
exceptions: Map = DEFAULT_EXCEPTIONS,
placeHolder: Boolean = DEFAULT_PLACEHOLDER,
localeNameRegex: String = DEFAULT_REGEX,
allowedLocaleCodes: List = DEFAULT_ALLOWED_LOCALE_CODES
): Map
suspend fun upload(localeId: String, filePath: String)
companion object {
fun newInstance(baseUrl: String, token: String, projectId: String, fileFormat: String)
: PhraseAppNetworkDataSource {
val gson = GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create()
val client = OkHttpClient.Builder()
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.hostnameVerifier { _, _ -> true }
.build()
val retrofit = Retrofit.Builder()
.baseUrl(baseUrl)
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
return PhraseAppNetworkDataSourceImpl(
token, projectId, fileFormat, retrofit.create(PhraseAppService::class.java)
)
}
}
}