OpenSource » SampleCode » NexportWebApiMvcSampleSite Sample MVC 4 Website project that demonstrates the use of the NexPort WebApi
Clone URL:  
Pushed to one repository · View In Graph Contained in tip

Added comments and EditUserEmail.

Changeset e50cb9741a7f

Parent 138874f56881

by Profile picture of User 240Clayton Miller <clayton.miller@nexportengineering.com>

Changes to 25 files · Browse files at e50cb9741a7f Showing diff from parent 138874f56881 Diff from another changeset...

 
 
 
 
 
 
 
 
 
3
4
5
 
 
 
6
7
8
 
54
55
56
 
 
 
 
 
 
 
 
 
 
57
 
3
4
5
6
7
8
9
10
11
 
57
58
59
60
61
62
63
64
65
66
67
68
69
70
@@ -3,6 +3,9 @@
   namespace NexportWebApiDemo.ApiModels.v1  { + //Each of these classes should reflect the request/response objects for the WebApi methods you intend to call + //You can choose to only implement particular datamembers, but the names MUST match the names in the online documentation +   public class WAAuthTokenRequest   {   public string username { get; set; } @@ -54,4 +57,14 @@
  public TimeSpan? access_time_limit { get; set; }   public int publishing_model { get; set; }   } + + /// <summary> + /// This is an example of a partially implemented class, useful for calling EditUser when you only want + /// to change the email. This way, you don't need to have every single datamember available. + /// </summary> + public class EditUserEmailRequest + { + public string login { get; set; } + public string email { get; set; } + }  }
 
8
9
10
 
 
 
 
 
11
12
13
14
15
 
16
 
17
 
18
19
20
 
21
 
22
23
 
24
 
25
26
27
 
 
 
 
 
 
 
 
28
29
 
 
 
30
31
32
 
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
@@ -8,25 +8,48 @@
 {   public class HomeController : Controller   { + /// <summary> + /// Normally you wouldn't put all of this code in the Index action, but this should be + /// acceptable for a demo environment + /// </summary> + /// <returns></returns>   public ActionResult Index()   {   ViewBag.Title="NexportWebApiDemo";   var authRequest = new WAAuthTokenRequest()   { + //You'll need to change the username to your own   username = "Clay0M", + //This should ALWAYS be "password"   grant_type = "password", + //This should be YOUR password   password = "password"   };   + //Need to authenticate before we can make any requests   var authToken = WebApiRequests.Authenticate(authRequest); +   var catRequest = new CatalogRequest()   { + //You'll need to change this to the ID of the organization you want catalogs for   group_id = new Guid("ed5ba4ac-73e4-4e64-837f-b26cb8ef3c4b"), + //AvailableToMembers   publishing_model = 0   };   + var editEmailRequest = new EditUserEmailRequest + { + //You'll need to change the login to the desired user login + login = "user58897", + //Must be valid format (foo@bar.baz) + email = "newemail@email.com" + }; +   var catResponse = WebApiRequests.GetCatalogs(catRequest, authToken.access_token);   var catalogs = catResponse.catalogs.ToList(); + //Uncomment the next line to change the email + //WebApiRequests.EditUserEmail(editEmailRequest, authToken.access_token); + //Throw the results of GetCatalogs into the view   ViewBag.Catalogs = catalogs;   return View();   }
 
 
 
81
82
83
 
84
85
86
 
96
97
98
 
99
100
101
 
81
82
83
84
85
86
87
 
97
98
99
100
101
102
103
@@ -81,6 +81,7 @@
  <th>Catalog</th>   <th>Organization</th>   <th>Date Created</th> + <!--You can add additional columns here, like Owner Name-->   </tr>   </thead>   <tbody> @@ -96,6 +97,7 @@
  <td>   @catalog.date_created   </td> + <!--Check CatalogResponseItem fields for other datamembers (such as catalog.owner_name)-->   </tr>   }   </tbody>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
5
6
 
 
 
 
7
8
9
 
66
67
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
70
 
4
5
6
7
8
9
10
11
12
13
 
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
@@ -4,6 +4,10 @@
   namespace NexportWebApiDemoClient  { + /// <summary> + /// This is where all of the methods that call the WebApi methods live. The idea is to take the request information + /// and an access token, and call the WebApi method from its URL. + /// </summary>   public class WebApiRequests   {   private const string BaseUri = "http://local.nexportcampus.com/api/v1/"; @@ -66,5 +70,22 @@
  return response.Content.ReadAsStringAsync().Result;   }   } + + /// <summary> + /// Example of partial EditUser implementation. Instead of editing all the fields, we only want to change the + /// email. Instead of having a different WebApi method to do this, simply make a method to call EditUser that + /// only passes the email in. + /// </summary> + /// <param name="request">Contains fields from EditUserRequest from WebApi</param> + /// <param name="accessToken"></param> + public static void EditUserEmail(EditUserEmailRequest request, Guid accessToken) + { + using (var client = new HttpClient()) + { + var requestUri = BaseUri + "AdminApi/EditUser?access_token=" + accessToken; + //While it may seem weird that I'm storing the result and not using it, this is actually necessary + var result = client.PostAsJsonAsync(requestUri, request).Result; + } + }   }  }