1
1
using Newtonsoft . Json ;
2
+ using Redcap . Http ;
2
3
using Redcap . Models ;
3
4
using Serilog ;
4
5
using System ;
@@ -213,10 +214,10 @@ public static async Task<string> ConvertIntArraytoString(this RedcapApi redcapAp
213
214
try
214
215
{
215
216
StringBuilder builder = new StringBuilder ( ) ;
216
- foreach ( var v in inputArray )
217
+ foreach ( var intValue in inputArray )
217
218
{
218
219
219
- builder . Append ( v ) ;
220
+ builder . Append ( intValue ) ;
220
221
// We do not need to append the , if less than or equal to a single string
221
222
if ( inputArray . Length <= 1 )
222
223
{
@@ -614,7 +615,7 @@ public static async Task<Stream> GetStreamContentAsync(this RedcapApi redcapApi,
614
615
/// <param name="payload">data</param>
615
616
/// <param name="uri">URI of the api instance</param>
616
617
/// <returns>string</returns>
617
- public static async Task < string > SendRequestAsync ( this RedcapApi redcapApi , MultipartFormDataContent payload , Uri uri )
618
+ public static async Task < string > SendPostRequestAsync ( this RedcapApi redcapApi , MultipartFormDataContent payload , Uri uri )
618
619
{
619
620
try
620
621
{
@@ -644,9 +645,8 @@ public static async Task<string> SendRequestAsync(this RedcapApi redcapApi, Mult
644
645
/// <param name="redcapApi"></param>
645
646
/// <param name="payload">data</param>
646
647
/// <param name="uri">URI of the api instance</param>
647
- /// <param name="isLargeDataset">Requests size > 32k chars </param>
648
648
/// <returns></returns>
649
- public static async Task < string > SendPostRequestAsync ( this RedcapApi redcapApi , Dictionary < string , string > payload , Uri uri , bool isLargeDataset = false )
649
+ public static async Task < string > SendPostRequestAsync ( this RedcapApi redcapApi , Dictionary < string , string > payload , Uri uri )
650
650
{
651
651
try
652
652
{
@@ -666,112 +666,53 @@ public static async Task<string> SendPostRequestAsync(this RedcapApi redcapApi,
666
666
payload . Remove ( pathkey ) ;
667
667
}
668
668
669
- /*
670
- * Encode the values for payload
671
- * Add in ability to process large data set, using StringContent
672
- * Thanks to Ibrahim for pointing this out.
673
- * https://stackoverflow.com/questions/23703735/how-to-set-large-string-inside-httpcontent-when-using-httpclient/23740338
674
- */
675
- if ( isLargeDataset )
669
+ using ( var content = new CustomFormUrlEncodedContent ( payload ) )
676
670
{
677
- /*
678
- * Send request with large data set
679
- */
680
-
681
- var serializedPayload = JsonConvert . SerializeObject ( payload ) ;
682
- using ( var content = new StringContent ( serializedPayload , Encoding . UTF8 , "application/json" ) )
671
+ using ( var response = await client . PostAsync ( uri , content ) )
683
672
{
684
- using ( var response = await client . PostAsync ( uri , content ) )
673
+ if ( response . IsSuccessStatusCode )
685
674
{
686
- if ( response . IsSuccessStatusCode )
675
+ // Get the filename so we can save with the name
676
+ var headers = response . Content . Headers ;
677
+ var fileName = headers . ContentType . Parameters . Select ( x => x . Value ) . FirstOrDefault ( ) ;
678
+ if ( ! string . IsNullOrEmpty ( fileName ) )
687
679
{
688
- // Get the filename so we can save with the name
689
- var headers = response . Content . Headers ;
690
- var fileName = headers . ContentType . Parameters . Select ( x => x . Value ) . FirstOrDefault ( ) ;
691
- if ( ! string . IsNullOrEmpty ( fileName ) )
692
- {
693
- var contentDisposition = response . Content . Headers . ContentDisposition = new ContentDispositionHeaderValue ( "attachment" )
694
- {
695
- FileName = fileName
696
- } ;
697
- }
698
-
699
- if ( ! string . IsNullOrEmpty ( pathValue ) )
700
- {
701
- // save the file to a specified location using an extension method
702
- await response . Content . ReadAsFileAsync ( fileName , pathValue , true ) ;
703
- _responseMessage = fileName ;
704
- }
705
- else
680
+ var contentDisposition = response . Content . Headers . ContentDisposition = new ContentDispositionHeaderValue ( "attachment" )
706
681
{
707
- _responseMessage = await response . Content . ReadAsStringAsync ( ) ;
708
- }
709
-
682
+ FileName = fileName
683
+ } ;
710
684
}
711
- else
712
- {
713
- _responseMessage = await response . Content . ReadAsStringAsync ( ) ;
714
- }
715
- }
716
685
717
- }
718
- return _responseMessage ;
719
- }
720
- else
721
- {
722
- /*
723
- * Maximum character limit of 32,000 using FormUrlEncodedContent
724
- * Send request using small data set
725
- */
726
- using ( var content = new FormUrlEncodedContent ( payload ) )
727
- {
728
- using ( var response = await client . PostAsync ( uri , content ) )
729
- {
730
- if ( response . IsSuccessStatusCode )
686
+
687
+ if ( ! string . IsNullOrEmpty ( pathValue ) )
731
688
{
732
- // Get the filename so we can save with the name
733
- var headers = response . Content . Headers ;
734
- var fileName = headers . ContentType . Parameters . Select ( x => x . Value ) . FirstOrDefault ( ) ;
735
- if ( ! string . IsNullOrEmpty ( fileName ) )
689
+ var fileExtension = payload . Where ( x => x . Key == "content" && x . Value == "pdf" ) . SingleOrDefault ( ) . Value ;
690
+ if ( ! string . IsNullOrEmpty ( fileExtension ) )
736
691
{
737
- var contentDisposition = response . Content . Headers . ContentDisposition = new ContentDispositionHeaderValue ( "attachment" )
738
- {
739
- FileName = fileName
740
- } ;
741
- }
742
-
692
+ // pdf
693
+ fileName = payload . Where ( x => x . Key == "instrument" ) . SingleOrDefault ( ) . Value ;
694
+ // to do , make extensions for various types
695
+ // save the file to a specified location using an extension method
696
+ await response . Content . ReadAsFileAsync ( fileName , pathValue , true , fileExtension ) ;
743
697
744
- if ( ! string . IsNullOrEmpty ( pathValue ) )
745
- {
746
- var fileExtension = payload . Where ( x => x . Key == "content" && x . Value == "pdf" ) . SingleOrDefault ( ) . Value ;
747
- if ( ! string . IsNullOrEmpty ( fileExtension ) )
748
- {
749
- // pdf
750
- fileName = payload . Where ( x => x . Key == "instrument" ) . SingleOrDefault ( ) . Value ;
751
- // to do , make extensions for various types
752
- // save the file to a specified location using an extension method
753
- await response . Content . ReadAsFileAsync ( fileName , pathValue , true , fileExtension ) ;
754
-
755
- }
756
- else
757
- {
758
- await response . Content . ReadAsFileAsync ( fileName , pathValue , true , fileExtension ) ;
759
-
760
- }
761
- _responseMessage = fileName ;
762
698
}
763
699
else
764
700
{
765
- _responseMessage = await response . Content . ReadAsStringAsync ( ) ;
701
+ await response . Content . ReadAsFileAsync ( fileName , pathValue , true , fileExtension ) ;
702
+
766
703
}
704
+ _responseMessage = fileName ;
767
705
}
768
706
else
769
707
{
770
708
_responseMessage = await response . Content . ReadAsStringAsync ( ) ;
771
709
}
772
710
}
711
+ else
712
+ {
713
+ _responseMessage = await response . Content . ReadAsStringAsync ( ) ;
714
+ }
773
715
}
774
-
775
716
}
776
717
return _responseMessage ;
777
718
}
@@ -781,14 +722,13 @@ public static async Task<string> SendPostRequestAsync(this RedcapApi redcapApi,
781
722
Log . Error ( $ "{ Ex . Message } ") ;
782
723
return Empty ;
783
724
}
784
- }
785
- /// <summary>
786
- /// Sends http request to api
787
- /// </summary>
788
- /// <param name="redcapApi"></param>
789
- /// <param name="payload">data </param>
790
- /// <param name="uri">URI of the api instance</param>
791
- /// <returns>string</returns>
725
+ } /// <summary>
726
+ /// Sends http request to api
727
+ /// </summary>
728
+ /// <param name="redcapApi"></param>
729
+ /// <param name="payload">data </param>
730
+ /// <param name="uri">URI of the api instance</param>
731
+ /// <returns>string</returns>
792
732
public static async Task < string > SendPostRequest ( this RedcapApi redcapApi , Dictionary < string , string > payload , Uri uri )
793
733
{
794
734
string responseString ;
0 commit comments