@@ -63,50 +63,56 @@ open class ImagesAPI: APIBase {
6363 - parameter imageType: (path) Image type
6464 - parameter authorization: (header) Format is: \"Scheme CredentialsList\". Possible values are: - Anon AK=AppKey - SocialPlus TK=SessionToken - Facebook AK=AppKey|TK=AccessToken - Google AK=AppKey|TK=AccessToken - Twitter AK=AppKey|RT=RequestToken|TK=AccessToken - Microsoft AK=AppKey|TK=AccessToken - AADS2S AK=AppKey|[UH=UserHandle]|TK=AADToken
6565 - parameter image: (body) MIME encoded contents of the image
66+ - parameter imageFileType: type of the file, such as "image/jpeg", "image/png", or "image/gif"
6667 - parameter completion: completion handler to receive the data and the error objects
6768 */
68- open class func imagesPostImage( imageType: ImageType_imagesPostImage , authorization: String , image: Data , completion: @escaping ( ( _ data: PostImageResponse ? , _ error: ErrorResponse ? ) -> Void ) ) {
69- imagesPostImageWithRequestBuilder ( imageType: imageType, authorization: authorization, image: image) . execute { ( response, error) -> Void in
70- completion ( response? . body, error)
71- }
72- }
73-
74-
75- /**
76- Upload a new image
77- - POST /v0.7/images/{imageType}
78- - Images will be resized. To access a resized image, append the 1 character size identifier to the blobHandle that is returned. - d is 25 pixels wide - h is 50 pixels wide - l is 100 pixels wide - p is 250 pixels wide - t is 500 pixels wide - x is 1000 pixels wide - ImageType.UserPhoto supports d,h,l,p,t,x - ImageType.ContentBlob supports d,h,l,p,t,x - ImageType.AppIcon supports l All resized images will maintain their aspect ratio. Any orientation specified in the EXIF headers will be honored.
79-
80- - examples: [{contentType=application/json, example={
81- "blobHandle" : "aeiou"
82- }}, {contentType=application/xml, example=<null>
83- <blobHandle>aeiou</blobHandle>
84- </null>}]
85- - examples: [{contentType=application/json, example={
86- "blobHandle" : "aeiou"
87- }}, {contentType=application/xml, example=<null>
88- <blobHandle>aeiou</blobHandle>
89- </null>}]
90- - parameter imageType: (path) Image type
91- - parameter authorization: (header) Format is: \"Scheme CredentialsList\". Possible values are: - Anon AK=AppKey - SocialPlus TK=SessionToken - Facebook AK=AppKey|TK=AccessToken - Google AK=AppKey|TK=AccessToken - Twitter AK=AppKey|RT=RequestToken|TK=AccessToken - Microsoft AK=AppKey|TK=AccessToken - AADS2S AK=AppKey|[UH=UserHandle]|TK=AADToken
92- - parameter image: (body) MIME encoded contents of the image
93- - returns: RequestBuilder<PostImageResponse>
94- */
95- open class func imagesPostImageWithRequestBuilder( imageType: ImageType_imagesPostImage , authorization: String , image: Data ) -> RequestBuilder < PostImageResponse > {
69+ open class func imagesPostImage( imageType: ImageType_imagesPostImage , authorization: String , image: Data , imageFileType: String , completion: @escaping ( ( _ data: PostImageResponse ? , _ error: ErrorResponse ? ) -> Void ) ) {
70+
71+ // construct the URL where the POST will be issued
9672 var path = " /v0.7/images/{imageType} "
9773 path = path. replacingOccurrences ( of: " {imageType} " , with: " \( imageType. rawValue) " , options: . literal, range: nil )
9874 let URLString = EmbeddedSocialClientAPI . basePath + path
99- let parameters = image. encodeToJSON ( ) as? [ String : AnyObject ]
100-
101- let url = NSURLComponents ( string: URLString)
102- let nillableHeaders : [ String : Any ? ] = [
103- " Authorization " : authorization
104- ]
105- let headerParameters = APIHelper . rejectNilHeaders ( nillableHeaders)
106-
107- let requestBuilder : RequestBuilder < PostImageResponse > . Type = EmbeddedSocialClientAPI . requestBuilderFactory. getBuilder ( )
108-
109- return requestBuilder. init ( method: " POST " , URLString: ( url? . string ?? URLString) , parameters: parameters, isBody: true , headers: headerParameters)
75+ let url = URL ( string: URLString) !
76+ var urlRequest = URLRequest ( url: url)
77+ urlRequest. httpMethod = " POST "
78+
79+ // construct the body
80+ urlRequest. httpBody = image
81+
82+ // construct the headers
83+ urlRequest. setValue ( imageFileType, forHTTPHeaderField: " Content-Type " )
84+ urlRequest. setValue ( authorization, forHTTPHeaderField: " Authorization " )
85+
86+ // issue the request
87+ let dataRequest = Alamofire . request ( urlRequest)
88+
89+ // parse the response
90+ let validatedDataRequest = dataRequest. validate ( )
91+ validatedDataRequest. responseJSON ( options: . allowFragments) { response in
92+ // failure
93+ if response. result. isFailure {
94+ completion ( nil , ErrorResponse . HttpError ( statusCode: response. response? . statusCode ?? 500 , data: response. data, error: response. result. error!) )
95+ return
96+ }
97+
98+ // handle HTTP 204 No Content
99+ if response. response? . statusCode == 204 && response. result. value is NSNull {
100+ completion ( nil , nil )
101+ return
102+ }
103+
104+ // parse the result into PostImageResponse
105+ if let json: Any = response. result. value {
106+ let decoded = Decoders . decode ( clazz: PostImageResponse . self, source: json as AnyObject , instance: nil )
107+ switch decoded {
108+ case let . success( object) : completion ( object, nil )
109+ case let . failure( error) : completion ( nil , ErrorResponse . DecodeError ( response: response. data, decodeError: error) )
110+ }
111+ return
112+ }
113+
114+ // unexpected
115+ completion ( nil , ErrorResponse . HttpError ( statusCode: 500 , data: nil , error: NSError ( domain: " localhost " , code: 500 , userInfo: [ " reason " : " unreacheable code " ] ) ) )
116+ }
110117 }
111-
112118}
0 commit comments