Giter Site home page Giter Site logo

servicestack / servicestack.swift Goto Github PK

View Code? Open in Web Editor NEW
9.0 8.0 9.0 12.07 MB

Swift support used in ServiceStack

License: Other

Swift 99.47% Ruby 0.38% Objective-C 0.15%
servicestack swift servicestack-xcode-plugin servicestack-reference typed generic service-client

servicestack.swift's Introduction

Follow @ServiceStack or join the Google+ Community for updates, or StackOverflow or the Customer Forums for support.

ServiceStack.Swift

See Swift Add ServiceStack Reference for an overview of the Swift Support in ServiceStack.

ServiceStack's Add ServiceStack Reference feature lets iOS/macOS developers easily generate an native typed Swift API for your ServiceStack Services using the x dotnet command-line tool.

Simple command-line utils for ServiceStack

The x dotnet tool provides a simple command-line UX to easily Add and Update Swift ServiceStack References.

Prerequisites: Install .NET Core.

$ dotnet tool install --global x 

This will make the x dotnet tool available in your $PATH which can now be used from within a Terminal window at your Xcode project folder.

To use the latest JsonServiceClient you'll need to add a reference to ServiceStack Swift library using your preferred package manager:

Xcode

From Xcode 12 the Swift Package Manager is built into Xcode.

Go to File > Swift Packages > Add Package Dependency:

Add a reference to the ServiceStack.Swift GitHub repo:

https://github.com/ServiceStack/ServiceStack.Swift

After adding the dependency both ServiceStack.Swift and its PromiseKit dependency will be added to your project:

SwiftPM

dependencies: [
    .package(name: "ServiceStack", url: "https://github.com/ServiceStack/ServiceStack.Swift.git", 
        Version(5,0,0)..<Version(6,0,0)),
],

CocoaPods

In your Podfile:

use_frameworks!

# Pods for Project
pod "ServiceStack", '~> 5.0'

Carthage

github "ServiceStack/ServiceStack.Swift" ~> 5.0

v5.0.0 Release

The latest v5 support for ServiceStack.Swift has been rewritten to use Swift 5 and DTOs generated using Swift's new Codable available in ServiceStack from v5.10.5+.

Previous Version

To use a JsonServiceStack with DTOs generated earlier ServiceStack versions you'll need to reference the older 1.x client version instead:

dependencies: [
    .package(name: "ServiceStack", url: "https://github.com/ServiceStack/ServiceStack.Swift.git", 
        Version(1,0,0)..<Version(2,0,0)),
],

Add a new ServiceStack Reference

To Add a new ServiceStack Reference, call x swift with the Base URL to a remote ServiceStack instance:

x swift {BaseUrl}
x swift {BaseUrl} {FileName}

Where if no FileName is provided, it first uses dtos.swift or if it exists the filename is inferred from the host name of the remote URL, e.g:

x swift https://techstacks.io

Downloads the Typed Swift DTOs for techstacks.io and saves them to dtos.swift.

Alternatively you can have it saved to a different FileName with:

x swift https://techstacks.io TechStacks

Which instead saves the DTOs to TechStacks.dtos.swift.

x swift also downloads ServiceStack's Swift Client and saves it to JsonServiceClient.swift which together with the Server DTOs contains all the dependencies required to consume Typed Web Services in Swift.

Update an existing ServiceStack Reference

The easiest way to update all your Swift Server DTOs is to just call x swift without any arguments:

x swift

This will go through and update all your *.dtos.swift Service References.

To Update a specific ServiceStack Reference, call x swift with the Filename:

x swift {FileName.dtos.swift}

As an example, you can Update the Server DTOs added in the previous command with:

x swift TechStacks.dtos.swift

Which also includes any Customization Options that were manually added.

Optional DTO Customizations

Refer to Swift Add ServiceStack Reference docs for info on additional customizations available.

Swift Apps using ServiceStack.Swift

AutoQuery Viewer is a native iPad App that provides an automatic UI for browsing, inspecting and querying any publicly accessible ServiceStack AutoQuery Service from an iPad.

AutoQuery Viewer on AppStore

The TechStacks Native iOS App provides a fluid and responsive experience for browsing https://techstacks.io content on iPhones and iPad devices. It takes advantage of the ease-of-use and utility of ServiceStack's new support for Swift and XCode for quickly building services-rich iOS Apps. Get it now free on the AppStore!

TechStacks on AppStore

Features

