package Ġcom . silver car . un le ash . repository Ċ Ċ import Ġcom . silver car . un le ash . Un le ash Exception Ċ import Ġcom . silver car . un le ash . repository . Feature Toggle Response . Status . CHANGED Ċ import Ġcom . silver car . un le ash . repository . Feature Toggle Response . Status . NOT _ CHANGED Ċ import Ġcom . silver car . un le ash . repository . Feature Toggle Response . Status . UN AVAILABLE Ċ import Ġcom . silver car . un le ash . util . Un le ash Config Ċ import Ġjava . io . BufferedReader Ċ import Ġjava . io . IOException Ċ import Ġjava . io . InputStream Ċ import Ġjava . io . InputStream Reader Ċ import Ġjava . net . Http URLConnection Ċ import Ġjava . net . URL Ċ import Ġkotlin . text . Charsets . UTF _ 8 Ċ Ċ class ĠHttp Toggle Fetcher ( private Ġval Ġun le ash Config : ĠUn le ash Config ) Ġ: ĠToggle Fetcher Ġ{ ĊĠ Ġprivate Ġvar Ġe Tag Ġ= Ġ"" ĊĠ Ġprivate Ġval Ġtoggle Url : ĠURL Ġ= Ġun le ash Config . un le ash URLs . fetch Toggle s URL ĊĠ Ġprivate Ġval Ġjson Toggle Parser Ġ= ĠJson Toggle Parser () ĊĊĠ Ġ@ Throws ( Un le ash Exception :: class ) ĊĠ Ġoverride Ġfun Ġfetch Toggle s (): ĠFeature Toggle Response Ġ{ ĊĠĠĠ Ġvar Ġconnection : ĠHttp URLConnection ? Ġ= Ġnull ĊĠĠĠ Ġreturn Ġtry Ġ{ ĊĠĠĠĠĠ Ġconnection Ġ= Ġtoggle Url . open Connection () Ġas ĠHttp URLConnection ĊĠĠĠĠĠ Ġconnection . connect Timeout Ġ= ĠCONNECT _ TIMEOUT ĊĠĠĠĠĠ Ġconnection . read Timeout Ġ= ĠCONNECT _ TIMEOUT ĊĠĠĠĠĠ Ġconnection . setRequest Property (" Accept ", Ġ" application / json ") ĊĠĠĠĠĠ Ġconnection . setRequest Property (" Content - Type ", Ġ" application / json ") ĊĊĠĠĠĠĠ Ġun le ash Config . setRequest Properties ( connection ) ĊĊĠĠĠĠĠ Ġif Ġ( e Tag . isNotEmpty ()) Ġ{ ĊĠĠĠĠĠĠĠ Ġconnection . setRequest Property (" If - None - Match ", Ġe Tag ) ĊĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠ Ġconnection . use Caches Ġ= Ġtrue ĊĠĠĠĠĠ Ġconnection . connect () ĊĊĠĠĠĠĠ Ġval Ġresponse Code Ġ= Ġconnection . response Code ĊĠĠĠĠĠ Ġwhen Ġ{ ĊĠĠĠĠĠĠĠ Ġresponse Code Ġ< ĠHTTP _ STATUS _ MULTIPLE _ CHO ICES Ġ-> Ġ{ ĊĠĠĠĠĠĠĠĠĠ Ġget Toggle Response ( connection ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġresponse Code Ġ== ĠHTTP _ STATUS _ NOT _ MODI FIED Ġ-> Ġ{ ĊĠĠĠĠĠĠĠĠĠ ĠFeature Toggle Response ( NOT _ CHANGED , Ġresponse Code ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġelse Ġ-> Ġ{ ĊĠĠĠĠĠĠĠĠĠ ĠFeature Toggle Response ( UN AVAILABLE , Ġresponse Code , Ġlocation Ġ= Ġget Location Header ( connection )) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ġcatch Ġ( e : ĠIOException ) Ġ{ ĊĠĠĠĠĠ Ġthrow ĠUn le ash Exception (" Could Ġnot Ġfetch Ġtoggle s ", Ġe ) ĊĠĠĠ Ġ} Ġcatch Ġ( e : ĠIllegalStateException ) Ġ{ ĊĠĠĠĠĠ Ġthrow ĠUn le ash Exception ( e . message , Ġe ) ĊĠĠĠ Ġ} Ġfinally Ġ{ ĊĠĠĠĠĠ Ġconnection ?. disconnect () ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Throws ( IOException :: class ) ĊĠ Ġprivate Ġfun Ġget Toggle Response ( request : ĠHttp URLConnection ): ĠFeature Toggle Response Ġ{ ĊĠĠĠ Ġe Tag Ġ= Ġrequest . getHeader Field (" ET ag ") Ġ?: Ġ"" ĊĊĠĠĠ ĠBufferedReader ( InputStream Reader ( request . content Ġas ĠInputStream , ĠUTF _ 8 )). use Ġ{ Ġreader Ġ-> ĊĠĠĠĠĠ Ġval Ġtoggle s Ġ= Ġjson Toggle Parser . fromJson ( reader ) ĊĠĠĠĠĠ Ġreturn ĠFeature Toggle Response ( CHANGED , Ġtoggle Collection Ġ= Ġtoggle s ) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġget Location Header ( connection : ĠHttp URLConnection ): ĠString ? Ġ{ ĊĠĠĠ Ġreturn Ġconnection . getHeader Field (" Location ") ĊĠ Ġ} ĊĊĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠ Ġprivate Ġconst Ġval ĠCONNECT _ TIMEOUT Ġ= Ġ 1 0 0 0 0 ĊĠĠĠ Ġprivate Ġconst Ġval ĠHTTP _ STATUS _ MULTIPLE _ CHO ICES Ġ= Ġ 3 0 0 ĊĠĠĠ Ġprivate Ġconst Ġval ĠHTTP _ STATUS _ NOT _ MODI FIED Ġ= Ġ 3 0 4 ĊĠ Ġ} Ċ } Ċ