Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Scheduled generated GitHub's REST API Swift code from OpenAPI specification.

License

NotificationsYou must be signed in to change notification settings

wei18/github-rest-api-swift-openapi

Repository files navigation

This Swift code generator is built upon theSwift OpenAPI Generator and leverages the OpenAPI description for GitHub's REST API. The goal is to automate the creation of Swift language code, providing developers with a seamless way to interact with GitHub's REST API.

Usage

For example you can import these frameworks to fetch github users, or see referenceUsersTests.swift.

import GitHubRestAPIUsersimport OpenAPIRuntimeimport OpenAPIURLSessionletclient=Client(serverURL:tryServers.Server1.url(), transport:URLSessionTransport())letusers=tryawait client.usersList().ok.body.json
Full Supported Framworks
import GitHubRestAPIActionsimport GitHubRestAPIActivityimport GitHubRestAPIAppsimport GitHubRestAPIBillingimport GitHubRestAPIChecksimport GitHubRestAPIClassroomimport GitHubRestAPICode_Scanningimport GitHubRestAPICodes_Of_Conductimport GitHubRestAPICodespacesimport GitHubRestAPICopilotimport GitHubRestAPIDependabotimport GitHubRestAPIDependency_Graphimport GitHubRestAPIDesktopimport GitHubRestAPIEmojisimport GitHubRestAPIGistsimport GitHubRestAPIGitimport GitHubRestAPIGitignoreimport GitHubRestAPIInteractionsimport GitHubRestAPIIssuesimport GitHubRestAPILicensesimport GitHubRestAPIMarkdownimport GitHubRestAPIMerge_Queueimport GitHubRestAPIMetaimport GitHubRestAPIMigrationsimport GitHubRestAPIOidcimport GitHubRestAPIOrgsimport GitHubRestAPIPackagesimport GitHubRestAPIProjectsimport GitHubRestAPIPullsimport GitHubRestAPIRate_Limitimport GitHubRestAPIReactionsimport GitHubRestAPIReposimport GitHubRestAPISearchimport GitHubRestAPISecret_Scanningimport GitHubRestAPISecurity_Advisoriesimport GitHubRestAPITeamsimport GitHubRestAPIUsers

Thetutorial show you the following example or refer below.

Example of code for enhanced issues comment API
// Usage.swift// -import Foundationimport GitHubRestAPIIssuesimport OpenAPIRuntimeimport OpenAPIURLSessionimport HTTPTypesstructGitHubRestAPIIssuesExtension{letowner:Stringletrepo:String    /// The issue number or pull number.letnumber:Int    /// Update the comment if the anchor is found; otherwise, create it.func comment(anchor:String, body:String)asyncthrows{lethidingContent="<!-- Comment anchor:\(anchor) -->"letnewBody="\(body)\n\n\(hidingContent)"letclient=Client(            serverURL:tryServers.Server1.url(),            transport:URLSessionTransport(),            middlewares:[AuthenticationMiddleware(token:nil)])letcomments=tryawait client.issuesListComments(            path:.init(owner: owner, repo: repo, issueNumber: number)).ok.body.jsoniflet comment= comments.first(where:{ $0.body?.contains(hidingContent)==true}){            _=tryawait client.issuesUpdateComment(                path:.init(owner: owner, repo: repo, commentId: comment.id),                body:.json(.init(body: newBody)))}else{            _=tryawait client.issuesCreateComment(                path:.init(owner: owner, repo: repo, issueNumber: number),                body:.json(.init(body: newBody)))}}}
Example of code for the `GITHUB_TOKEN` to authenticate.
import Foundationimport GitHubRestAPIUsersimport OpenAPIRuntimeimport OpenAPIURLSessionimport HTTPTypes/// Example: ProcessInfo.processInfo.environment["GITHUB_TOKEN"] ?? ""lettoken:String="***"letclient=Client(    serverURL:tryServers.Server1.url(),    transport:URLSessionTransport(),    middlewares:[AuthenticationMiddleware(token: token)])/// Injects an authorization header to every request.structAuthenticationMiddleware:ClientMiddleware{privatelettoken:Stringinit(token:String){self.token= token}privatevarheader:[String:String]{["Authorization":"Bearer\(token)"]}func intercept(        _ request:HTTPRequest,        body:HTTPBody?,        baseURL:URL,        operationID:String,        next:@Sendable(HTTPRequest, HTTPBody?, URL)asyncthrows->(HTTPResponse,HTTPBody?))asyncthrows->(HTTPResponse,HTTPBody?){varrequest= request        request.headerFields.append(HTTPField(name:.authorization, value:"Bearer\(token)"))returntryawaitnext(request, body, baseURL)}}

Installation

Swift Package Manager

TheSwift Package Manager is a tool for automating the distribution of Swift code and is integrated into theswift compiler.

Once you have your Swift package set up, adding github-rest-api-swift-openapi as a dependency is as easy as adding it to thedependencies value of yourPackage.swift.

// swift-tools-version: 5.9// The swift-tools-version declares the minimum version of Swift required to build this package.dependencies:[.package(url:"https://github.com/wei18/github-rest-api-swift-openapi.git", from:"1.0.0"),]

Overview

OpenAPI serves as a standardized way to document HTTP services. It allows developers to automate workflows, such as generating code for making HTTP requests or implementing API servers.

TheSwift OpenAPI Generator is a Swift package plugin designed to generate code at build-time, ensuring it remains synchronized with the OpenAPI document.

UseSubmodules to clonegithub/rest-api-description and then split openapi tags into multiple modules (Swift Package Products).

Motivation

Wanna use Swift as the development language to create some convenient and user-friendly GitHub Actions.

Contributions

Contributions are welcome! If you encounter issues or have suggestions for improvements, feel free to open an issue or submit a pull request.

This repository is automatically kept up to date with the submodulegithub/rest-api-description.

If you've identified a mismatch between GitHub API's Swift code and these descriptions, or found an issue with the format of a schema,please open an issue to github/rest-api-description oropen an issue to apple/swift-openapi-generator.


[8]ページ先頭

©2009-2025 Movatter.jp