-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
144 lines (127 loc) · 5.34 KB
/
Program.cs
File metadata and controls
144 lines (127 loc) · 5.34 KB
1
2
3
4
5
6
7
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using RestSharp;
using S9APIUploadDemo.Models;
namespace S9APIUploadDemo
{
class Program
{
static RestClient ApiClient = new RestClient("http://localhost/Square9Api"); //path to the website where the api is hosted
static void Main(string[] args)
{
//basic authentication
ApiClient.Authenticator = new HttpBasicAuthenticator("S9UserName", "S9Password");
var databaseID = 1; //Database ID
var archiveID = 3; //Archive ID
var localFileName = "tester.pdf"; //file to be uploaded
var token = "";
try
{
//upload file
string uploadedFileName = PostFile(localFileName); //api returns the name of the file on the server for indexing
//valid SS license
token = GetLicense();
//some field data
List<FieldItem> fieldData = new List<FieldItem>() { new FieldItem(1, "uploader1"), new FieldItem(2, "uploader2") };
//index file
IndexDocument(databaseID, archiveID, fieldData, uploadedFileName, token);
Console.WriteLine(localFileName + " Uploaded Successfully.");
Console.Read();
}
catch (Exception ex)
{
Console.WriteLine("Unable to upload file:\n " + ex.Message);
Console.Read();
}
finally
{
if (!String.IsNullOrEmpty(token))
{
ReleaseLicense(token);
}
}
}
static String PostFile(String fileToPost)
{
String uploadedFileName = String.Empty;
var request = new RestRequest("api/files/", Method.POST);
request.AddFile("File", fileToPost);
var response = ApiClient.Execute(request);
if (response.StatusCode != HttpStatusCode.OK)
{
throw new Exception("Unable to upload file: " + response.Content);
}
//for some reason the restsharp deserializer is having trouble with this object
var uploadedFiles = JsonConvert.DeserializeObject<UploadedFileList>(response.Content);
uploadedFileName = uploadedFiles.files[0].name;
return uploadedFileName;
}
static void IndexDocument(Int32 DatabaseID, Int32 ArchiveID, List<FieldItem> FieldData, String UploadedFileName, String Token)
{
//load up our data
var indexData = new Indexer();
indexData.files.Add(new File(UploadedFileName));
if (FieldData != null && FieldData.Count > 0)
{
foreach (FieldItem field in FieldData)
{
var fieldItem = new Field(field.ID.ToString(), field.VAL);
indexData.fields.Add(fieldItem);
}
}
var request = new RestRequest("api/dbs/{db}/archives/{arch}?token={token}", Method.POST);
request.AddParameter("token", Token, ParameterType.UrlSegment); //have to specifiy type on POST
request.AddParameter("db", DatabaseID, ParameterType.UrlSegment);
request.AddParameter("arch", ArchiveID, ParameterType.UrlSegment);
request.RequestFormat = DataFormat.Json;
request.AddBody(indexData);
var response = ApiClient.Execute(request);
if (response.StatusCode != HttpStatusCode.OK)
{
throw new Exception("Unable to index document: " + response.Content);
}
}
static String GetLicense()
{
var request = new RestRequest("api/licenses");
var license = ApiClient.Execute<License>(request);
if (license.StatusCode != HttpStatusCode.OK)
{
if (license.StatusCode == HttpStatusCode.Unauthorized)
{
throw new Exception("Unable to get a License: The passed user is Unauthorized.");
}
else if (license.StatusCode == HttpStatusCode.BadRequest)
{
throw new Exception("Unable to get a License: " + license.Content);
}
else if (license.StatusCode == HttpStatusCode.NotFound)
{
throw new Exception("Unable to get a License: Unable to connect to the license server, server not found.");
}
else
{
throw new Exception("Unable to get a License: " + license.Content);
}
}
return license.Data.Token;
}
static void ReleaseLicense(String Token)
{
var request = new RestRequest("api/licenses/" + Token);
var response = ApiClient.Execute(request);
if (response.ErrorException != null)
{
throw new Exception("Unable to release license token. ", response.ErrorException);
}
else if (response.StatusCode != HttpStatusCode.OK)
{
throw new Exception("Unable to release license token. " + response.Content);
}
}
}
}