1
- // using System.Collections.Generic;
2
- // using System.Linq;
3
- // using System.Net.Http;
4
- // using EnsureThat;
5
- // using MyCouch.Net;
6
- // using MyCouch.Requests;
7
- // using MyCouch.Serialization;
8
-
9
- // namespace MyCouch.HttpRequestFactories
10
- // {
11
- // public class SearchIndexHttpRequestFactory
12
- // {
13
- // protected ISerializer Serializer { get; private set; }
14
-
15
- // public SearchIndexHttpRequestFactory(ISerializer serializer)
16
- // {
17
- // Ensure.Any.IsNotNull(serializer, nameof(serializer));
18
-
19
- // Serializer = serializer;
20
- // }
21
-
22
- // public virtual HttpRequest Create(SearchIndexRequest request)
23
- // {
24
- // Ensure.Any.IsNotNull(request, nameof(request));
25
-
26
- // return new HttpRequest(HttpMethod.Get, GenerateRelativeUrl(request))
27
- // .SetRequestTypeHeader(request.GetType());
28
- // }
29
-
30
- // protected virtual string GenerateRelativeUrl(SearchIndexRequest request)
31
- // {
32
- // return string.Format("/_design/{0}/_search/{1}{2}",
33
- // new UrlSegment(request.IndexIdentity.DesignDocument),
34
- // new UrlSegment(request.IndexIdentity.Name),
35
- // GenerateRequestUrlQueryString(request));
36
- // }
37
-
38
- // protected virtual string GenerateRequestUrlQueryString(SearchIndexRequest request)
39
- // {
40
- // var p = GenerateQueryStringParams(request);
41
-
42
- // return string.IsNullOrEmpty(p) ? string.Empty : string.Concat("?", p);
43
- // }
44
-
45
- // protected virtual string GenerateQueryStringParams(SearchIndexRequest request)
46
- // {
47
- // return string.Join("&", GenerateJsonCompatibleKeyValues(request)
48
- // .Select(kv => string.Format("{0}={1}", kv.Key, UrlParam.Encode(kv.Value))));
49
- // }
50
-
51
- // protected virtual IDictionary<string, string> GenerateJsonCompatibleKeyValues(SearchIndexRequest request)
52
- // {
53
- // var kvs = new Dictionary<string, string>();
54
-
55
- // if (!string.IsNullOrWhiteSpace(request.Expression))
56
- // kvs.Add(KeyNames.Expression, request.Expression);
57
-
58
- // if (request.HasSortings())
59
- // kvs.Add(KeyNames.Sort, Serializer.ToJsonArray(request.Sort.ToArray()));
60
-
61
- // if (!string.IsNullOrWhiteSpace(request.Bookmark))
62
- // kvs.Add(KeyNames.Bookmark, request.Bookmark);
63
-
64
- // if (request.Stale.HasValue)
65
- // kvs.Add(KeyNames.Stale, request.Stale.Value.AsString());
66
-
67
- // if (request.Limit.HasValue)
68
- // kvs.Add(KeyNames.Limit, Serializer.ToJson(request.Limit.Value));
69
-
70
- // if (request.IncludeDocs.HasValue)
71
- // kvs.Add(KeyNames.IncludeDocs, Serializer.ToJson(request.IncludeDocs.Value));
1
+ using System . Collections . Generic ;
2
+ using System . Linq ;
3
+ using System . Net . Http ;
4
+ using EnsureThat ;
5
+ using MyCouch . Net ;
6
+ using MyCouch . Requests ;
7
+ using MyCouch . Serialization ;
8
+
9
+ namespace MyCouch . HttpRequestFactories
10
+ {
11
+ public class SearchIndexHttpRequestFactory
12
+ {
13
+ protected ISerializer Serializer { get ; private set ; }
14
+
15
+ public SearchIndexHttpRequestFactory ( ISerializer serializer )
16
+ {
17
+ Ensure . Any . IsNotNull ( serializer , nameof ( serializer ) ) ;
18
+
19
+ Serializer = serializer ;
20
+ }
21
+
22
+ public virtual HttpRequest Create ( SearchIndexRequest request )
23
+ {
24
+ Ensure . Any . IsNotNull ( request , nameof ( request ) ) ;
25
+
26
+ return new HttpRequest ( HttpMethod . Get , GenerateRelativeUrl ( request ) )
27
+ . SetRequestTypeHeader ( request . GetType ( ) ) ;
28
+ }
29
+
30
+ protected virtual string GenerateRelativeUrl ( SearchIndexRequest request )
31
+ {
32
+ return string . Format ( "/_design/{0}/_search/{1}{2}" ,
33
+ new UrlSegment ( request . IndexIdentity . DesignDocument ) ,
34
+ new UrlSegment ( request . IndexIdentity . Name ) ,
35
+ GenerateRequestUrlQueryString ( request ) ) ;
36
+ }
37
+
38
+ protected virtual string GenerateRequestUrlQueryString ( SearchIndexRequest request )
39
+ {
40
+ var p = GenerateQueryStringParams ( request ) ;
41
+
42
+ return string . IsNullOrEmpty ( p ) ? string . Empty : string . Concat ( "?" , p ) ;
43
+ }
44
+
45
+ protected virtual string GenerateQueryStringParams ( SearchIndexRequest request )
46
+ {
47
+ return string . Join ( "&" , GenerateJsonCompatibleKeyValues ( request )
48
+ . Select ( kv => string . Format ( "{0}={1}" , kv . Key , UrlParam . Encode ( kv . Value ) ) ) ) ;
49
+ }
50
+
51
+ protected virtual IDictionary < string , string > GenerateJsonCompatibleKeyValues ( SearchIndexRequest request )
52
+ {
53
+ var kvs = new Dictionary < string , string > ( ) ;
54
+
55
+ if ( ! string . IsNullOrWhiteSpace ( request . Expression ) )
56
+ kvs . Add ( KeyNames . Expression , request . Expression ) ;
57
+
58
+ if ( request . HasSortings ( ) )
59
+ kvs . Add ( KeyNames . Sort , Serializer . ToJsonArray ( request . Sort . ToArray ( ) ) ) ;
60
+
61
+ if ( ! string . IsNullOrWhiteSpace ( request . Bookmark ) )
62
+ kvs . Add ( KeyNames . Bookmark , request . Bookmark ) ;
63
+
64
+ if ( request . Stale . HasValue )
65
+ kvs . Add ( KeyNames . Stale , request . Stale . Value . AsString ( ) ) ;
66
+
67
+ if ( request . Limit . HasValue )
68
+ kvs . Add ( KeyNames . Limit , Serializer . ToJson ( request . Limit . Value ) ) ;
69
+
70
+ if ( request . IncludeDocs . HasValue )
71
+ kvs . Add ( KeyNames . IncludeDocs , Serializer . ToJson ( request . IncludeDocs . Value ) ) ;
72
72
73
- // if (request.Ranges != null)
74
- // kvs.Add(KeyNames.Ranges, Serializer.Serialize(request.Ranges));
75
-
76
- // if (request.HasCounts())
77
- // kvs.Add(KeyNames.Counts, Serializer.ToJsonArray(request.Counts.ToArray()));
78
-
79
- // if (!string.IsNullOrWhiteSpace(request.GroupField))
80
- // kvs.Add(KeyNames.GroupField, request.GroupField);
81
-
82
- // if (request.GroupLimit.HasValue)
83
- // kvs.Add(KeyNames.GroupLimit, Serializer.ToJson(request.GroupLimit.Value));
84
-
85
- // if (request.HasGroupSortings())
86
- // kvs.Add(KeyNames.GroupSort, Serializer.ToJsonArray(request.GroupSort.ToArray()));
87
-
88
- // if (request.DrillDown.HasValue)
89
- // kvs.Add(KeyNames.DrillDown, Serializer.ToJsonArray(new[] { request.DrillDown.Value.Key, request.DrillDown.Value.Value }));
90
-
91
- // return kvs;
92
- // }
93
-
94
- // protected static class KeyNames
95
- // {
96
- // public const string Expression = "q";
97
- // public const string Sort = "sort";
98
- // public const string Bookmark = "bookmark";
99
- // public const string Stale = "stale";
100
- // public const string Limit = "limit";
101
- // public const string IncludeDocs = "include_docs";
102
- // public const string Ranges = "ranges";
103
- // public const string Counts = "counts";
104
- // public const string GroupField = "group_field";
105
- // public const string GroupLimit = "group_limit";
106
- // public const string GroupSort = "group_sort";
107
- // public const string DrillDown = "drilldown";
108
- // }
109
- // }
110
- // }
73
+ if ( request . Ranges != null )
74
+ kvs . Add ( KeyNames . Ranges , Serializer . Serialize ( request . Ranges ) ) ;
75
+
76
+ if ( request . HasCounts ( ) )
77
+ kvs . Add ( KeyNames . Counts , Serializer . ToJsonArray ( request . Counts . ToArray ( ) ) ) ;
78
+
79
+ if ( ! string . IsNullOrWhiteSpace ( request . GroupField ) )
80
+ kvs . Add ( KeyNames . GroupField , request . GroupField ) ;
81
+
82
+ if ( request . GroupLimit . HasValue )
83
+ kvs . Add ( KeyNames . GroupLimit , Serializer . ToJson ( request . GroupLimit . Value ) ) ;
84
+
85
+ if ( request . HasGroupSortings ( ) )
86
+ kvs . Add ( KeyNames . GroupSort , Serializer . ToJsonArray ( request . GroupSort . ToArray ( ) ) ) ;
87
+
88
+ if ( request . DrillDown . HasValue )
89
+ kvs . Add ( KeyNames . DrillDown , Serializer . ToJsonArray ( new [ ] { request . DrillDown . Value . Key , request . DrillDown . Value . Value } ) ) ;
90
+
91
+ return kvs ;
92
+ }
93
+
94
+ protected static class KeyNames
95
+ {
96
+ public const string Expression = "q" ;
97
+ public const string Sort = "sort" ;
98
+ public const string Bookmark = "bookmark" ;
99
+ public const string Stale = "stale" ;
100
+ public const string Limit = "limit" ;
101
+ public const string IncludeDocs = "include_docs" ;
102
+ public const string Ranges = "ranges" ;
103
+ public const string Counts = "counts" ;
104
+ public const string GroupField = "group_field" ;
105
+ public const string GroupLimit = "group_limit" ;
106
+ public const string GroupSort = "group_sort" ;
107
+ public const string DrillDown = "drilldown" ;
108
+ }
109
+ }
110
+ }
0 commit comments