Skip to content

Commit

Permalink
Generate missing app auth routes, clean up task reconnection helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlocke committed Jan 5, 2024
1 parent 4753481 commit 01f17c1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
44 changes: 35 additions & 9 deletions stone/backends/swift_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ def _generate_routes(self, namespace):
template_globals['objc_init_args_to_swift'] = self._objc_init_args_to_swift
template_globals['objc_result_from_swift'] = self._objc_result_from_swift
template_globals['objc_no_defualts_func_args'] = self._objc_no_defualts_func_args
template_globals['objc_app_auth_route_wrapper_already_defined'] = self._objc_app_auth_route_wrapper_already_defined

Check warning on line 215 in stone/backends/swift_client.py

View check run for this annotation

Codecov / codecov/patch

stone/backends/swift_client.py#L215

Added line #L215 was not covered by tests

ns_class = self._class_name(fmt_class(namespace.name))

Expand All @@ -220,6 +221,7 @@ def _generate_routes(self, namespace):
template.globals = template_globals

output_from_parsed_template = template.render(namespace=namespace)

self._write_output_in_target_folder(output_from_parsed_template,
'DBX{}Routes.swift'.format(ns_class),
True,
Expand All @@ -245,15 +247,21 @@ def _generate_request_boxes(self, api):
template_globals['request_type_signature'] = self._request_type_signature
template_globals['fmt_func'] = fmt_func
template_globals['fmt_route_objc_class'] = self._fmt_route_objc_class
swift_class_name = '{}RequestBox'.format(self.args.class_name)

Check warning on line 250 in stone/backends/swift_client.py

View check run for this annotation

Codecov / codecov/patch

stone/backends/swift_client.py#L250

Added line #L250 was not covered by tests

if self.args.objc:
template = self._jinja_template("ObjCRequestBox.jinja")
template.globals = template_globals

# don't include the default case in the generated switch statement if it's unreachable
include_default_in_switch = len(background_objc_routes) < len(background_compatible_routes)

Check warning on line 257 in stone/backends/swift_client.py

View check run for this annotation

Codecov / codecov/patch

stone/backends/swift_client.py#L257

Added line #L257 was not covered by tests

class_name = 'DBX{}RequestBox'.format(self.args.class_name)
output = template.render(background_compatible_routes=background_compatible_routes,
background_objc_routes=background_objc_routes,
class_name=class_name)
class_name=swift_class_name,
include_default_in_switch=include_default_in_switch
)
file_name = 'DBX{}RequestBox.swift'.format(self.args.class_name)
self._write_output_in_target_folder(output,
file_name,
Expand All @@ -263,10 +271,9 @@ def _generate_request_boxes(self, api):
template = self._jinja_template("SwiftRequestBox.jinja")
template.globals = template_globals

class_name = '{}RequestBox'.format(self.args.class_name)
output = template.render(background_compatible_routes=background_compatible_routes,
background_objc_routes=background_objc_routes,
class_name=class_name)
class_name=swift_class_name)
self._write_output_in_target_folder(output,
'{}RequestBox.swift'.format(self.args.class_name))

Expand All @@ -276,15 +283,22 @@ def _generate_reconnection_helpers(self, api):
if len(background_compatible_pairs) == 0:
return

is_app_auth_client = self.args.auth_type == 'app'
class_name_prefix = 'AppAuth' if is_app_auth_client else ''
class_name = '{}ReconnectionHelpers'.format(class_name_prefix)
return_type = '{}RequestBox'.format(self.args.class_name)

Check warning on line 289 in stone/backends/swift_client.py

View check run for this annotation

Codecov / codecov/patch

stone/backends/swift_client.py#L286-L289

Added lines #L286 - L289 were not covered by tests

template = self._jinja_template("SwiftReconnectionHelpers.jinja")
template.globals['fmt_func'] = fmt_func
template.globals['fmt_class'] = fmt_class
template.globals['class_name'] = class_name
template.globals['return_type'] = return_type

Check warning on line 295 in stone/backends/swift_client.py

View check run for this annotation

Codecov / codecov/patch

stone/backends/swift_client.py#L294-L295

Added lines #L294 - L295 were not covered by tests

output_from_parsed_template = template.render(
background_compatible_namespace_route_pairs=background_compatible_pairs
)
self._write_output_in_target_folder(output_from_parsed_template,
'ReconnectionHelpers.swift')

self._write_output_in_target_folder(output_from_parsed_template, '{}.swift'.format(class_name))

Check warning on line 301 in stone/backends/swift_client.py

View check run for this annotation

Codecov / codecov/patch

stone/backends/swift_client.py#L301

Added line #L301 was not covered by tests

