|
| 1 | +// |
| 2 | +// Contentful |
| 3 | +// |
| 4 | +// Created by Tomasz Szulc on 26/09/2020. |
| 5 | +// Copyright © 2020 Contentful GmbH. All rights reserved. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +public extension Client { |
| 11 | + |
| 12 | + /** |
| 13 | + Fetches the raw `Data` objects and bypass the JSON parsing provided by the SDK. |
| 14 | + |
| 15 | + - Parameters: |
| 16 | + - url: The URL representing the endpoint with query parameters. |
| 17 | + - completion: The completion handler to call when the request is complete. |
| 18 | + */ |
| 19 | + @discardableResult |
| 20 | + func fetch( |
| 21 | + url: URL, |
| 22 | + then completion: @escaping ResultsHandler<Data> |
| 23 | + ) -> URLSessionDataTask { |
| 24 | + fetchData( |
| 25 | + url: url, |
| 26 | + completion: completion |
| 27 | + ) |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + Fetches the JSON data at the specified URL and decoding it. |
| 32 | + |
| 33 | + - Parameters: |
| 34 | + - url: The URL representing the endpoint with query parameters. |
| 35 | + - completion: The completion handler wrapping `DecodableType` to call when the request is complete. |
| 36 | + */ |
| 37 | + @discardableResult |
| 38 | + func fetch<DecodableType: Decodable>( |
| 39 | + url: URL, |
| 40 | + then completion: @escaping ResultsHandler<DecodableType> |
| 41 | + ) -> URLSessionDataTask { |
| 42 | + fetchDecodable( |
| 43 | + url: url, |
| 44 | + completion: completion |
| 45 | + ) |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + Fetches a resource by id. |
| 50 | + |
| 51 | + Available resource types that match this function's constraints are: `Space`, `Asset`, `ContentType`, `Entry`, |
| 52 | + or any of custom types conforming to `EntryDecodable` or `AssetDecodable`. |
| 53 | + |
| 54 | + - Parameters: |
| 55 | + - resourceType: A reference to the Swift type which conforms to `Decodable & EndpointAccessible`. |
| 56 | + - id: The identifier of the resource. |
| 57 | + - include: The level of includes to be resolved. Default value when nil. See more: [Retrieval of linked items]. |
| 58 | + - completion: The completion handler to call when the request is complete. |
| 59 | + |
| 60 | + [Retrieval of linked items]: https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/links/retrieval-of-linked-items |
| 61 | + */ |
| 62 | + @discardableResult |
| 63 | + func fetch<ResourceType>( |
| 64 | + _ resourceType: ResourceType.Type, |
| 65 | + id: String, |
| 66 | + include includesLevel: UInt? = nil, |
| 67 | + then completion: @escaping ResultsHandler<ResourceType> |
| 68 | + ) -> URLSessionDataTask where ResourceType: Decodable & EndpointAccessible { |
| 69 | + fetchResource( |
| 70 | + resourceType: resourceType, |
| 71 | + id: id, |
| 72 | + include: includesLevel, |
| 73 | + completion: completion |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + Fetches collections of `ContentType`, `Entry`, and `Asset` types. |
| 79 | + |
| 80 | + - Parameters: |
| 81 | + - resourceType: A reference to concrete resource class which conforms to `Decodable & EndpointAccessible & ResourceQueryable`. |
| 82 | + - query: Query to match results against. |
| 83 | + - completion: The completion handler with `ArrayResponse` to call when the request is complete. |
| 84 | + */ |
| 85 | + @discardableResult |
| 86 | + func fetchArray<ResourceType, QueryType>( |
| 87 | + of resourceType: ResourceType.Type, |
| 88 | + matching query: QueryType? = nil, |
| 89 | + then completion: @escaping ResultsHandler<HomogeneousArrayResponse<ResourceType>> |
| 90 | + ) -> URLSessionDataTask where ResourceType: ResourceQueryable, QueryType == ResourceType.QueryType { |
| 91 | + fetchDecodable( |
| 92 | + url: url(endpoint: ResourceType.endpoint, parameters: query?.parameters ?? [:]), |
| 93 | + completion: completion |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + Fetches collections of `EntryDecodable` of your own definition. |
| 99 | + |
| 100 | + - Parameters: |
| 101 | + - entryType: A reference to a concrete Swift class conforming to `EntryDecodable` that will be fetched. |
| 102 | + - query: Query to match results against. |
| 103 | + - completion: The completion handler with `ArrayResponse` to call when the request is complete. |
| 104 | + */ |
| 105 | + @discardableResult |
| 106 | + func fetchArray<EntryType>( |
| 107 | + of entryType: EntryType.Type, |
| 108 | + matching query: QueryOn<EntryType> = QueryOn<EntryType>(), |
| 109 | + then completion: @escaping ResultsHandler<HomogeneousArrayResponse<EntryType>> |
| 110 | + ) -> URLSessionDataTask { |
| 111 | + fetchDecodable( |
| 112 | + url: url(endpoint: .entries, parameters: query.parameters), |
| 113 | + completion: completion |
| 114 | + ) |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + Fetches heterogenous collections of types conforming to `EntryDecodable`. |
| 119 | + |
| 120 | + - Parameters: |
| 121 | + - query: Query to match results against. |
| 122 | + - completion: The completion handler to call when the request is complete. |
| 123 | + */ |
| 124 | + @discardableResult |
| 125 | + func fetchArray( |
| 126 | + matching query: Query? = nil, |
| 127 | + then completion: @escaping ResultsHandler<HeterogeneousArrayResponse> |
| 128 | + ) -> URLSessionDataTask { |
| 129 | + fetchDecodable( |
| 130 | + url: url(endpoint: .entries, parameters: query?.parameters ?? [:]), |
| 131 | + completion: completion |
| 132 | + ) |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + Fetches data associated with `AssetProtocol` object. |
| 137 | + |
| 138 | + - Parameters: |
| 139 | + - asset: Instance that has the URL for media file. |
| 140 | + - imageOptions: Options for server-side manipulations of image files. |
| 141 | + - completion: The completion handler to call when the request is complete. |
| 142 | + */ |
| 143 | + @discardableResult |
| 144 | + func fetchData( |
| 145 | + for asset: AssetProtocol, |
| 146 | + with imageOptions: [ImageOption] = [], |
| 147 | + then completion: @escaping ResultsHandler<Data> |
| 148 | + ) -> URLSessionDataTask? { |
| 149 | + do { |
| 150 | + let url = try asset.url(with: imageOptions) |
| 151 | + return fetchData( |
| 152 | + url: url, |
| 153 | + completion: completion |
| 154 | + ) |
| 155 | + } catch let error { |
| 156 | + completion(.failure(error)) |
| 157 | + return nil |
| 158 | + } |
| 159 | + } |
| 160 | + /** |
| 161 | + Fetches the space this client is configured to interface with. |
| 162 | + |
| 163 | + - Parameters: |
| 164 | + - completion: The completion handler to call when the reqeust is complete. |
| 165 | + */ |
| 166 | + @discardableResult |
| 167 | + func fetchSpace(then completion: @escaping ResultsHandler<Space>) -> URLSessionDataTask? { |
| 168 | + fetchCurrentSpace(then: completion) |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + Fetches all `Locale`s belonging to the current space the client is configured to interface with. |
| 173 | + |
| 174 | + - Parameters: |
| 175 | + - completion: The completion handler to call when the request is complete. |
| 176 | + */ |
| 177 | + @discardableResult |
| 178 | + func fetchLocales(then completion: @escaping ResultsHandler<HomogeneousArrayResponse<Contentful.Locale>>) -> URLSessionDataTask { |
| 179 | + fetchCurrentSpaceLocales(then: completion) |
| 180 | + } |
| 181 | +} |
0 commit comments