Skip to content

Commit

Permalink
Code and Data as service response working
Browse files Browse the repository at this point in the history
  • Loading branch information
yury authored and Carlos Cabanero committed Jan 24, 2023
1 parent 58ba9ab commit d48e09c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
57 changes: 36 additions & 21 deletions Blink/BuildApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,20 @@ enum BuildAPIError: Error, LocalizedError {

enum BuildAPI {

func requestService(request: URLRequest) async {
static func requestService(_ request: URLRequest) async -> (Int32, Data) {
var signal: TokioSignals!

await withTaskCancellationHandler(operation: {
await withCheckedContinuation { (c: CheckedContinuation<(), Never>) in
let ctx = UnsafeMutablePointer<CheckedContinuation<(), Never>>.allocate(capacity: 1)
return await withTaskCancellationHandler(operation: {
await withCheckedContinuation { (c: CheckedContinuation<(Int32, Data), Never>) in
let ctx = UnsafeMutablePointer<CheckedContinuation<(Int32, Data), Never>>.allocate(capacity: 1)
ctx.initialize(to: c)

signal = TokioSignals.requestService(request, auth: true, ctx: ctx) { ctx, w in
let ref = UnsafeMutablePointer<CheckedContinuation<(), Never>>(OpaquePointer(ctx))
let ref = UnsafeMutablePointer<CheckedContinuation<(Int32, Data), Never>>(OpaquePointer(ctx))
let c = ref.move()
ref.deallocate()
c.resume(returning: ())
let data = Data(bytes: w.pointee.body, count: Int(w.pointee.body_len));
c.resume(returning: (w.pointee.code, data))
}
}
}, onCancel: { [signal] in
Expand All @@ -71,6 +72,15 @@ enum BuildAPI {

}

public static func accountInfo() async {

let (code, data) = try await requestService(.init(getJson: _path("/account")))
let s = String(data: data, encoding: .utf8)!;
if code == 200 {
print(s)
}
}

private static func _baseURL() -> String {
let options = PublishingOptions.current;
if options.intersection([PublishingOptions.testFlight, PublishingOptions.developer]).isEmpty {
Expand All @@ -86,14 +96,7 @@ enum BuildAPI {

private static func _post(_ url: URL, params: [String: Any]) async throws -> (Int, Data, [String: Any]) {

let json = try JSONSerialization.data(withJSONObject: params)

var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = json
request.addValue("application/json", forHTTPHeaderField: "Content-Type")

let (data, response) = try await URLSession.shared.data(for: request)
let (data, response) = try await URLSession.shared.data(for: .init(postJson: url, params: params))

guard let response = response as? HTTPURLResponse else {
throw BuildAPIError.invalidResponse
Expand All @@ -109,13 +112,7 @@ enum BuildAPI {

private static func _get(_ url: URL, params: [String: Any] = [:]) async throws -> (Int, Data, [String: Any]) {

// TODO: handle params

var request = URLRequest(url: url)
request.httpMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")

let (data, response) = try await URLSession.shared.data(for: request)
let (data, response) = try await URLSession.shared.data(for: .init(getJson: url, params: params))

guard let response = response as? HTTPURLResponse else {
throw BuildAPIError.invalidResponse
Expand Down Expand Up @@ -199,3 +196,21 @@ enum BuildAPI {
}
}
}


extension URLRequest {
init(postJson url: URL, params: [String: Any]) throws {
self.init(url: url)
self.httpMethod = "POST"
self.httpBody = try JSONSerialization.data(withJSONObject: params)
self.addValue("application/json", forHTTPHeaderField: "Content-Type")
}

init(getJson url: URL, params: [String: Any] = [:]) {

// TODO: handle params
self.init(url: url)
self.httpMethod = "GET"
self.addValue("application/json", forHTTPHeaderField: "Content-Type")
}
}
5 changes: 3 additions & 2 deletions Blink/Commands/TokioSignals.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@
NS_ASSUME_NONNULL_BEGIN

typedef struct BuildHTTPResponse {
UInt32 code;
char * body;
const int32_t code;
const void * body;
const NSUInteger body_len;
} BuildHTTPResponse;

typedef void (*build_service_callback) (void *, BuildHTTPResponse *);
Expand Down
4 changes: 3 additions & 1 deletion Blink/Commands/TokioSignals.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
extern void build_call_service(
const char * url,
const char * method,
const char * body,
const void * body,
NSUInteger body_len,
const char * content_type,
BOOL auth, void * ctx,
build_service_callback callback,
Expand All @@ -63,6 +64,7 @@ + (instancetype) requestService:
request.URL.absoluteString.UTF8String,
request.HTTPMethod.UTF8String,
request.HTTPBody.bytes,
request.HTTPBody.length,
[request valueForHTTPHeaderField:@"Content-Type"].UTF8String,
auth,
ctx, callback, &signals->_signals);
Expand Down
6 changes: 5 additions & 1 deletion Settings/ViewControllers/Subscriptions/BuildView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,11 @@ struct BuildAccountView: View {
}
Section(header: Text("Usage")) {
list().accentColor(.green)
}
}.onAppear(perform: {
Task {
await BuildAPI.accountInfo()
}
})
}
.tint(.green)
.alert(errorMessage: $_model.alertErrorMessage)
Expand Down

0 comments on commit d48e09c

Please sign in to comment.