Skip to content

Adopt OpenAPI for fetching current swiftly releases #205

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

Merged
merged 3 commits into from
Jan 27, 2025

Conversation

cmcgee1024
Copy link
Member

Use the OpenAPI specification for the swift.org API and the swift OpenAPI generation capabilities to avoid having to hand craft the HTTP handling and local data types.

@cmcgee1024
Copy link
Member Author

@swift-ci test macOS

Use the OpenAPI specification for the swift.org API and the
swift OpenAPI generation capabilities to avoid having to hand
craft the HTTP handling and local data types.
generate:
- types
- client
namingStrategy: idiomatic
Copy link
Member

@czechboy0 czechboy0 Jan 25, 2025

Choose a reason for hiding this comment

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

You can add a filter to only generate the exact operations you're using, to reduce the codegen and build times: https://swiftpackageindex.com/apple/swift-openapi-generator/1.7.0/documentation/swift-openapi-generator/configuring-the-generator#Document-filtering

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, I've applied a filter to the Swiftly portions of the API.

public struct SwiftOrgSwiftlyRelease: Codable {
public var version: SwiftlyVersion
public var platforms: [SwiftOrgSwiftlyPlatform]
}
Copy link
Member

Choose a reason for hiding this comment

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

You could keep these types, remove their Codable conformance, and add a eg init(_ release: Components.Schemas.Release) initialize on each of them that converts from the generated equivalent into this business logic type.

It's good practice to keep the API and business logic types separate, and explicitly convert between them. It then allows the API to keep evolving without it affecting your codebase too much (you'd only need to update the conversion initializer).

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the suggestions. Swiftly doesn't do much with these types so they don't really add much value beyond holding the parts of the message payload that it uses right away. They don't go any further than swiftly itself. I was hoping to reduce the duplication and just use (or maybe extend) the ones generated by the OAG.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, the extensions you added also look good.

let client = Client(
serverURL: URL(string: "https://swift.org/api")!,
transport: AsyncHTTPClientTransport(configuration: config)
)
Copy link
Member

Choose a reason for hiding this comment

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

You can inject a user agent by adding the middlewares parameter on this Client. An example of a header injection middleware that you can copy is here: https://github.com/apple/swift-openapi-generator/blob/main/Examples/auth-client-middleware-example/Sources/AuthenticationClientMiddleware/AuthenticationClientMiddleware.swift

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, this example was what I needed to find the correct imports.

@@ -1,6 +1,6 @@
import Foundation

public struct Error: LocalizedError, CustomStringConvertible {
public struct SwiftlyError: LocalizedError, CustomStringConvertible {
Copy link
Member

Choose a reason for hiding this comment

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

I'd suggest you break out this renaming into a separate PR, it'll then make the OpenAPI changes a lot easier to review.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry for the noise. Thanks for having a look at this.

Add swiftly user agent middleware

Restrict the generator to only the swiftly portion of the API
@cmcgee1024
Copy link
Member Author

@swift-ci test macOS

@cmcgee1024 cmcgee1024 marked this pull request as ready for review January 25, 2025 12:45
let swiftlyUserAgent = SwiftlyUserAgentMiddleware()

let client = Client(
serverURL: URL(string: "https://swift.org/api")!,
Copy link
Member

Choose a reason for hiding this comment

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

I think this either needs to be https://www.swift.org/api/v1 (you're missing /v1), or just use the generated value: serverURL: Servers.Server1.url().

Copy link
Member Author

Choose a reason for hiding this comment

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

Looks like a test gap. I will add an integration test case for this.

Copy link
Member

@czechboy0 czechboy0 left a comment

Choose a reason for hiding this comment

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

Pending the Swift.org URL update, lgtm!

@cmcgee1024
Copy link
Member Author

Pending the Swift.org URL update, lgtm!

Thanks again for your help with this. I think it should be ready for review and merge.

@cmcgee1024
Copy link
Member Author

@swift-ci test macOS

Copy link
Member

@czechboy0 czechboy0 left a comment

Choose a reason for hiding this comment

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

Looks great!

@cmcgee1024
Copy link
Member Author

@swift-ci test macOS

@cmcgee1024 cmcgee1024 merged commit 8fb4bef into swiftlang:main Jan 27, 2025
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants