JsonRequest
public protocol JsonRequest : Request where Self.Result : Decodable
This request protocol is a specialization of the Request
protocol. It can be used often when
working with a JSON API where the returned data is a JSON object. As a requirement, the
request’s result type must implement the Decodable
protocol. The decode(_:)
method is then
synthesized automatically by using a JSONDecoder
and decoding the raw data to the specified
type. decodeSnakeCase
can further be used to modify the behavior of the aforementioned
decoder.
-
decodeSnakeCase
Default implementationDefines whether the decoder decoding the raw data to the result type should consider camel case in the Swift code as snake case in the JSON (i.e.
userID
would be parsed from the fielduser_id
if not specified explicity in the type to decode to). By default, attributes are decoded using snake case attribute names.Default Implementation
Declaration
Swift
var decodeSnakeCase: Bool { get }
-
decode(_:
Extension method) Declaration
Swift
public func decode(_ data: Data) throws -> Result
-
schedule(forPaginationWith:
Extension methodchunk: zeroBasedPageIndex: paginatedType: ) This method is very similar to the method
Request.schedule(forPaginationWith:chunk:zeroBasedPageIndex:decode:)
, however, the user does not have to explicitly define adecode
function whenever both the actual result and the type ofPaginatedData
to be used conform to theDecodable
protocol. The type of the paginated data is tried to be inferred automatically, but might need to be given explicitly in some circumstances.Declaration
Swift
public func schedule<P, S>(forPaginationWith service: S, chunk: Int, zeroBasedPageIndex: Bool = false, paginatedType: P.Type = P.self) -> Paginator<Self, P, S> where P: PaginatedData, P.DataType == Result, P: Decodable, S: HttpService
Parameters
service
The service representing the API against which to schedule paginated requests.
chunk
The (maximum) number of elements that are requested per page. The number of returned elements is only smaller than the given chunk if the given page index is the index of the last page and the number of elements is not divisible by the chunk.
zeroBasedPageIndex
Whether the API endpoint that the request is scheduled against indexes the first page with 0. By default, the first page is indexed by 1.
paginatedType
The paginated data type to which to decode a response.