Skip to content

ikmuwn/Retrofit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Retrofit

Use

  • Retrofit 구조체 정의

    class GithubRetrofit @Inject constructor() : Retrofit() {
    
        // 호스트 서버 지정
        override val domain = DOMAIN
    
        // tls 활성
        override val enableTls = true
    
        // 공통 헤더값 지정
        override val headers: ArrayMap<String, String>
            get() = Companion.headers
    
        override fun proceed(chain: Interceptor.Chain, request: Request): Response {
            val response = super.proceed(chain, request)
            if (!response.isSuccessful) {
                // 자체 에러 전문으로 커스텀 오류 throw 가능
                throw errorBodyAs(GithubServerException::class.java, response.body)
                    ?: IOException(response.message)
            }
    
            return response
        }
    
        companion object {
    
            val DOMAIN: String
                get() = "https://api.github.com"
    
            val headers: ArrayMap<String, String>
                get() = ArrayMap<String, String>().apply {
                    put("x-app-ver", BuildConfig.VERSION_NAME)
                    put("x-os-ver", Build.VERSION.RELEASE)
                    put("x-device-model", Build.MODEL)
                }
        }
    
    }
  • 서비스 클래스 생성

    private val service by lazy { githubRetrofit.createService(GithubService::class.java) }

About

Retrofit abstract class

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages