// | |
// OCRClient.swift | |
// ReceiptManager | |
// | |
// Created by non on 2016/10/10. | |
// Copyright © 2016年 non. All rights reserved. | |
// | |
import Foundation | |
import Alamofire | |
public class OCRClient { | |
let HOST = "https://vision.googleapis.com" | |
let ANNOTATE_API = "/v1/images:annotate" | |
var api_key: String | |
init(api_key: String){ | |
self.api_key = api_key | |
} | |
// MARK: - main request method | |
func annotate(image: Data) -> AnnotatedResponse? { | |
let parameters: Parameters = [ | |
"requests": [ | |
"image": [ | |
"content": image.base64EncodedString() | |
], | |
"features": [ | |
[ | |
"type": "TEXT_DETECTION", | |
"maxResults": 2 | |
] | |
] | |
] | |
] | |
let url = HOST + ANNOTATE_API + "?key=" + api_key | |
var annotatedResonse: AnnotatedResponse? | |
Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).validate().responseJSON { | |
response in | |
print(response) | |
if let json = response.result.value { | |
annotatedResonse = try? AnnotatedResponse.decodeValue(json) | |
} | |
} | |
return annotatedResonse | |
} | |
// MARK: - convenience request | |
func annotate(imagePath: URL) -> AnnotatedResponse? { | |
let image = try! Data.init(contentsOf: imagePath) | |
return annotate(image: image) | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
まず L15 の
|
This comment has been minimized.
This comment has been minimized.
あとどうでもいいけど L51 の
だけど Swift は明示的に
の方が見栄えが言いと思う。 あと |
This comment has been minimized.
This comment has been minimized.
なんか重箱の隅ばっかだけどスネークケースな
はこの書き方だとインスタンスに紐付く定数なんだけど、意味的にはもっと大域でも良さそうなのでクラスに紐付くように
って書いた方が伝わる感じがする |
This comment has been minimized.
This comment has been minimized.
なるほど!!!! |
This comment has been minimized.
This comment has been minimized.
まだ重箱なんだけど
の |
This comment has been minimized.
This comment has been minimized.
文化ぜんぜん知らんので重箱の隅助かる |
This comment has been minimized.
This comment has been minimized.
このくらいの規模なら |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
厳しい。。。 この程度の規模のAPIなら題材としても楽だし、今コスト払って公式ライブラリ使えるようになっていたほうが良さそうだね。 |
This comment has been minimized.
This comment has been minimized.
んで、 |
This comment has been minimized.
This comment has been minimized.
ネットワークライブラリ使わずに自作ネットワークライブラリつくって自分で脆弱性つくり込むより、メンテナンスされたライブラリ使った方がいいとも思いますけどね |
This comment has been minimized.
39-45行目が実行されていない様子