Skip to content

Commit

Permalink
fix: Conversion of file data to multipart form data
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Oliveri committed Oct 19, 2018
1 parent 9392b23 commit 1d46baf
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github "daltoniam/Starscream" ~> 3.0
github "watson-developer-cloud/restkit" ~> 1.2
github "watson-developer-cloud/restkit" ~> 1.3
4 changes: 2 additions & 2 deletions Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github "daltoniam/Starscream" "3.0.5"
github "watson-developer-cloud/restkit" "1.2.0"
github "daltoniam/Starscream" "3.0.6"
github "watson-developer-cloud/restkit" "1.3.0"
45 changes: 26 additions & 19 deletions Source/DiscoveryV1/Discovery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -821,21 +821,22 @@ public class Discovery {
// construct body
let multipartFormData = MultipartFormData()
if let configuration = configuration {
guard let configurationData = configuration.data(using: .utf8) else {
failure?(RestError.serializationError)
return
if let configurationData = configuration.data(using: .utf8) {
multipartFormData.append(configurationData, withName: "configuration")
}
multipartFormData.append(configurationData, withName: "configuration")
}
if let file = file {
multipartFormData.append(file, withName: "file")
do {
try multipartFormData.append(file: file, withName: "file")
} catch {
failure?(error)
return
}
}
if let metadata = metadata {
guard let metadataData = metadata.data(using: .utf8) else {
failure?(RestError.serializationError)
return
if let metadataData = metadata.data(using: .utf8) {
multipartFormData.append(metadataData, withName: "metadata")
}
multipartFormData.append(metadataData, withName: "metadata")
}
guard let body = try? multipartFormData.toData() else {
failure?(RestError.encodingError)
Expand Down Expand Up @@ -1472,14 +1473,17 @@ public class Discovery {
// construct body
let multipartFormData = MultipartFormData()
if let file = file {
multipartFormData.append(file, withName: "file")
do {
try multipartFormData.append(file: file, withName: "file")
} catch {
failure?(error)
return
}
}
if let metadata = metadata {
guard let metadataData = metadata.data(using: .utf8) else {
failure?(RestError.serializationError)
return
if let metadataData = metadata.data(using: .utf8) {
multipartFormData.append(metadataData, withName: "metadata")
}
multipartFormData.append(metadataData, withName: "metadata")
}
guard let body = try? multipartFormData.toData() else {
failure?(RestError.encodingError)
Expand Down Expand Up @@ -1620,14 +1624,17 @@ public class Discovery {
// construct body
let multipartFormData = MultipartFormData()
if let file = file {
multipartFormData.append(file, withName: "file")
do {
try multipartFormData.append(file: file, withName: "file")
} catch {
failure?(error)
return
}
}
if let metadata = metadata {
guard let metadataData = metadata.data(using: .utf8) else {
failure?(RestError.serializationError)
return
if let metadataData = metadata.data(using: .utf8) {
multipartFormData.append(metadataData, withName: "metadata")
}
multipartFormData.append(metadataData, withName: "metadata")
}
guard let body = try? multipartFormData.toData() else {
failure?(RestError.encodingError)
Expand Down
21 changes: 18 additions & 3 deletions Source/LanguageTranslatorV2/LanguageTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,28 @@ public class LanguageTranslator {
// construct body
let multipartFormData = MultipartFormData()
if let forcedGlossary = forcedGlossary {
multipartFormData.append(forcedGlossary, withName: "forced_glossary")
do {
try multipartFormData.append(file: forcedGlossary, withName: "forced_glossary")
} catch {
failure?(error)
return
}
}
if let parallelCorpus = parallelCorpus {
multipartFormData.append(parallelCorpus, withName: "parallel_corpus")
do {
try multipartFormData.append(file: parallelCorpus, withName: "parallel_corpus")
} catch {
failure?(error)
return
}
}
if let monolingualCorpus = monolingualCorpus {
multipartFormData.append(monolingualCorpus, withName: "monolingual_corpus")
do {
try multipartFormData.append(file: monolingualCorpus, withName: "monolingual_corpus")
} catch {
failure?(error)
return
}
}
guard let body = try? multipartFormData.toData() else {
failure?(RestError.encodingError)
Expand Down
14 changes: 12 additions & 2 deletions Source/LanguageTranslatorV3/LanguageTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,20 @@ public class LanguageTranslator {
// construct body
let multipartFormData = MultipartFormData()
if let forcedGlossary = forcedGlossary {
multipartFormData.append(forcedGlossary, withName: "forced_glossary")
do {
try multipartFormData.append(file: forcedGlossary, withName: "forced_glossary")
} catch {
failure?(error)
return
}
}
if let parallelCorpus = parallelCorpus {
multipartFormData.append(parallelCorpus, withName: "parallel_corpus")
do {
try multipartFormData.append(file: parallelCorpus, withName: "parallel_corpus")
} catch {
failure?(error)
return
}
}
guard let body = try? multipartFormData.toData() else {
failure?(RestError.encodingError)
Expand Down
14 changes: 12 additions & 2 deletions Source/NaturalLanguageClassifierV1/NaturalLanguageClassifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,18 @@ public class NaturalLanguageClassifier {
{
// construct body
let multipartFormData = MultipartFormData()
multipartFormData.append(metadata, withName: "training_metadata")
multipartFormData.append(trainingData, withName: "training_data")
do {
try multipartFormData.append(file: metadata, withName: "training_metadata")
} catch {
failure?(error)
return
}
do {
try multipartFormData.append(file: trainingData, withName: "training_data")
} catch {
failure?(error)
return
}
guard let body = try? multipartFormData.toData() else {
failure?(RestError.encodingError)
return
Expand Down
7 changes: 6 additions & 1 deletion Source/SpeechToTextV1/SpeechToText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1540,7 +1540,12 @@ public class SpeechToText {
{
// construct body
let multipartFormData = MultipartFormData()
multipartFormData.append(corpusFile, withName: "corpus_file")
do {
try multipartFormData.append(file: corpusFile, withName: "corpus_file")
} catch {
failure?(error)
return
}
guard let body = try? multipartFormData.toData() else {
failure?(RestError.encodingError)
return
Expand Down
78 changes: 48 additions & 30 deletions Source/VisualRecognitionV3/VisualRecognition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,35 +176,32 @@ public class VisualRecognition {
// construct body
let multipartFormData = MultipartFormData()
if let imagesFile = imagesFile {
multipartFormData.append(imagesFile, withName: "images_file")
do {
try multipartFormData.append(file: imagesFile, withName: "images_file")
} catch {
failure?(error)
return
}
}
if let url = url {
guard let urlData = url.data(using: .utf8) else {
failure?(RestError.serializationError)
return
if let urlData = url.data(using: .utf8) {
multipartFormData.append(urlData, withName: "url")
}
multipartFormData.append(urlData, withName: "url")
}
if let threshold = threshold {
guard let thresholdData = "\(threshold)".data(using: .utf8) else {
failure?(RestError.serializationError)
return
if let thresholdData = "\(threshold)".data(using: .utf8) {
multipartFormData.append(thresholdData, withName: "threshold")
}
multipartFormData.append(thresholdData, withName: "threshold")
}
if let owners = owners {
guard let ownersData = owners.joined(separator: ",").data(using: .utf8) else {
failure?(RestError.serializationError)
return
if let ownersData = owners.joined(separator: ",").data(using: .utf8) {
multipartFormData.append(ownersData, withName: "owners")
}
multipartFormData.append(ownersData, withName: "owners")
}
if let classifierIDs = classifierIDs {
guard let classifierIDsData = classifierIDs.joined(separator: ",").data(using: .utf8) else {
failure?(RestError.serializationError)
return
if let classifierIDsData = classifierIDs.joined(separator: ",").data(using: .utf8) {
multipartFormData.append(classifierIDsData, withName: "classifier_ids")
}
multipartFormData.append(classifierIDsData, withName: "classifier_ids")
}
guard let body = try? multipartFormData.toData() else {
failure?(RestError.encodingError)
Expand Down Expand Up @@ -285,14 +282,17 @@ public class VisualRecognition {
// construct body
let multipartFormData = MultipartFormData()
if let imagesFile = imagesFile {
multipartFormData.append(imagesFile, withName: "images_file", mimeType: "application/octet-stream")
do {
try multipartFormData.append(file: imagesFile, withName: "images_file")
} catch {
failure?(error)
return
}
}
if let url = url {
guard let urlData = url.data(using: .utf8) else {
failure?(RestError.serializationError)
return
if let urlData = url.data(using: .utf8) {
multipartFormData.append(urlData, withName: "url")
}
multipartFormData.append(urlData, withName: "url")
}
guard let body = try? multipartFormData.toData() else {
failure?(RestError.encodingError)
Expand Down Expand Up @@ -366,16 +366,24 @@ public class VisualRecognition {
{
// construct body
let multipartFormData = MultipartFormData()
guard let nameData = name.data(using: .utf8) else {
failure?(RestError.serializationError)
return
if let nameData = name.data(using: .utf8) {
multipartFormData.append(nameData, withName: "name")
}
multipartFormData.append(nameData, withName: "name")
positiveExamples.forEach { example in
multipartFormData.append(example.examples, withName: example.name + "_positive_examples")
do {
try multipartFormData.append(file: example.examples, withName: example.name + "_positive_examples")
} catch {
failure?(error)
return
}
}
if let negativeExamples = negativeExamples {
multipartFormData.append(negativeExamples, withName: "negative_examples")
do {
try multipartFormData.append(file: negativeExamples, withName: "negative_examples")
} catch {
failure?(error)
return
}
}
guard let body = try? multipartFormData.toData() else {
failure?(RestError.encodingError)
Expand Down Expand Up @@ -561,11 +569,21 @@ public class VisualRecognition {
let multipartFormData = MultipartFormData()
if let positiveExamples = positiveExamples {
positiveExamples.forEach { example in
multipartFormData.append(example.examples, withName: example.name + "_positive_examples")
do {
try multipartFormData.append(file: example.examples, withName: example.name + "_positive_examples")
} catch {
failure?(error)
return
}
}
}
if let negativeExamples = negativeExamples {
multipartFormData.append(negativeExamples, withName: "negative_examples")
do {
try multipartFormData.append(file: negativeExamples, withName: "negative_examples")
} catch {
failure?(error)
return
}
}
guard let body = try? multipartFormData.toData() else {
failure?(RestError.encodingError)
Expand Down

0 comments on commit 1d46baf

Please sign in to comment.