1 #Region "Microsoft.VisualBasic::7f24c2ef06dc7b20508e8d8ced1f3148, Microsoft.VisualBasic.Core\Extensions\WebServices\RequestBuilder.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Module RequestBuilder
35     
36     '         Function: BuildParameters, IsPrimitive
37     
38     
39     ' /********************************************************************************/
40
41 #End Region
42
43 Imports System.Reflection
44 Imports System.Runtime.CompilerServices
45 Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel
46 Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel.SchemaMaps
47 Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel.SchemaMaps.DataFrameColumnAttribute
48 Imports Microsoft.VisualBasic.Language
49
50 Namespace Net.Http
51
52     Public Module RequestBuilder
53
54         ''' <summary>
55         ''' Encoding a class object as url parameters.
56         ''' (当前的这个函数操作里面已经包含有了URL的编码工作了)
57         ''' </summary>
58         ''' <typeparam name="T"></typeparam>
59         ''' <param name="obj"></param>
60         ''' <returns></returns>
61         <Extension>
62         Public Function BuildParameters(Of T As Class)(obj As T) As String
63             Dim type As Type = GetType(T)
64             Dim args As New List(Of NamedValue(Of String))
65             Dim ps = LoadMapping(type, mapsAll:=True)
66
67             For Each p As BindProperty(Of DataFrameColumnAttribute) In ps.Values.Where(IsPrimitive)
68                 Dim value As Object = p.GetValue(obj)
69
70                 If Not value Is Nothing Then
71                     Dim str$
72
73                     If value.GetType.IsEnum Then
74                         str = DirectCast(value, [Enum]).Description
75                     Else
76                         str = value.ToString
77                     End If
78
79                     args += New NamedValue(Of StringWith {
80                         .Value = str,
81                         .Name = p.field.Name
82                     }
83                 End If
84             Next
85
86             Dim param As String = args _
87                 .Select(Function(o) $"{o.Name}={o.Value.UrlEncode}") _
88                 .JoinBy("&")
89             Return param
90         End Function
91
92         Private Function IsPrimitive() As Func(Of BindProperty(Of DataFrameColumnAttribute), Boolean)
93             Return Function(o)
94                        Return DataFramework.IsPrimitive(DirectCast(o.member, PropertyInfo).PropertyType)
95                    End Function
96         End Function
97     End Module
98 End Namespace