def _background_compatible_routes(self, api):
background_compatible_pairs = self._background_compatible_namespace_route_pairs(api)
Expand Down Expand Up @@ -323,14 +337,26 @@ def _valid_route_for_auth_type(self, route):
# jlocke: this is a bit of a hack to match the route grouping style of the Objective-C SDK
# in app auth situations without blowing up the current user and team auth names

# route_auth_type can be either a string or a list of strings
route_auth_type = route.attrs.get('auth')
client_auth_type = self.args.auth_type
is_app_auth_route = route_auth_type == 'app'

# if building the app client, only include app auth routes
# if building the user or team client, include routes of all auth types except app auth exclusive routes

is_app_auth_only_route = route_auth_type == 'app'
route_auth_types_include_app = 'app' in route_auth_type

Check warning on line 348 in stone/backends/swift_client.py

View check run for this annotation

Codecov / codecov/patch

stone/backends/swift_client.py#L347-L348

Added lines #L347 - L348 were not covered by tests

if client_auth_type == 'app':
return is_app_auth_route
return is_app_auth_only_route or route_auth_types_include_app

Check warning on line 351 in stone/backends/swift_client.py

View check run for this annotation

Codecov / codecov/patch

stone/backends/swift_client.py#L351

Added line #L351 was not covered by tests
else:
return not is_app_auth_route
return not is_app_auth_only_route

Check warning on line 353 in stone/backends/swift_client.py

View check run for this annotation

Codecov / codecov/patch

stone/backends/swift_client.py#L353

Added line #L353 was not covered by tests

def _objc_app_auth_route_wrapper_already_defined(self, route):
client_auth_type = self.args.auth_type
is_app_auth_client = client_auth_type == 'app'

Check warning on line 357 in stone/backends/swift_client.py

View check run for this annotation

Codecov / codecov/patch

stone/backends/swift_client.py#L355-L357

Added lines #L355 - L357 were not covered by tests

return is_app_auth_client and route.attrs.get('auth') != 'app'

Check warning on line 359 in stone/backends/swift_client.py

View check run for this annotation

Codecov / codecov/patch

stone/backends/swift_client.py#L359

Added line #L359 was not covered by tests

def _namespace_contains_valid_routes_for_auth_type(self, namespace):
valid_count = 0
Expand Down Expand Up @@ -539,7 +565,7 @@ def _background_compatible_routes_for_objc_requests(self, api):
objc_class_to_route = {}
for namespace in namespaces:
for route in namespace.routes:
if self._background_session_route_style(route) is not None:
if self._background_session_route_style(route) is not None and self._valid_route_for_auth_type(route):
args_data = self._route_client_args(route)[0]
objc_class = self._fmt_route_objc_class(namespace, route, args_data)
objc_class_to_route[objc_class] = [namespace, route, args_data]
Expand Down
4 changes: 3 additions & 1 deletion stone/backends/swift_rsrc/ObjCRequestBox.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation
import SwiftyDropbox

extension DropboxBaseRequestBox {
extension {{ class_name }} {
var objc: DBXRequest {
switch self {
{% for route_args_data in background_objc_routes %}
Expand All @@ -17,8 +17,10 @@ extension DropboxBaseRequestBox {
case .{{ fmt_func(route.name, route.version) }}(let swift):
return {{ fmt_route_objc_class(namespace, route, args_data) }}(swift: swift)
{% endfor %}
{% if include_default_in_switch %}
default:
fatalError("For Obj-C compatibility, add this route to the Objective-C compatibility module allow-list")
{% endif %}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions stone/backends/swift_rsrc/ObjCRoutes.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public class DBX{{ namespace_name }}Routes: NSObject {
{% for route_args_data in routes_for_objc_requests(namespace) %}
{% set route = route_args_data[0] %}
{% if valid_route_for_auth_type(route) is true %}

{# do not redefine DBXRequests defined by the user auth client #}
{% if not objc_app_auth_route_wrapper_already_defined(route) %}
{% set args_data = route_args_data[1] %}
{% set request_object_name = request_object_name(route, args_data) %}
{% set result_serial_type = fmt_serial_type(route.result_data_type) %}
Expand Down Expand Up @@ -179,5 +182,6 @@ public class {{ fmt_route_objc_class(namespace, route, args_data) }}: NSObject,
}
}

{% endif %}
{% endif %}
{% endfor %}
4 changes: 2 additions & 2 deletions stone/backends/swift_rsrc/SwiftReconnectionHelpers.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import Foundation

enum ReconnectionHelpers {
enum {{ class_name }} {

static func rebuildRequest(apiRequest: ApiRequest, client: DropboxTransportClientInternal) throws -> DropboxBaseRequestBox {
static func rebuildRequest(apiRequest: ApiRequest, client: DropboxTransportClientInternal) throws -> {{ return_type }} {
let info = try persistedRequestInfo(from: apiRequest)

switch info.routeName {
Expand Down

0 comments on commit 01f17c1

Please sign in to comment.