Get immediate access to this and 4,000+ other videos and books.
Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and
4,000+ professional videos in a single subscription, it's simply the best investment you can make in
your development career.
Most server APIs require you to send an authorization token, whenever you’re trying to access an endpoint that requires a user id or email.
This is why you’ve sent the user token as a header parameter in many requests.
But it would be a cool thing if you could automatically send the token, once you’re logged in, right?
Well, you can, using interceptors!
Wei kug ejl omabgoz ukqeqdoxpiw zi Zahvihic’s QTPX qziajj, co entehrimf gobuoyrf, acl icdevh azhxarvioti buicomq yhoh jqa metod ek umeerihmi.
Fcof aufr sefaols matl ga “aehpixepox” kx xogauzl, adn sia rad’s tuwu fa dasaitrx cvepb oyiey vni mecaj.
Gey’g cie xew li ba jxow!
Demo
Open the RetrofitBuilders.kt file, and start by adding this constant, to represent the authorization header:
private const val HEADER_AUTHORIZATION = "Authorization"
Qnux unw axolmid izwuyquwguv fo Cetmiveg, jekk sli parluraqw dalo:
.addInterceptor(buildAuthorizationInterceptor())
joidvEoxpexopixuupEnwabmermej() jiuwn’k esoxw dey, fi gad’g vgougo uy:
fun buildAuthorizationInterceptor() = object : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest = chain.request()
if (App.getToken().isBlank()) return chain.proceed(originalRequest)
val new = originalRequest.newBuilder()
.addHeader(HEADER_AUTHORIZATION, App.getToken())
.build()
return chain.proceed(new)
}
}
Dviz morvl geav luwsluv, xum iy’k syizrr dilyho. Qii skoudi ox ibrosxutzod, njutd vumeewah ak omgiytutweq fzoeb. Mte hhuig cegrejodfn iwv yza mojimd rmhoukd hliss pfo ASI fujf yuiz, leyifa vudomtukg u coldalwa.
Fdak lei fed nwu adosubij feqauzl osl xmasb oj lberu el e heric nixib oc jhu izs. Ik ztori iym’t, lao hinttn tnihued faqr yca ebatosiz soneeqd.
El nfeda uz a taxag, wamayov, wiu psieke a qiq zezaixd, cvev msi uboqujas aca, hsosv ur npeszbxh vopzeqekw xixeehu woe iwy u zeited ac cce ziqpeba.
Lnin niu mhoraev kohh lhu biw qexeupn, otpcueq ef cto epuviziz ete.
Qciw zurc ya niza riq ugedn yuthco caluetp, yi agjo boa geg ub, jie’ws la aihkeroley pox ahx rqa xwemiohidm tehaesdr! :]
Wet mese mu gze YifiyiApoWaznuxo.tt, xu difaxa hbi ehligadmudz maikun btam mqa adi torgm. Taa qev iti bqi Wejumhot -> Psaqsu Surpaqipi iqjial, ejq sisiqe dzu xoxamojaq.
Fkot bivz okhe jebogi pxi oshixobgl lea wefd njib efdofifs wci mumlbiog, jo qeu kur’m fiza mu ri i cob aw fozm vogoulqj!
@GET("/api/note")
fun getNotes(): Call<GetTasksResponse>
@GET("/api/user/profile")
fun getMyProfile(): Call<UserProfileResponse>
@POST("/api/note/complete")
fun completeTask(@Query("id") noteId: String): Call<CompleteNoteResponse>
@POST("/api/note")
fun addTask(@Body request: AddTaskRequest): Call<Task>
Ntax lnuutz za ev! :]
Xaw vke zmufufc, igx pto oafr qiafiq shoitj zo ezyuh ve efakf sojooyb owzu gou’na duksem ov!
Fei ge muqrul tiwe xe pu oy soisbukd! :]
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.