Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authv2 / Networking improvements #1168

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

federicocappelli
Copy link
Member

@federicocappelli federicocappelli commented Jan 17, 2025

Task/Issue URL: https://app.asana.com/0/1205842942115003/1209170372758735/f
iOS PR: duckduckgo/iOS#3820
macOS PR: duckduckgo/macos-browser#3746
What kind of version bump will this require?: Major (APIRequestV2 init can now return nil, TestUtils library has been replaced)

CC: @miasma13

Description:

Networking v2 Improvements

  • Introduction of RetryPolicy
  • Authentication support improved with refresh callback in case of 401 error
  • Equatable conformance
  • Hashable Conformance
  • APIRequestV2Error Equatable conformance
  • New Oauth framework inside Networking v2
  • Malicious site protection APIClient updated to Networking v2
  • A lot of utilities, moks and improvements added to NetworkingTestingUtils

Additional changes include:

  • DecodableHelper expanded and renamed CodableHelper
  • Date extension with utilities + unit tests
  • Rationalisation of the package "Testing Utils" modules, now every library has its XYZTestingUtils module, I removed the generic TestingUtils

Steps to test this PR:

  • malicious site protection API works as expected

Internal references:

Software Engineering Expectations
Technical Design Template

@federicocappelli federicocappelli requested review from a team, studiosutara and alessandroboron and removed request for a team and studiosutara January 17, 2025 13:28
import Foundation
import Common

extension Dictionary where Key == String, Value == String {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we make it a Collection extension defaulting to KeyValuePairs<String, String> – this will preserve items order

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion thanks, I agree that maintaining the order is a nice to have, I tried to change it in
public typealias QueryItems = any Collection<KeyValuePairs<String, String>>
But that introduces a lot of issues (like can't access values directly by key), then I decided to stuck with

public typealias QueryItem = Dictionary<String, String>.Element
public typealias QueryItems = Array<QueryItem>

Less elegant but achieves the objective of having a data structure of ordered key value pairs

I moved everything to QueryItems.swift

@@ -107,7 +107,9 @@ struct APIClient {
let url = environment.url(for: requestType)
let timeout = environment.timeout(for: requestType) ?? requestConfig.defaultTimeout ?? 60

let apiRequest = APIRequestV2(url: url, method: .get, headers: headers, timeoutInterval: timeout)
guard let apiRequest = APIRequestV2(url: url, method: .get, headers: headers, timeoutInterval: timeout) else {
throw APIRequestV2.Error.invalidDataType
Copy link
Collaborator

@mallexxx mallexxx Jan 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should crash in case we can‘t form a request as it would indicate a critical code flaw. Or at least assert if there are valid cases when a request cannot be created from a url (which I believe there‘s no).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that it isn't realistically possible to create a invalid request via an invalid URL, but... URLComponents.init result is optional so I'm not keen to force unwrap anything. I haven't yet discovered a way to make URLComponents.init fail but I can't demonstrate that doesn't exist.
I've added an assert, but crashing in production is not an option.

}
}

public typealias DecodableHelper = CodableHelper
Copy link
Member Author

@federicocappelli federicocappelli Jan 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expanded the previous DecodableHelper into CodableHelper withe helpers for encoding and decoding.
This typealias help avoid merge conflicts


import Foundation

/// A convenience enum to unify the logic for selecting the right keychain through the query attributes.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this from mac

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants