Last active
July 18, 2023 20:48
-
-
Save billypchan/2e6cb788b19d7c1f82d2c3b518dfc187 to your computer and use it in GitHub Desktop.
Getting Phrase.com String rsc key and id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
import Foundation | |
// Set up your authentication token | |
let authToken = "authToken" | |
// Specify the project ID | |
let projectId = "projectId" | |
// Define the API endpoint URL | |
let apiUrl = "https://api.phrase.com/v2/projects/\(projectId)/keys" | |
// Define the page size for pagination | |
let pageSize = 100 // Adjust the page size as needed | |
struct PhraseKey { | |
let keyId, name: String | |
} | |
var phraseKeys = [PhraseKey]() | |
// Create the URL request | |
func createURLRequest(page: Int) -> URLRequest { | |
let url = URL(string: "\(apiUrl)?page=\(page)&per_page=\(pageSize)")! | |
var request = URLRequest(url: url) | |
request.httpMethod = "GET" | |
request.addValue("Bearer \(authToken)", forHTTPHeaderField: "Authorization") | |
return request | |
} | |
// Send the request and process the keys | |
func processKeys(data: Data) async { | |
do { | |
// Parse the JSON response | |
if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] { | |
for keyData in json { | |
if let keyId = keyData["id"] as? String, | |
let name = keyData["name"] as? String { | |
print("Key ID: \(keyId), Name: \(name)") | |
phraseKeys.append(PhraseKey(keyId: keyId, name: name)) | |
} | |
} | |
} | |
} catch { | |
print("Error parsing JSON response: \(error)") | |
} | |
} | |
// Send the request with pagination | |
func sendRequest(page: Int) async { | |
let request = createURLRequest(page: page) | |
do { | |
let (data, response) = try await URLSession.shared.data(for: request) | |
if let httpResponse = response as? HTTPURLResponse { | |
await processKeys(data: data) | |
// Pagination handling | |
if let linkHeader = httpResponse.value(forHTTPHeaderField: "Link") { | |
let nextPage = getNextPage(linkHeader: linkHeader) | |
if nextPage > page { | |
await sendRequest(page: nextPage) | |
} | |
} | |
} | |
} catch { | |
print("Error retrieving keys: \(error)") | |
} | |
} | |
// Extract the next page number from the Link header | |
func getNextPage(linkHeader: String) -> Int { | |
let components = linkHeader.components(separatedBy: ",") | |
for component in components { | |
if component.contains("rel=next") { | |
if let range = component.range(of: "page=\\d+", options: .regularExpression) { | |
let pageNumberString = component[range].replacingOccurrences(of: "page=", with: "") | |
if let pageNumber = Int(pageNumberString) { | |
return pageNumber | |
} | |
} | |
} | |
} | |
return 0 | |
} | |
// Start the request with the first page | |
Task { | |
await sendRequest(page: 1) | |
print("phraseKeys.count: \(phraseKeys.count)") | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment