Code Monkey home page Code Monkey logo

kiturakit's Introduction

Kitura

APIDoc Build Status - Master macOS Linux Apache 2 Slack Status

KituraKit - A Kitura v2 Client Library

Kitura is a lightweight web framework for writing Swift server applications.

KituraKit is a client side framework for sending HTTP requests to a Kitura server. By using the Swift Codable protocol, you can send and receive models directly from client to server.

Swift version

The latest version of KituraKit requires Swift 4.0 or later. You can download this version of the Swift binaries by following this link.

Usage

Cocoapod Installation

  1. Navigate to the root of your project (where your .xcodeproj directory is)

  2. If you don't already have a podfile, run pod init to create a new podfile in your current directory.

  3. Open the Podfile with your preferred text editor and under the "# Pods for 'your_project_name'>" line add:

pod 'KituraKit'
  1. Install KituraKit by running the command: pod install

  2. As well as installing KituraKit, the pod install also creates an Xcode workspace which contains all of your installed pods. So you'll need to open the .xcworkspace file (not .xcodeproj) to have access to those pods. This is the default behaviour of Cocoapods.

SPM Installation

We expect users on the client side to use the Cocoapod installation, however, if you require access to KituraKit from the server you can use Swift Package Manager.

Add dependencies

Add the KituraKit package to the dependencies within your application’s Package.swift file. Substitute "x.x.x" with the latest KituraKit release.

.package(url: "https://github.com/Kitura/KituraKit.git", from: "x.x.x")

Add KituraKit to your target's dependencies:

.target(name: "example", dependencies: ["KituraKit"]),

Import package

import KituraKit

Examples

To run through a FoodTracker tutorial which covers various components of Kitura, including KituraKit, click here

To try out the sample iOS project for yourself, making use of KituraKit, click here.

API Documentation

KituraKit

The KituraKit class handles the connection to your Kitura server and executes the REST requests.

You create a KituraKit instance by providing the URL of your Kitura server:

if let client = KituraKit(baseURL: "http://localhost:8080") {
    // Use client to make requests here
}

Codable Models

Kitura and KituraKit send and receive instances of Swift types directly. These types (aka models) can be shared between the client and server.

The only requirement for a model is that it conforms to the Codable protocol:

public struct User: Codable {
    public let name: String
    public init(name: String) {
        self.name = name
    }
}

HTTP Requests

The signatures for HTTP requests in KituraKit mirror the Codable routes in Kitura. We will demonstrate what the code for this looks like in the following examples.

If you had the following GET route on your server:

// Kitura server route
router.get("/users") { (completion: ([User]?, RequestError?) -> Void) in
    let users = [User(name: "Joe"), User(name: "Bloggs")]
    completion(users, nil)
}

You would make a request to it using the get function on your KituraKit client:

// KituraKit client request
client.get("/users") { (users: [User]?, error: RequestError?) -> Void in
    if let users = users {
        // GET successful, work with returned users here
    }
}

Similarly, to make a request to a Kitura POST route:

// Kitura server route
router.post("/users") { (user: User, completion: (User?, RequestError?) -> Void) in
    completion(user, nil)
}

You would make a request to it using the post function on your KituraKit client:

// KituraKit client request
let newUser = User(name: "Kitura")
client.post("/users", data: newUser) { (user: User?, error: RequestError?) -> Void in
    if let user = user {
        // POST successful, work with returned users here
    }
}

KituraKit supports the following REST requests:

  • GET a Codable object.
  • GET a Codable object using an identifier.
  • GET a Codable object using query parameters.
  • POST a Codable object.
  • POST a Codable object and be returned an identifier.
  • PUT a Codable object using an identifier.
  • PATCH a Codable object using an identifier.
  • DELETE using an identifier.
  • DELETE without an identifier.

Authentication

The Kitura server can authenticate users using the Credentials repository. KituraKit allows you to provide credentials alongside your request to identify yourself to the server.

Note: When sending credentials you should always use HTTPS to avoid sending passwords/tokens as plaintext.

You can set default credentials for your client which will be attached to all requests. If your server is using Kitura-CredentialsHTTP for basic authentication, you would provide the username and password as follows:

if let client = KituraKit(baseURL: "https://localhost:8080") {
    client.defaultCredentials = HTTPBasic(username: "John", password: "12345")
}

Alternatively, you can provide the credentials directly on the request:

let credentials = HTTPBasic(username: "Mary", password: "abcde")
client.get("/protected", credentials: credentials) { (users: [User]?, error: RequestError?) -> Void in
    if let users = users {
        // work with users
    }
}

