Permalink
Browse files

fix: Conversion of file data to multipart form data

  • Loading branch information...
AnthonyOliveri committed Oct 9, 2018
1 parent 9392b23 commit 1d46baf317f171e7b8a8ccba2a4efcf67149ac11
View
@@ -1,2 +1,2 @@
github "daltoniam/Starscream" ~> 3.0
github "watson-developer-cloud/restkit" ~> 1.2
github "watson-developer-cloud/restkit" ~> 1.3
View
@@ -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"
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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
@@ -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
@@ -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)
@@ -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)
@@ -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)
@@ -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)

0 comments on commit 1d46baf

Please sign in to comment.