Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions GraphQLNetworkTester.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
AC162E262A923B0200990143 /* GeneratedModels.swift in Sources */ = {isa = PBXBuildFile; fileRef = AC162E252A923B0200990143 /* GeneratedModels.swift */; };
AC162E2A2A92578800990143 /* DataLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = AC162E292A92578800990143 /* DataLoader.swift */; };
AC162E2C2A9257B200990143 /* Queries.swift in Sources */ = {isa = PBXBuildFile; fileRef = AC162E2B2A9257B200990143 /* Queries.swift */; };
ACE87A7E2AB1462100EF4ACD /* FetchPRDataCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = ACE87A7D2AB1462100EF4ACD /* FetchPRDataCoordinator.swift */; };
ACE87A822AB1471300EF4ACD /* RepoFetchObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = ACE87A812AB1471300EF4ACD /* RepoFetchObject.swift */; };
ACE87A852AB14D3E00EF4ACD /* Queue in Frameworks */ = {isa = PBXBuildFile; productRef = ACE87A842AB14D3E00EF4ACD /* Queue */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -51,13 +54,16 @@
AC162E252A923B0200990143 /* GeneratedModels.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeneratedModels.swift; sourceTree = "<group>"; };
AC162E292A92578800990143 /* DataLoader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataLoader.swift; sourceTree = "<group>"; };
AC162E2B2A9257B200990143 /* Queries.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Queries.swift; sourceTree = "<group>"; };
ACE87A7D2AB1462100EF4ACD /* FetchPRDataCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FetchPRDataCoordinator.swift; sourceTree = "<group>"; };
ACE87A812AB1471300EF4ACD /* RepoFetchObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RepoFetchObject.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
AC162DF42A923AE400990143 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
ACE87A852AB14D3E00EF4ACD /* Queue in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -109,6 +115,8 @@
AC162E252A923B0200990143 /* GeneratedModels.swift */,
AC162E292A92578800990143 /* DataLoader.swift */,
AC162E2B2A9257B200990143 /* Queries.swift */,
ACE87A7D2AB1462100EF4ACD /* FetchPRDataCoordinator.swift */,
ACE87A812AB1471300EF4ACD /* RepoFetchObject.swift */,
);
path = GraphQLNetworkTester;
sourceTree = "<group>";
Expand Down Expand Up @@ -154,6 +162,9 @@
dependencies = (
);
name = GraphQLNetworkTester;
packageProductDependencies = (
ACE87A842AB14D3E00EF4ACD /* Queue */,
);
productName = GraphQLNetworkTester;
productReference = AC162DF72A923AE400990143 /* GraphQLNetworkTester.app */;
productType = "com.apple.product-type.application";
Expand Down Expand Up @@ -226,6 +237,9 @@
Base,
);
mainGroup = AC162DEE2A923AE400990143;
packageReferences = (
ACE87A832AB14D3E00EF4ACD /* XCRemoteSwiftPackageReference "Queue" */,
);
productRefGroup = AC162DF82A923AE400990143 /* Products */;
projectDirPath = "";
projectRoot = "";
Expand Down Expand Up @@ -269,10 +283,12 @@
buildActionMask = 2147483647;
files = (
AC162E2C2A9257B200990143 /* Queries.swift in Sources */,
ACE87A7E2AB1462100EF4ACD /* FetchPRDataCoordinator.swift in Sources */,
AC162E2A2A92578800990143 /* DataLoader.swift in Sources */,
AC162DFD2A923AE400990143 /* ContentView.swift in Sources */,
AC162DFB2A923AE400990143 /* GraphQLNetworkTesterApp.swift in Sources */,
AC162E262A923B0200990143 /* GeneratedModels.swift in Sources */,
ACE87A822AB1471300EF4ACD /* RepoFetchObject.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -588,6 +604,25 @@
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */

/* Begin XCRemoteSwiftPackageReference section */
ACE87A832AB14D3E00EF4ACD /* XCRemoteSwiftPackageReference "Queue" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/mattmassicotte/Queue";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 0.1.4;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
ACE87A842AB14D3E00EF4ACD /* Queue */ = {
isa = XCSwiftPackageProductDependency;
package = ACE87A832AB14D3E00EF4ACD /* XCRemoteSwiftPackageReference "Queue" */;
productName = Queue;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = AC162DEF2A923AE400990143 /* Project object */;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 20 additions & 12 deletions GraphQLNetworkTester/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ struct ContentView: View {
var body: some View {
HStack {
VStack {
Button("Get PRs") {
Task {
await FetchPRDataCoordinator().getPRs()
}
}

Text("Fetch Data for Pull Request")
.padding(1)
Form {
Expand All @@ -26,19 +32,21 @@ struct ContentView: View {
Button("Fetch PR") {
Task {
print("Attempting to fetch data for owner: \(owner), repo: \(repo), number: \(number)")
let result = try await DataLoader().request(forQraphQLQuery: .getPullRequestData(forOwner: owner, inRepo: repo, withNumber: number))
// let result = try await DataLoader().request(forQraphQLQuery: .getPullRequestData(forOwner: owner, inRepo: repo, withNumber: number))
let result = await fetchPRDataFromGitHub(forOwner: owner, inRepo: repo, forNumber: number)
if let validData = result {
print("We're in the unwrapped return value and have some data!")
let stringData = String(data: validData, encoding: String.Encoding.ascii)
print("String data is: \(String(describing: stringData))")

let decodedPRData = try? JSONDecoder().decode(DecodePRStartingPoint.self, from: validData)
if let unwrappedDecodedData = decodedPRData {
print("Successfully decoded data! The PR number is: ")
print(unwrappedDecodedData.data.repository.pullRequest.number)
} else {
print("No data here! Couldn't decode it.")
}
print("I've got \(validData.commits.nodes.count) commits")
// print("We're in the unwrapped return value and have some data!")
// let stringData = String(data: validData, encoding: String.Encoding.ascii)
// print("String data is: \(String(describing: stringData))")
//
// let decodedPRData = try? JSONDecoder().decode(DecodePRStartingPoint.self, from: validData)
// if let unwrappedDecodedData = validData {
// print("Successfully decoded data! The PR number is: ")
// print(unwrappedDecodedData.data.repository.pullRequest.number)
// } else {
// print("No data here! Couldn't decode it.")
// }
}
}
}
Expand Down
34 changes: 27 additions & 7 deletions GraphQLNetworkTester/DataLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import Foundation

class DataLoader {
var remainingAPIRequests = 0

func request(forQraphQLQuery: GraphQLQuery) async throws -> Data? {
let token = "<INSERT_YOUR_TOKEN_HERE>"
let token = "INSERT-YOUR-API-KEY-HERE"

let gitHubGraphQLURL = URL(string: "https://api.github.com/graphql")

Expand Down Expand Up @@ -46,12 +48,30 @@ class DataLoader {
// Code might suspend here
let (data, response) = try await URLSession.shared.data(for: apiRequest)

guard (response as? HTTPURLResponse)?.statusCode == 200 else {
throw DataFetcherError.serverError
let httpUrlResponse = response as? HTTPURLResponse
if let unwrappedResponse = httpUrlResponse {
let statusCode = unwrappedResponse.statusCode

if statusCode == 200 {
let remainingRequests = unwrappedResponse.value(forHTTPHeaderField: "x-ratelimit-remaining")
if let unwrappedOptionalRemainingRequests = remainingRequests {
let remainingRequestsAsInt = Int(unwrappedOptionalRemainingRequests)
if let validInt = remainingRequestsAsInt {
remainingAPIRequests = validInt
print("Available requests remaining: \(remainingAPIRequests)")
}
}
return data
} else {
print("There was an error fetching data from GitHub: \(statusCode.description)")
let decodedErrorInfo = try? JSONDecoder().decode(DecodeGitHubAPIErrorBody.self, from: data)
if let unwrappedErrorMessage = decodedErrorInfo {
print("The GitHub error message was: \(unwrappedErrorMessage)")
throw DataFetcherError.serverError(unwrappedErrorMessage.message)
}
throw DataFetcherError.serverError("There was no decoded error message from GitHub")
}
}

//print(response as? HTTPURLResponse)

return data
}
}
Expand All @@ -60,5 +80,5 @@ enum DataFetcherError: Error {
case invalidURL
case notFound
case typeNotFound
case serverError
case serverError(String?)
}
Loading