Last active
February 27, 2020 10:27
-
-
Save Frank1234/22891c3e4ba116006fcc9e5d521d4706 to your computer and use it in GitHub Desktop.
Provides Retrofit Headers
This file contains hidden or 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
class ApiMainHeadersProvider { | |
/** | |
* Public headers for calls that do not need an authenticated user. | |
*/ | |
fun getPublicHeaders(): PublicHeaders = | |
PublicHeaders().apply { | |
putAll(getDefaultHeaders()) | |
} | |
/** | |
* Returns both the default headers and the headers that are mandatory for authenticated users. | |
*/ | |
fun getAuthenticatedHeaders(accessToken: AccessToken): AuthenticatedHeaders = | |
AuthenticatedHeaders().apply { | |
putAll(getDefaultHeaders()) | |
put(AUTHORIZATION, getBearer(accessToken)) | |
} | |
/** | |
* Default headers used on all calls. | |
*/ | |
private fun getDefaultHeaders() = mapOf( | |
ACCEPT_LANGUAGE to LANGUAGE, | |
HEADER_ACCEPT to "application/json", | |
USER_AGENT to BuildConfig.config_api_user_agent | |
) | |
companion object { | |
private const val ACCEPT_LANGUAGE = "Accept-Language" | |
private const val USER_AGENT = "User-Agent" | |
private const val AUTHORIZATION = "Authorization" | |
private const val HEADER_ACCEPT = "Accept" | |
private fun getBearer(accessToken: AccessToken) = "Bearer ${accessToken.value}" | |
} | |
} | |
open class ApiMainHeaders : HashMap<String, String>() | |
class AuthenticatedHeaders : ApiMainHeaders() | |
class PublicHeaders : ApiMainHeaders() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment