Skip to content

Commit

Permalink
MultipartForm can now be converted from a Dictionary
Browse files Browse the repository at this point in the history
cURLClient.Post now expects an optional MultiPartForm. You may pass a
Dictionary to automatically convert to a MultiPartForm.

cURLClient.SetFormData is now form application/x-www-form-urlencoded
forms; for multipart/form-data use cuRLClient.SetForm.
  • Loading branch information
charonn0 committed Jun 7, 2015
1 parent 0267f49 commit dc6f1f1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
9 changes: 9 additions & 0 deletions libcURL/MultipartForm.rbbas
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ Inherits libcURL.cURLHandle
End Function
#tag EndMethod

#tag Method, Flags = &h0
Sub Operator_Convert(FromDict As Dictionary)
Me.Constructor()
For Each item As String In FromDict.Keys
If Not Me.AddElement(item, FromDict.Value(item)) Then Raise New cURLException(Me)
Next
End Sub
#tag EndMethod


#tag Note, Name = Using this class
This class represents a linked list of form elements that are managed by libcURL.
Expand Down
8 changes: 4 additions & 4 deletions libcURL/cURLClient.rbbas
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Inherits libcURL.cURLManager
#tag EndMethod

#tag Method, Flags = &h0
Sub Post(URL As String, FormData As Dictionary, WriteTo As Writeable = Nil)
Sub Post(URL As String, FormData As libcURL.MultiPartForm = Nil, WriteTo As Writeable = Nil)
' Asynchronously POST the passed FormData via HTTP(S) using multipart/form-data encoding. The FormData dictionary
' contains NAME:VALUE pairs comprising HTML form elements. NAME is a string containing the form-element name; VALUE
' may be a string or a FolderItem.
Expand All @@ -65,13 +65,13 @@ Inherits libcURL.cURLManager
' The transfer will be performed on the event loop (main thread).

Me.Cleanup()
Me.SetFormData(FormData)
Me.SetForm(FormData)
Me.Perform(URL, Nil, WriteTo)
End Sub
#tag EndMethod

#tag Method, Flags = &h0
Function Post(URL As String, FormData As Dictionary, WriteTo As Writeable = Nil) As Boolean
Function Post(URL As String, FormData As libcURL.MultiPartForm = Nil, WriteTo As Writeable = Nil) As Boolean
' Asynchronously POST the passed FormData via HTTP(S) using multipart/form-data encoding. The FormData dictionary
' contains NAME:VALUE pairs comprising HTML form elements. NAME is a string containing the form-element name; VALUE
' may be a string or a FolderItem.
Expand All @@ -81,7 +81,7 @@ Inherits libcURL.cURLManager
' on the calling thread.

Me.Cleanup()
Me.SetFormData(FormData)
Me.SetForm(FormData)
Return Me.Perform(URL, Nil, WriteTo)
End Function
#tag EndMethod
Expand Down
38 changes: 22 additions & 16 deletions libcURL/cURLManager.rbbas
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Protected Class cURLManager
Me.UploadMode = False
If Not Me.SetOption(libcURL.Opts.HTTPGET, True) Then Raise New libcURL.cURLException(mEasyItem)
mUpload = Nil

Me.SetFormData(Nil)
End Sub
#tag EndMethod

Expand Down Expand Up @@ -147,27 +147,33 @@ Protected Class cURLManager
#tag EndMethod

#tag Method, Flags = &h0
Sub SetFormData(FormData As Dictionary, Multipart As Boolean = True)
Select Case True
Case FormData = Nil And mForm <> Nil
If Not Me.SetOption(libcURL.Opts.HTTPPOST, Nil) Then Raise New libcURL.cURLException(mEasyItem)
mForm = Nil

Case Multipart And FormData <> Nil
mForm = New libcURL.MultipartForm
For Each item As String In FormData.Keys
If Not mForm.AddElement(item, FormData.Value(item)) Then Raise New libcURL.cURLException(mForm)
Next
Sub SetForm(FormData As libcURL.MultipartForm)
If FormData = Nil Then
If mForm <> Nil Then
If Not Me.SetOption(libcURL.Opts.HTTPPOST, Nil) Then Raise New libcURL.cURLException(mEasyItem)
mForm = Nil
End If
Else
If Not Me.SetOption(libcURL.Opts.HTTPPOST, mForm) Then Raise New libcURL.cURLException(mEasyItem)
mForm = New libcURL.MultipartForm
End If
End Sub
#tag EndMethod

#tag Method, Flags = &h0
Sub SetFormData(FormData As Dictionary)
If FormData = Nil Then
Me.SetForm(Nil)

Case FormData <> Nil
Else
Dim data() As String
For Each key As String in FormData.Keys
data.Append(URLEncode(Key) + "=" + URLEncode(FormData.Value(key)))
Next
If Not mEasyItem.SetOption(libcURL.Opts.COPYPOSTFIELDS, Join(data, "&")) Then Raise New libcURL.cURLException(mEasyItem)

End Select
End If

End Sub
#tag EndMethod

Expand Down Expand Up @@ -353,8 +359,8 @@ Protected Class cURLManager
Private mErrorBuffer As MemoryBlock
#tag EndProperty

#tag Property, Flags = &h21
Private mForm As libcURL.MultipartForm
#tag Property, Flags = &h1
Protected mForm As libcURL.MultipartForm
#tag EndProperty

#tag Property, Flags = &h21
Expand Down

0 comments on commit dc6f1f1

Please sign in to comment.