by
7,382 32
0
7
2,932
1
Top 1% !
Popular
Famous
Tagged
Specified
Refined
OpenSource
Popularity: 5335th place
Created
Modified Sep 18, 2013

Published on:

.NETFramework4.0
Architecturex86_64
Extension.cs
InputModifier1Out
InputName0resp
InputName1totNumWithNonDefOp
InputType0S2CResListBaseEntity<Snippet>
InputType1int
LanguageCSharp
LicenseMIT_X11
MethodNameGetMiscFromResp
OSWindows
OutputTypeDictionary<string, string[]>
Windows VersionWindows7

Search snippets with Snip2Code APIs

This method calls Snip2Code web service to search for the snippets matching the given filters. This method can be called both as anonymous user (without having previously logged-in, in this case the results will be only public snippets) or as logged user (in this case the results will be found in the basket of user's visible snippets).
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/11206/Search-snippets-with-Snip2Code-APIs?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
public IList<Snippet> GetSnippetsForSearch(string searchText, out Dictionary<string, string[]> misSpellings, out int totNum, out int totNumWithNonDefOp, int maxNum = int.MaxValue, int start = 0, bool onlyCreated = false, int onlyOfGroup = 0) { //check for erroneous input: misSpellings = null; totNum = 0; totNumWithNonDefOp = 0; if ((maxNum <= 0) || (start < 0)) { log.ErrorFormat("Input error: userID={0}, searchText={1}, maxNum={2}, start={3}", CurrentUserID, searchText.PrintNull(), maxNum, start); return new List<Snippet>(); } //send the request and parse the response: string querystring = string.Format("query={0}&maxNum={1}&start={2}&onlyCreated={3}&onlyOfGroup={4}", HttpUtility.UrlEncode(searchText), maxNum, start, onlyCreated, onlyOfGroup); string SEARCH_SNIPPET_URL = "http://www.snip2code.com/Search/Get"; string response = SendRequest(SEARCH_SNIPPET_URL, querystring, false); S2CResListBaseEntity<Snippet> resp = Deserialize(response); //you can use a third party library to pass from json-encoded response to the object... //build the result: if ((resp == null) || (resp.Data == null)) return null; IList<Snippet> snips = resp.Data; totNum = resp.TotNum; misSpellings = resp.GetMiscFromResp(out totNumWithNonDefOp); return snips; } public static Dictionary<string, string[]> GetMiscFromResp(this S2CResListBaseEntity<Snippet> resp, out int totNumWithNonDefOp) { totNumWithNonDefOp = 0; if ((resp == null) || (resp.Misc == null)) return null; else { Dictionary<string, string[]> res = new Dictionary<string, string[]>(); foreach (string key in resp.Misc.Keys) { if (string.IsNullOrEmpty(key) || (resp.Misc[key] == null)) continue; if (key.Equals(TOT_NUM_WITH_NON_DEF_OP, StringComparison.InvariantCultureIgnoreCase)) int.TryParse(resp.Misc[key], out totNumWithNonDefOp); else res.Add(key, resp.Misc[key].Split(',')); } return res; } }

Other Steps and Solutions

If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus