GithubHelp home page GithubHelp logo

loebfly / gitlabswift Goto Github PK

View Code? Open in Web Editor NEW

This project forked from malcommac/gitlabswift

0.0 0.0 0.0 1.75 MB

๐Ÿ›ต Async/Await GitLab REST APIs v4 for Swift

License: MIT License

Swift 100.00%

gitlabswift's Introduction

logo-library

GitLabSwift is an async/await client to perform type-safe, multi-thread Swift call to the GitLab API services.
It's effortless to use; below just a few examples:

// Configure your APIs service connector
let api = GitLab(config: .init(baseURL: "http://...", {
    $0.token = "<YOUR_PERSONAL_TOKEN>" // GitLab PAT
})

// SOME EXAMPLES

// Get your profile
let me: GLModel.User = try await gitlab.users.me()

// Get projects
let response = try await gitlab.projects.list(options: {
    $0.orderBy = .id
    $0.statistics = true
    $0.sort = .desc
})
            
// List some project's commits
let commits =try await gitlab.commits.list(project: anyProjectID, options: {
    $0.since = Date.oneWeekAgo()
    $0.until = Date.now
    $0.includeStats = true
})

Each API is available inside its namespace. Currently, this library supports:

Note Not all API endpoints are supported. I tried to implement the most commonly used. Feel free to contribute by opening a new PR.

Documentation

The usage of the library is pretty simple.
First of all, you need to instantiate a new GLApi service which is the service used to communicate with your own GitLab instance:

let api = GitLab(config: .init(baseURL: "http://...", {
    $0.token = "<YOUR_PERSONAL_TOKEN>"
})

GitLabSwift supports Personal Access Tokens you can create directly from your GitLab instance profile.
Once ready, each context is reachable by calling gitlab.<context>.<api_call>.

Each endpoint is type-safe. When an API supports multiple options, an options callback is used where each parameter is type-safe.
For example:

let response = try await gitlab.milestones.list(project: .id(1097), options: {
    // Each parameter is type-safe
    $0.includeParent = true
    $0.state = .activate
    $0.search = .beginWith("geo")
})

Each request return a generic object called GLResponse.
This object allows you to identify any metadata of the request:

print("There are \(response.totalItems) in \(response.totalPages)"
print("Now showing \(response.countItemsPerPage) items per page")

You can also access the underlying data via response.httpResponse and the original request via response.httpRequest.

Since the result is an async throwing function, if an error occurs, a GLErrors is cached.

Most of the time, you're interested in getting the decoded objects. GitLabSwift uses Codable and each supported GitLab model is exposed via GLModel namespace.
You just need to call response.decode() function to get the actual model instance from the response:

let tags: [GLModel.Tag] = try await gitlab.tags.list(
    project: .id(1097), 
    sort: .asc, 
    search: "release"
).decode()
// You will get an array of `Tags`!
for tag in tags {
    print("- Tag \(tag.name) from commit \(tag.commit.id) created on \(tag.commit.created_at)")
}

Starting from a response, you can easily move to the next pages:

let nextPageResponse = try await response.nextPage() // move forward
let prevPageResponse = try await response.prevPage() // move backward
let next3Pages = try await response.nextPages(3) // get the next 3 pages responses from current page of the response
let allRemainingPages = try await response.nextPages() // get all remaining pages

Note These functions execute multiple async requests and return results at the end. If one of the calls fails, the entire flow fails.

Requirements

GitLabSwift can be installed on any platform which supports:

  • iOS 13+, macOS Catalina+, watchOS 6+, tvOS 13+
  • Xcode 14+
  • Swift 5.7+

Installation

Swift Package Manager

Add it as a dependency in a Swift Package, and add it to your Package. Swift:

dependencies: [
  .package(url: "https://github.com/malcommac/GitLabSwift.git", from: "0.9.0")
]

And add it as a dependency of your target:

targets: [
  .target(name: "MyTarget", dependencies: [
    .product(name: "https://github.com/malcommac/GitLabSwift.git", package: "GitLabSwift")
  ])
]

Author

GitLabSwift is currently owned and maintained by Daniele Margutti.

License

This software is licensed under MIT License.

gitlabswift's People

Contributors

malcommac avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.