TechStacks OSX Desktop App is built around 2 AutoQuery Services showing how much querying functionality AutoQuery Services provides for free and how easy they are to call with ServiceStack's new support for Swift and XCode.

TechStack Desktop Search Fields

servicestack.swift's People

Contributors

alfogrillo avatar gistlyn avatar gruppio avatar layoric avatar mythz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

servicestack.swift's Issues

Redundant conformance of 'response' to protocol 'JsonSerializable'

I have many errors in my dtos.swift, how:

Type '[String]' does not conform to protocol 'JsonSerializable'
or

// @Route("/banner", "GET")
public class BannerAllRequest : IReturn
{
    public typealias Return = BannerAllResponse

    required public init(){}
}



public class BannerAllResponse : RespuestaGenerica
{
    required public init(){}
    public var bannerLista:[BannerResponse] = []
}



extension BannerAllResponse : JsonSerializable  /* ERROR: Redundant conformance of 'BannerAllResponse' to protocol 'JsonSerializable' */
{
    public static var typeName:String { return "BannerAllResponse" } /* ERROR: Overriding non-@objc declarations from extensions is not supported */
    public static var metadata = Metadata.create([ . /* ERROR: Cannot override with a stored property 'metadata' */
            Type<BannerAllResponse>.arrayProperty("bannerLista", get: { $0.bannerLista }, set: { $0.bannerLista = $1 }),
            Type<BannerAllResponse>.optionalProperty("responseStatus", get: { $0.responseStatus }, set: { $0.responseStatus = $1 }),
            Type<BannerAllResponse>.optionalProperty("codigoRespuesta", get: { $0.codigoRespuesta }, set: { $0.codigoRespuesta = $1 }),
            Type<BannerAllResponse>.optionalProperty("result", get: { $0.result }, set: { $0.result = $1 }),
        ])
}

and in the JsonServiceClient.swift

line 284: pendingPromise.resolver.reject(self.handleError(nsError: error! as NSError)) /* ERROR: Cannot convert value of type 'NSError' to expected argument type 'Error'*/

line 290: pendingPromise.resolver.reject(self.fireErrorCallbacks(error: resposneError!)) /* ERROR: Cannot convert value of type 'NSError' to expected argument type 'Error'*/

line 538: pendingPromise.resolver.reject(self.handleError(nsError: error! as NSError)) /* ERROR: Cannot convert value of type 'NSError' to expected argument type 'Error'*/

line 552: public typealias URLSessionDataTaskSync = (task: URLSessionDataTask, callback: URLSessionDataCallback?) /* ERROR: 'URLSessionDataCallback' is ambiguous for type lookup in this context */

line 555: var callback: URLSessionDataCallback? /* ERROR: 'URLSessionDataCallback' is ambiguous for type lookup in this context */

line 819: extension Data : JsonSerializable /* ERROR: Redundant conformance of 'Data' to protocol 'JsonSerializable' */

line 823: public static var typeName:String { return "Data" } /* ERROR: Invalid redeclaration of 'typeName' */

line 825: public static var metadata:Metadata = Metadata.create([]) /* ERROR: Invalid redeclaration of 'metadata' */

help me, please.

Reference to generic type 'Data' requires arguments in <...>

Hi, I solved the first problem but I continue have this in JsonServiceClient.swift:

LINE 73 : Reference to generic type 'Data' requires arguments in <...>

and all the code that use Data have the same error

My dtos presents this problem:

Type '[String]' does not conform to protocol 'JsonSerializable'

Type 'Bool' does not conform to protocol 'JsonSerializable'

Type '[UsuarioGet]' does not conform to protocol 'JsonSerializable'

Originally posted by @ruben44bac in #13 (comment)

Enum case conversion

Hi, I have a request to improve Swift DTO code generation for enums. At this point .NET enums being UpperCase are converted also in UpperCase.
From swift Swift Proposal 006 enum cases are transformed to LowerCase.
It is possible to specify params for output: camelCase or lowercase?
For Example:

// C# input
enum EnumCamelCase {
    case First
    case Second
    ...
    case SomeCoolCase
    case OtherNotCoolCase
}

 // Swift output
enum EnumCamelCase {
    case first
    case second
    ...
    case someCoolCase
    case otherNotCoolCase
}
// C# input
enum EnumLowerCase {
    case ONE
    case TWO
}

 // Swift output
enum EnumLowerCase {
    case one
    case two
}

Thanks

Xcode 7 Issue

