Reputation: 19
Im using Notions API to print out some data from one of my own pages in notion and im using URLSession to make the request then parsing the unwrapped data but nothing is being returned to my console and I know my endpoint and API key is correct. I've gone through the notion API documentation can't can't seem to find anything in it that I am not doing or doing wrong. Ill provide my code as well as the documentation I've been consulting: https://developers.notion.com/reference/intro
'''
import Foundation
struct Page: Codable {
let id: String
let title: String
}
let endpoint = URL(string: "https://api.notion.com/v1/pages/8efc0ca3d9cc44fbb1f34383b794b817")
let apiKey = "… redacted …"
let session = URLSession.shared
func makeRequest() {
if let endpoint = endpoint {
let task = URLSession.shared.dataTask(with: endpoint) { data, response, error in
if let taskError = error {
print("could not establish url request:\(taskError)")
return
}
if let unwrapData = data { //safely unwrapping the data value using if let
do {
let decoder = JSONDecoder() //JSONDecoder method to decode api data,
let codeUnwrappedData = try decoder.decode(Page.self,from: unwrapData) //type: specifies its a struct, from: passes the data parmeter that contains the api data to be decoded
} catch {
print("could not parse json data")
}
}
if let httpResponse = response as? HTTPURLResponse {
if httpResponse.statusCode == 200 {
if let apiData = data {
print(String(data: apiData, encoding: .utf8)!)
}
} else {
print("unsuccessful http response:\(httpResponse)")
}
makeRequest()
}
}
task.resume()
}
}
'''
Ive tried using the CFNetwork debugging tool but upon adding it into my environment variables and setting a value, nothing new appears or changes when I build my project.
Upvotes: 0
Views: 125
Reputation: 36304
Try this example code to connect to your server and retrieve some data. I don't have an apikey so I cannot test this, but the code should give you some ideas.
struct ContentView: View {
var body: some View {
Text("testing")
.onAppear {
makeRequest() { results in
print("--->results: \(results)\n")
switch results {
case .success(let page):
print("-----> page: \(page)")
case .failure(let error):
print("-----> error: \(error)")
}
}
}
}
let endpoint = URL(string: "https://api.notion.com/v1/pages/8efc0ca3d9cc44fbb1f34383b794b817")
let apiKey = "YOUR-APIKEY"
func makeRequest(completion: @escaping (Result<Page, Error>) -> Void) {
if let endpoint = endpoint {
var request = URLRequest(url: endpoint)
request.httpMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "content-type")
request.addValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let taskError = error {
print("could not establish url request: \(taskError)")
completion(.failure(taskError))
return
}
if let unwrapData = data {
print("--->data: \(String(data: unwrapData, encoding: .utf8))\n")
do {
let decoder = JSONDecoder()
let codeUnwrappedData = try decoder.decode(Page.self,from: unwrapData)
print("--->codeUnwrappedData: \(codeUnwrappedData)")
completion(.success(codeUnwrappedData))
} catch {
print("could not parse json data: \(error)\n") // <--- important
completion(.failure(error))
}
}
}
task.resume()
}
}
}
struct Page: Codable {
let id: String
let title: String
}
If you get some errors while decoding the JSON response, look at the docs to ensure that your Page
model match the json data.
Upvotes: 0