I am creating an app that connects to the notion api through a post request to query and display the contents of a database I created. This is the image of the database and what it looks like on my app. The issue right now is sorting the names and tags based on the time it was created and encoding the response body.How do I perform a post request to sort the data based on the time it was created ? I don't understand to solve this based on the documentation. Below is my networking code.
class NetworkService{
static let shared = NetworkService()
let urlString = "https://api.notion.com/v1/databases/""/query"
func getJSON(completion: @escaping ([Result]?, Error?) -> Void){
guard let url = URL(string: urlString ) else{
return
}
var request = URLRequest(url: url)
let headers = ["Accept": "application/json","Notion-Version": "2021-08-16"]
request.allHTTPHeaderFields = headers
request.httpMethod = "POST"
request.setValue ("secret_"", forHTTPHeaderField: "Authorization")
request.setValue ("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try JSONEncoder().encode([Result].self)
URLSession.shared.dataTask(with: request) { data, response, err in
if let error = err {
print("Failed to query database", error)
return
}
guard let data = data else {
print("Data not received\(String(describing: err))")
return
}
let decoder = JSONDecoder()
do{
let decodedJson = try decoder.decode(DatabaseResponse.self, from: data)
DispatchQueue.main.async {
completion(decodedJson.results, nil)
}
}catch let error{
print("Json failed to decode",error)
return
}
}.resume()
}
}
[1]: https://developers.notion.com/reference/post-database-query
[2]: https://ibb.co/4F00Hh2
[3]: https://ibb.co/mcn2YLf