The Xcode 7 ServiceStackXcode.dmg has 3 issues:

  1. The generated DTO class extensions don't implement method of HasMetadata: public static var metadata (still HasReflect methods)
  2. Xcode 7 gives error for the line of "typealias Return = XXX" for each Request class. Need to add public access modifier.
  3. JsonServiceClient.swift includes ResponseStatus, ResponseError and ErrorResponse classes, which are also included in the auto-generated DTO file.

Unable to deserialize array of objects

I'm having an issue deserializing a request that return a list of object (e.g.: IReturn<SomeClass>). I know that this is not supported and that @mythz do not want to automatically generate wrappers to fix this issue (he has good arguments in the thread).

Therefore I am proposing adding an extension point to the Swift client where the developers will be able to handle the deserialization themselves if the normal deserialization was not able to handle the returned object. For this to work, ServiceStack.Swift would need to provide a new protocol (CanPopulateFromJsonObject) and developers could implement this protocol to handle their needs.

Let me know what you think.

Protocol:

protocol CanPopulateFromJsonObject // feel free to change the name
{
    func fromJsonObject(any: AnyObject?) -> Bool
}

_Changes to fromJson:_

        static func fromJson<T : JsonSerializable>(instance:T, json:String) -> T? {
        if instance is NSString || instance is String {
            if let value = json as? T {
                return value
            }
        }

        let jsonObject = parseJson(json)
        if (jsonObject != nil) {
            if let map = jsonObject as? NSDictionary {
                return populate(instance, map: map, propertiesMap: T.propertyMap)
            }

            if let populator = instance as? CanPopulateFromJsonObject {
                if (populator.fromJsonObject(jsonObject)) {
                    return instance
                }
            }
        }

        return nil
    }

_Sample usage:_
This has do be done by the developer for each class that he wants to support.


public class ArrayOfSomeClass : CanPopulateFromJsonObject, JsonSerializable
{
    public required init() {}
    public static var typeName:String { return "ArrayOfSomeClass" }
    public static var metadata = Metadata.create([])
    public var values = [SomeClass]()

    public func fromArray(array:NSArray)
    {
        for(item) in array {
            if let obj = SomeClass.fromObject(item) {
                values.append(obj)
            }
        }
    }
    public func fromJsonObject(any: AnyObject?) -> Bool {
        if let array = any as? NSArray {
            fromArray(array)
            return true
        }
        return false
    }

    // I didn't implement these methods for this sample
    public static func fromObject(any:AnyObject) -> ArrayOfSomeClass? { return nil }
    public func toJson() -> String { return "" }
    public func toString() -> String { return toJson() }
    public static func fromString(string:String) -> ArrayOfSomeClass? { return nil }
}

public class SomeClass
{
    public required init() {}
    var someValue: String?
}

extension SomeClass : JsonSerializable
{
    public static var typeName:String { return "SomeClass" }
    public static var metadata = Metadata.create([
        Type<SomeClass>.optionalProperty("someValue", get: { $0.someValue }, set: { $0.someValue = $1 }),
        ])

    public typealias T = SomeClass
    public static func fromObject(any:AnyObject) -> SomeClass? {
        if let map = any as? NSDictionary {
            return populate(SomeClass(), map: map, propertiesMap: SomeClass.propertyMap)
        }
        return nil
    }
}

public class MyRequest : IReturn
{
    public typealias Return = ArrayOfSomeClass
    required public init(){}
}

// Testing
var jsonString = "[{ \"someValue\": \"a\"}, { \"someValue\": \"b\"}]"
var obj = Type<ArrayOfSomeClass>.fromString(ArrayOfSomeClass(), string: jsonString)

print("items:")
for(item) in obj!.values {
    print(item.someValue)
}

Wrong date format for GET requests

This is an issue similar to this Java issue. Date values are not serialized correctly when they are used in the URL. Other data types (e.g.: TimeSpan) may be affected as well.

/json/reply/MyGetRequest?date=2015-11-11 14:42:03

This is the local date instead of the UTC date so when ServiceStack 'reads', it converts it to UTC which could cause issue if the client and the server are not in the same timezone. For testing purposes, I changed the verb to POST and this was the date format in the body of the request: 2015-11-11T19:45:54.092Z

    public class MyGetRequest : IReturn<Token>
    {
        public DateTime Date { get; set; }
    }

Exception when response is nil

When the response is nil, line 267 throw e and then line 277 catch var ex as NSError but the response is nil now, so the line 278 self.handleResponse in catch block will crash the app on response!:

fatal error: unexpectedly found nil while unwrapping an Optional value

image

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.