KituraKit supports client side authentication for the following plugins:

For more information visit our API reference.

Community

We love to talk server-side Swift and Kitura. Join our Slack to meet the team!

License

This library is licensed under Apache 2.0. Full license text is available in LICENSE.

kiturakit's People

Contributors

andrew-lees11 avatar dannys42 avatar djones6 avatar ianpartridge avatar kyemaloy97 avatar rolivieri avatar sandmman avatar seabaylea avatar shihabmehboob avatar tunniclm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kiturakit's Issues

Uncomment linux test once foundation bug resolved

We have had to comment out some tests for Linux with Swift 5. They are failing due to a bug in foundation where URLSession is calling back twice. It has been raised and fixed in this pr.
Once this is brought into Swift 5.0 we should uncomment these tests to confirm the issue is solved.

Support JWT authentication

Question:

Kitura and KituraKit: Is there a way to use JSON Web Token (JWT) when you use KituraKit to send
HTTP requests to a Kitura server?

Answer from @djones6:

As far as I'm aware, KituraKit doesn't support JWT authentication yet, but it does have the ability to send other bearer token types.

I think it should be possible to add your own support for authenticating with a JWT by replicating the code for the other token types. If you get this working, you'd be welcome to submit a pull request!

You'd need some additional code though if you wanted to support a login route that issued a token. @andrew Lees wrote a great blog post that covers that topic: https://developer.ibm.com/swift/2018/12/18/swift-jwt-3-0-codable-json-web-tokens/

Support custom encoders/decoders

Context & Description

In Kitura 2.5 we added functionality for custom encoders/decoders. This allows users to set their own encoders/decoders for codable routes.
We would like to mimic the functionality in KituraKit

Clear & Actionable Tasks

  • Add test for KituraKit for expected behavior
  • Add documentation describing new behavior
  • Raise PR with changes
  • Have pr reviewed
  • merge and tag the Change

Publish Cocoapod and dependancies

Currently the cocoapod for KituraKit is created by a script to generate a pod branch and then users have to point at the git branch.

It would be easier to use and maintain if we published KituraKit and it's dependancies to the cocoapods library.

Make extensions of public structs public

I wanted to add a header-based authentication, so I just made an extension to the client, adding the headers when needed. But RequestError extensions aren't public, meaning I can't write a func that would use RequestError(restError: restError)

Making extensions public for public structs should be the default, being private/protected only when the situation recommends it

Release v1.0

  • underlyingError fixes
  • update podspec to SwiftyRequest v3 / Contracts v1.2 -> #56
  • tag 1.0
  • publish pod

Kitura Extra argument 'query' in call

Context and Description

I'm a student developping an iOS application with Kitura https://github.com/charelF/Radar
I am getting a strange error which seems to be a bug from Kitura, at least I can not possibly understand what I am doing wrong. I implemented the client.get method and it throws an error when I include the query parameter.

Screenshot
grafik
Code: https://github.com/charelF/Radar/blob/be0f0d3f517338370559ba15e466ff7b3bfe85af/Radar/Model/DataBase.swift#L77

Environment Details

I am using Xcode 11.1, macOS catalina, swift 5.1

Steps to Reproduce

Nothing particular, as you can see Xcode detects the line as not being correct even though it should be correct.

I initially posted this issue on the Kitura Repo, but it seems to fit better here

CI failure with Swift 5

#42 aims to add Swift 5 testing to the CI, but one of the tests fails on Linux with:

/home/travis/build/IBM-Swift/KituraKit/Tests/KituraKitTests/BasicAuthTests.swift:90: error: BasicAuthTests.testBasicAuthUnauthorized : XCTAssertEqual failed: ("601 : A connection error occurred, cannot connect to the server. Please ensure that the server is started and running, with the correct port and URL ('ToDoServer' if using the sample app).") is not equal to ("401 : Unauthorized") -
Fatal error: Trying to remove task, but it's not in the registry.: file /home/buildnode/jenkins/workspace/oss-swift-5.0-package-linux-ubuntu-16_04/swift-corelibs-foundation/Foundation/URLSession/TaskRegistry.swift, line 76
/home/travis/build/IBM-Swift/KituraKit/Tests/KituraKitTests/BasicAuthTests.swift:91: error: BasicAuthTests.testBasicAuthUnauthorized : API violation - multiple calls made to XCTestExpectation.fulfill() for A response is received from the server -> .unauthorized.
Exited with signal code 4

This needs investigation.

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.