2018-09-11 14:52:21 +00:00
//
// T i m e l i n e . s w i f t
// P a c h y d e r m
//
// C r e a t e d b y S h a d o w f a c t s o n 9 / 9 / 1 8 .
// C o p y r i g h t © 2 0 1 8 S h a d o w f a c t s . A l l r i g h t s r e s e r v e d .
//
import Foundation
public enum Timeline {
case home
case ` public ` ( local : Bool )
2019-12-20 03:41:23 +00:00
case instance ( instanceURL : URL )
2018-09-11 14:52:21 +00:00
case tag ( hashtag : String )
case list ( id : String )
case direct
}
extension Timeline {
2018-09-14 15:35:00 +00:00
var endpoint : String {
2018-09-11 14:52:21 +00:00
switch self {
case . home :
2018-09-14 15:35:00 +00:00
return " /api/v1/timelines/home "
2019-12-20 03:41:23 +00:00
case . public , . instance ( _ ) :
2018-09-14 15:35:00 +00:00
return " /api/v1/timelines/public "
2018-09-11 14:52:21 +00:00
case let . tag ( hashtag ) :
2018-09-14 15:35:00 +00:00
return " /api/v1/timelines/tag/ \( hashtag ) "
2018-09-11 14:52:21 +00:00
case let . list ( id ) :
2018-09-14 15:35:00 +00:00
return " /api/v1/timelines/list/ \( id ) "
2018-09-11 14:52:21 +00:00
case . direct :
2018-09-14 15:35:00 +00:00
return " /api/v1/timelines/direct "
}
}
func request ( range : RequestRange ) -> Request < [ Status ] > {
2019-12-20 03:41:23 +00:00
var request : Request < [ Status ] >
if case let . instance ( instanceURL ) = self {
request = Request < [ Status ] > ( method : . get , baseURL : instanceURL , path : endpoint )
} else {
request = Request < [ Status ] > ( method : . get , path : endpoint )
}
2018-09-14 15:35:00 +00:00
if case . public ( true ) = self {
request . queryParameters . append ( " local " = > true )
2018-09-11 14:52:21 +00:00
}
request . range = range
return request
}
}
2019-09-16 00:43:06 +00:00
extension Timeline : Codable {
public init ( from decoder : Decoder ) throws {
let container = try decoder . container ( keyedBy : CodingKeys . self )
let type = try container . decode ( String . self , forKey : . type )
switch type {
case " home " :
self = . home
case " public " :
self = . public ( local : try container . decode ( Bool . self , forKey : . local ) )
2019-12-20 03:41:23 +00:00
case " instanceURL " :
self = . instance ( instanceURL : try container . decode ( URL . self , forKey : . instanceURL ) )
2019-09-16 00:43:06 +00:00
case " tag " :
self = . tag ( hashtag : try container . decode ( String . self , forKey : . hashtag ) )
case " list " :
self = . list ( id : try container . decode ( String . self , forKey : . listID ) )
case " direct " :
self = . direct
default :
throw DecodingError . dataCorruptedError ( forKey : CodingKeys . type , in : container , debugDescription : " Timeline type must be one of 'home', 'local', 'tag', 'list', or 'direct' " )
}
}
public func encode ( to encoder : Encoder ) throws {
var container = encoder . container ( keyedBy : CodingKeys . self )
switch self {
case . home :
try container . encode ( " home " , forKey : . type )
case let . public ( local ) :
try container . encode ( " public " , forKey : . type )
try container . encode ( local , forKey : . local )
2019-12-20 03:41:23 +00:00
case let . instance ( instanceURL ) :
try container . encode ( " instanceURL " , forKey : . type )
try container . encode ( instanceURL , forKey : . instanceURL )
2019-09-16 00:43:06 +00:00
case let . tag ( hashtag ) :
try container . encode ( " tag " , forKey : . type )
try container . encode ( hashtag , forKey : . hashtag )
case let . list ( id ) :
try container . encode ( " list " , forKey : . type )
try container . encode ( id , forKey : . listID )
case . direct :
try container . encode ( " direct " , forKey : . type )
}
}
enum CodingKeys : String , CodingKey {
case type
case local
2019-12-20 03:41:23 +00:00
case instanceURL
2019-09-16 00:43:06 +00:00
case hashtag
case listID
}
}