aries 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/README.md +102 -0
- data/Rakefile +1 -0
- data/aries.gemspec +31 -0
- data/bin/aries +7 -0
- data/examples/clients/RackJsonSchemaApi.swift +277 -0
- data/examples/schemas/rack-json_schema.json +148 -0
- data/examples/schemas/schema-heroku.json +7095 -0
- data/lib/aries.rb +28 -0
- data/lib/aries/cli.rb +19 -0
- data/lib/aries/error.rb +3 -0
- data/lib/aries/generator.rb +94 -0
- data/lib/aries/link.rb +105 -0
- data/lib/aries/param.rb +75 -0
- data/lib/aries/param_type.rb +18 -0
- data/lib/aries/presenters/link_swift.rb +55 -0
- data/lib/aries/presenters/param_swift.rb +41 -0
- data/lib/aries/presenters/param_type_swift.rb +33 -0
- data/lib/aries/presenters/resource_swift.rb +32 -0
- data/lib/aries/presenters/schema_swift.rb +30 -0
- data/lib/aries/resource.rb +35 -0
- data/lib/aries/schema.rb +56 -0
- data/lib/aries/templates/client.swift.erb +217 -0
- data/lib/aries/version.rb +3 -0
- metadata +201 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a721b5050b0c0d62fac4c082a6999db9b0b7fd32
|
4
|
+
data.tar.gz: 83c31100b9aa075a09bad91bfa6215edf82fd5b6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: abbab34967cf7e96b9552886c2f777aeffefd67eca7295e180f871fc468065649a91faf586f58ff56325c5f10962e8ee48d055861b821f320242ceb67d5885aa
|
7
|
+
data.tar.gz: c7fa32042565cc17e52379d51d5c53d7497d228cf7781ef0872d80b3278635dabc6764c0ac320189f969c19abbcb16fb1d527a96659d0e9e5e62ed4b525a1cf2
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# Aries
|
2
|
+
|
3
|
+
Aries is a iOS(Swift), Andorid(not yet, but future) HTTP client generator for APIs represented with JSON Schema
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'aries'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install aries
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Generate a client
|
24
|
+
|
25
|
+
Aries generates an HTTP client from a JSON schema that describes your API. Look at prmd for tooling to help write a JSON schema. When you have a JSON schema prepared you can generate a client for your API
|
26
|
+
|
27
|
+
$ bin/aries generate json-schema.json
|
28
|
+
|
29
|
+
### Custom Configuration
|
30
|
+
|
31
|
+
Aries can setup costom config for api client class name, base url, output path.
|
32
|
+
|
33
|
+
$ bin/aries generate json-schema.json --name MyApi \
|
34
|
+
--url http://api.myapp.com \
|
35
|
+
--output /path/to/dir
|
36
|
+
|
37
|
+
### Swift(v1,1)
|
38
|
+
|
39
|
+
Dependent on [SwiftTask](https://github.com/ReactKit/SwiftTask), [Alamofire](https://github.com/Alamofire/Alamofire), [URITemplate](https://github.com/kylef/URITemplate.swift).
|
40
|
+
Aries api client should be used with these libraries, and works like promise style.
|
41
|
+
|
42
|
+
Aries api client behaves use the following.
|
43
|
+
|
44
|
+
```
|
45
|
+
|
46
|
+
Api.TodoItem.Read().success { (value: AnyObject) -> Void in
|
47
|
+
let items = value as [[String:AnyObject]]
|
48
|
+
println(items)
|
49
|
+
}.failure { (error, isCancelled) -> Void in
|
50
|
+
return
|
51
|
+
}
|
52
|
+
|
53
|
+
```
|
54
|
+
|
55
|
+
Aries has Custom Delegation for request, response for authenticatin, custom header, etc.
|
56
|
+
|
57
|
+
```
|
58
|
+
|
59
|
+
@UIApplicationMain
|
60
|
+
class AppDelegate: UIResponder, UIApplicationDelegate, AriesApiDelegate {
|
61
|
+
|
62
|
+
self.token: String? = nil
|
63
|
+
|
64
|
+
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
|
65
|
+
AriesApi.delegate = self
|
66
|
+
return true
|
67
|
+
}
|
68
|
+
|
69
|
+
func onFailure(err: NSError) -> NSError {
|
70
|
+
debugPrintln(err)
|
71
|
+
return err
|
72
|
+
}
|
73
|
+
|
74
|
+
func onSuccess(response: AnyObject) -> AnyObject {
|
75
|
+
debugPrintln(response)
|
76
|
+
return response
|
77
|
+
}
|
78
|
+
|
79
|
+
func beforeRequest(request: NSMutableURLRequest) -> NSMutableURLRequest {
|
80
|
+
debugPrintln(request)
|
81
|
+
request.setValue("Bearer \(self.token)", forHTTPHeaderField: "Authorization")
|
82
|
+
return request
|
83
|
+
}
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
```
|
88
|
+
|
89
|
+
|
90
|
+
## Development
|
91
|
+
|
92
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
93
|
+
|
94
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
95
|
+
|
96
|
+
## Contributing
|
97
|
+
|
98
|
+
1. Fork it ( https://github.com/[my-github-username]/aries/fork )
|
99
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
100
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
101
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
102
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/aries.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'aries/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "aries"
|
8
|
+
spec.version = Aries::VERSION
|
9
|
+
spec.authors = ["yss44"]
|
10
|
+
spec.email = ["nya060@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Aries is a iOS(Swift), Android(java,not yet) API Client generator represented with JSON Schema}
|
13
|
+
spec.description = %q{Aries is a iOS(Swift), Andorid(not yet, but future) HTTP client generator for APIs represented with JSON Schema}
|
14
|
+
spec.homepage = "https://github.com/yss44/aries"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "bin"
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "addressable"
|
23
|
+
spec.add_dependency "thor"
|
24
|
+
spec.add_dependency "activesupport"
|
25
|
+
spec.add_dependency "json_schema"
|
26
|
+
spec.add_dependency "erubis"
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.8"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
29
|
+
spec.add_development_dependency "pry"
|
30
|
+
spec.add_development_dependency "rack-json_schema"
|
31
|
+
end
|
data/bin/aries
ADDED
@@ -0,0 +1,277 @@
|
|
1
|
+
//
|
2
|
+
// RackJsonSchemaApi.swift
|
3
|
+
//
|
4
|
+
// Auto generated by Aries on 2015/04/11.
|
5
|
+
// Do not modify this file directory
|
6
|
+
//
|
7
|
+
|
8
|
+
import Alamofire
|
9
|
+
import SwiftTask
|
10
|
+
import URITemplate
|
11
|
+
|
12
|
+
typealias RackJsonSchemaApiResponse = Task<Int,AnyObject,NSError>
|
13
|
+
|
14
|
+
protocol RackJsonSchemaApiDelegate {
|
15
|
+
func customReqeust(request: NSMutableURLRequest) -> NSMutableURLRequest
|
16
|
+
func onDefaultSuccess(response: AnyObject) -> AnyObject
|
17
|
+
func onDefaultFailure(err: NSError) -> NSError
|
18
|
+
}
|
19
|
+
|
20
|
+
class RackJsonSchemaApi {
|
21
|
+
|
22
|
+
private struct BaseUrlStringStruct{ static var url = "http://localhost:5000" }
|
23
|
+
class var BaseUrlString: String {
|
24
|
+
get{ return BaseUrlStringStruct.url }
|
25
|
+
set{ BaseUrlStringStruct.url = newValue }
|
26
|
+
}
|
27
|
+
|
28
|
+
// workaround tips
|
29
|
+
// After updated swift 1.2, use ( static var someVariable: Int = 0 ) instead
|
30
|
+
private struct delegateStruct{ static var detegator: RackJsonSchemaApiDelegate? }
|
31
|
+
class var delegate: RackJsonSchemaApiDelegate? {
|
32
|
+
get{ return delegateStruct.detegator }
|
33
|
+
set{ delegateStruct.detegator = newValue }
|
34
|
+
}
|
35
|
+
|
36
|
+
private class func defaultURLRequest(let path: String, let method: Alamofire.Method) -> NSMutableURLRequest {
|
37
|
+
let URL = NSURL(string: RackJsonSchemaApi.BaseUrlString)!
|
38
|
+
let mutableURLRequest = NSMutableURLRequest(URL: URL.URLByAppendingPathComponent(path))
|
39
|
+
|
40
|
+
mutableURLRequest.HTTPMethod = method.rawValue
|
41
|
+
mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
42
|
+
mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Accept")
|
43
|
+
|
44
|
+
if let delegate = self.delegate?{
|
45
|
+
return delegate.customReqeust(mutableURLRequest)
|
46
|
+
}
|
47
|
+
|
48
|
+
return mutableURLRequest
|
49
|
+
}
|
50
|
+
|
51
|
+
private class func generateURI(template: String, params: [String: AnyObject]?) -> String {
|
52
|
+
if let params = params {
|
53
|
+
return URITemplate(template: template).expand(params)
|
54
|
+
}
|
55
|
+
return template
|
56
|
+
}
|
57
|
+
|
58
|
+
class Base {
|
59
|
+
|
60
|
+
private class func request(URLRequest: URLRequestConvertible) -> RackJsonSchemaApiResponse {
|
61
|
+
return RackJsonSchemaApiResponse {(progress, fulfill, reject, configure) in
|
62
|
+
let alam = Alamofire.request(URLRequest)
|
63
|
+
.validate()
|
64
|
+
.responseJSON({ (req, res, data, err) -> Void in
|
65
|
+
|
66
|
+
if let err = err {
|
67
|
+
reject(self.buildErr(res, data: data, err: err))
|
68
|
+
return
|
69
|
+
}
|
70
|
+
fulfill(data!)
|
71
|
+
})
|
72
|
+
debugPrintln(alam)
|
73
|
+
configure.pause = { [weak alam] in if let alam = alam { alam.suspend() } }
|
74
|
+
configure.resume = { [weak alam] in if let alam = alam { alam.resume() } }
|
75
|
+
configure.cancel = { [weak alam] in if let alam = alam { alam.cancel() } }
|
76
|
+
}.then({ (data, errorInfo) -> RackJsonSchemaApiResponse in
|
77
|
+
return RackJsonSchemaApiResponse { (progress, fulfill, reject, configure) in
|
78
|
+
if(errorInfo == nil){
|
79
|
+
if let delegate = RackJsonSchemaApi.delegate {
|
80
|
+
let newData: AnyObject = delegate.onDefaultSuccess(data!)
|
81
|
+
fulfill(newData)
|
82
|
+
return
|
83
|
+
}
|
84
|
+
fulfill(data!)
|
85
|
+
return
|
86
|
+
}else{
|
87
|
+
let (err, isCancelled) = errorInfo!
|
88
|
+
if let err = err {
|
89
|
+
if let delegate = RackJsonSchemaApi.delegate{
|
90
|
+
let newErr = delegate.onDefaultFailure(err)
|
91
|
+
reject(newErr)
|
92
|
+
return
|
93
|
+
}
|
94
|
+
reject(err)
|
95
|
+
return
|
96
|
+
}
|
97
|
+
}
|
98
|
+
}
|
99
|
+
|
100
|
+
})
|
101
|
+
}
|
102
|
+
|
103
|
+
private class func buildErr(res: NSHTTPURLResponse?, data: AnyObject?, err: NSError?) -> NSError {
|
104
|
+
var userInfo = [String: AnyObject]()
|
105
|
+
if let res = res {
|
106
|
+
userInfo["statusCode"] = res.statusCode
|
107
|
+
}else{
|
108
|
+
userInfo["statusCode"] = 502
|
109
|
+
}
|
110
|
+
if let data: AnyObject = data {
|
111
|
+
userInfo["response"] = data
|
112
|
+
}
|
113
|
+
if let err = err {
|
114
|
+
userInfo["originalError"] = err
|
115
|
+
}
|
116
|
+
var error = NSError(domain: "com.aries.error", code: -1, userInfo: userInfo)
|
117
|
+
return error
|
118
|
+
}
|
119
|
+
|
120
|
+
}
|
121
|
+
|
122
|
+
|
123
|
+
class App: Base {
|
124
|
+
|
125
|
+
|
126
|
+
class func Create(name: String? = nil) -> RackJsonSchemaApiResponse {
|
127
|
+
var params = [String: AnyObject]()
|
128
|
+
|
129
|
+
|
130
|
+
if let name = name {
|
131
|
+
params["name"] = name
|
132
|
+
}
|
133
|
+
|
134
|
+
return request(Router.Create(params))
|
135
|
+
}
|
136
|
+
|
137
|
+
|
138
|
+
class func Delete(appId: String) -> RackJsonSchemaApiResponse {
|
139
|
+
var params = [String: AnyObject]()
|
140
|
+
|
141
|
+
params["appId"] = appId
|
142
|
+
|
143
|
+
|
144
|
+
return request(Router.Delete(params))
|
145
|
+
}
|
146
|
+
|
147
|
+
|
148
|
+
class func Info(appId: String) -> RackJsonSchemaApiResponse {
|
149
|
+
var params = [String: AnyObject]()
|
150
|
+
|
151
|
+
params["appId"] = appId
|
152
|
+
|
153
|
+
|
154
|
+
return request(Router.Info(params))
|
155
|
+
}
|
156
|
+
|
157
|
+
|
158
|
+
class func Info(appId: String) -> RackJsonSchemaApiResponse {
|
159
|
+
var params = [String: AnyObject]()
|
160
|
+
|
161
|
+
params["appId"] = appId
|
162
|
+
|
163
|
+
|
164
|
+
return request(Router.Info(params))
|
165
|
+
}
|
166
|
+
|
167
|
+
|
168
|
+
class func List() -> RackJsonSchemaApiResponse {
|
169
|
+
var params: [String: AnyObject]? = nil
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
return request(Router.List(params))
|
174
|
+
}
|
175
|
+
|
176
|
+
|
177
|
+
class func Update(appId: String, name: String? = nil) -> RackJsonSchemaApiResponse {
|
178
|
+
var params = [String: AnyObject]()
|
179
|
+
|
180
|
+
params["appId"] = appId
|
181
|
+
|
182
|
+
if let name = name {
|
183
|
+
params["name"] = name
|
184
|
+
}
|
185
|
+
|
186
|
+
return request(Router.Update(params))
|
187
|
+
}
|
188
|
+
|
189
|
+
|
190
|
+
class func Create(appId: String, file: String? = nil) -> RackJsonSchemaApiResponse {
|
191
|
+
var params = [String: AnyObject]()
|
192
|
+
|
193
|
+
params["appId"] = appId
|
194
|
+
|
195
|
+
if let file = file {
|
196
|
+
params["file"] = file
|
197
|
+
}
|
198
|
+
|
199
|
+
return request(Router.Create(params))
|
200
|
+
}
|
201
|
+
|
202
|
+
|
203
|
+
private enum Router: URLRequestConvertible {
|
204
|
+
|
205
|
+
case Create([String: AnyObject]?)
|
206
|
+
case Delete([String: AnyObject]?)
|
207
|
+
case Info([String: AnyObject]?)
|
208
|
+
case Info([String: AnyObject]?)
|
209
|
+
case List([String: AnyObject]?)
|
210
|
+
case Update([String: AnyObject]?)
|
211
|
+
case Create([String: AnyObject]?)
|
212
|
+
var method: Alamofire.Method {
|
213
|
+
switch self {
|
214
|
+
case .Create:
|
215
|
+
return .POST
|
216
|
+
case .Delete:
|
217
|
+
return .DELETE
|
218
|
+
case .Info:
|
219
|
+
return .GET
|
220
|
+
case .Info:
|
221
|
+
return .GET
|
222
|
+
case .List:
|
223
|
+
return .GET
|
224
|
+
case .Update:
|
225
|
+
return .PATCH
|
226
|
+
case .Create:
|
227
|
+
return .POST
|
228
|
+
}
|
229
|
+
}
|
230
|
+
|
231
|
+
var path: String {
|
232
|
+
switch self {
|
233
|
+
case .Create(let params):
|
234
|
+
return RackJsonSchemaApi.generateURI("/apps", params: params)
|
235
|
+
case .Delete(let params):
|
236
|
+
return RackJsonSchemaApi.generateURI("/apps/{appId}", params: params)
|
237
|
+
case .Info(let params):
|
238
|
+
return RackJsonSchemaApi.generateURI("/apps/{appId}", params: params)
|
239
|
+
case .Info(let params):
|
240
|
+
return RackJsonSchemaApi.generateURI("/apps/{appId}/text", params: params)
|
241
|
+
case .List(let params):
|
242
|
+
return RackJsonSchemaApi.generateURI("/apps", params: params)
|
243
|
+
case .Update(let params):
|
244
|
+
return RackJsonSchemaApi.generateURI("/apps/{appId}", params: params)
|
245
|
+
case .Create(let params):
|
246
|
+
return RackJsonSchemaApi.generateURI("/apps/{appId}/files", params: params)
|
247
|
+
}
|
248
|
+
}
|
249
|
+
|
250
|
+
// MARK: URLRequestConvertible
|
251
|
+
|
252
|
+
var URLRequest: NSURLRequest {
|
253
|
+
switch self{
|
254
|
+
case .Create(let parameters):
|
255
|
+
return Alamofire.ParameterEncoding.JSON.encode(RackJsonSchemaApi.defaultURLRequest(path, method: method), parameters: parameters).0
|
256
|
+
case .Delete(let parameters):
|
257
|
+
return Alamofire.ParameterEncoding.JSON.encode(RackJsonSchemaApi.defaultURLRequest(path, method: method), parameters: parameters).0
|
258
|
+
case .Info(let parameters):
|
259
|
+
return Alamofire.ParameterEncoding.URL.encode(RackJsonSchemaApi.defaultURLRequest(path, method: method), parameters: parameters).0
|
260
|
+
case .Info(let parameters):
|
261
|
+
return Alamofire.ParameterEncoding.URL.encode(RackJsonSchemaApi.defaultURLRequest(path, method: method), parameters: parameters).0
|
262
|
+
case .List(let parameters):
|
263
|
+
return Alamofire.ParameterEncoding.URL.encode(RackJsonSchemaApi.defaultURLRequest(path, method: method), parameters: parameters).0
|
264
|
+
case .Update(let parameters):
|
265
|
+
return Alamofire.ParameterEncoding.JSON.encode(RackJsonSchemaApi.defaultURLRequest(path, method: method), parameters: parameters).0
|
266
|
+
case .Create(let parameters):
|
267
|
+
return Alamofire.ParameterEncoding.JSON.encode(RackJsonSchemaApi.defaultURLRequest(path, method: method), parameters: parameters).0
|
268
|
+
}
|
269
|
+
}
|
270
|
+
|
271
|
+
}
|
272
|
+
|
273
|
+
|
274
|
+
}
|
275
|
+
|
276
|
+
|
277
|
+
}
|