diff --git a/MatrixDotNetLib/.vs/MatrixDotNetLib/v16/.suo b/MatrixDotNetLib/.vs/MatrixDotNetLib/v16/.suo index aab2068..4280dc4 100644 Binary files a/MatrixDotNetLib/.vs/MatrixDotNetLib/v16/.suo and b/MatrixDotNetLib/.vs/MatrixDotNetLib/v16/.suo differ diff --git a/MatrixDotNetLib/MatrixDotNetCmd/Program.cs b/MatrixDotNetLib/MatrixDotNetCmd/Program.cs index 36bd344..1442541 100644 --- a/MatrixDotNetLib/MatrixDotNetCmd/Program.cs +++ b/MatrixDotNetLib/MatrixDotNetCmd/Program.cs @@ -51,6 +51,8 @@ namespace MatrixDotNetCmd // stop once user hits Enter while (key.Key != ConsoleKey.Enter); + pass = pass.Trim(); + Console.WriteLine(); Console.WriteLine("Logging in..."); @@ -59,19 +61,20 @@ namespace MatrixDotNetCmd try { // create a new response object; trim password to remove added \r character - MatrixLoginResponse response = session.Login(server, user, pass.Trim()); + MatrixLoginResponse response = session.Login(server, user, pass); // notify user is logged in Console.WriteLine("Logged in!"); // assign globals globals.Token = response.Token; - session.Token = response.Token; globals.Server = server; - session.Server = server; globals.Session = response; - MatrixUserRooms rooms = session.GetRooms(globals.Token); + MatrixUserRooms rooms = session.GetRooms(); + + // prompt user to choose a room + // i guess we should find an alias for each room, if it exists Console.WriteLine(rooms.Joined[0]); } diff --git a/MatrixDotNetLib/MatrixDotNetCmd/obj/Debug/netcoreapp3.1/MatrixDotNetCmd.csprojAssemblyReference.cache b/MatrixDotNetLib/MatrixDotNetCmd/obj/Debug/netcoreapp3.1/MatrixDotNetCmd.csprojAssemblyReference.cache index be00802..f1a238b 100644 Binary files a/MatrixDotNetLib/MatrixDotNetCmd/obj/Debug/netcoreapp3.1/MatrixDotNetCmd.csprojAssemblyReference.cache and b/MatrixDotNetLib/MatrixDotNetCmd/obj/Debug/netcoreapp3.1/MatrixDotNetCmd.csprojAssemblyReference.cache differ diff --git a/MatrixDotNetLib/MatrixDotNetLib/MatrixApis.cs b/MatrixDotNetLib/MatrixDotNetLib/MatrixApis.cs index 5becb17..18f56df 100644 --- a/MatrixDotNetLib/MatrixDotNetLib/MatrixApis.cs +++ b/MatrixDotNetLib/MatrixDotNetLib/MatrixApis.cs @@ -7,120 +7,636 @@ namespace MatrixDotNetLib public class MatrixApis { - public class Server + public static class Server { + /// + /// Gets discovery information about the domain. + /// + /// + /// https://matrix.org/docs/api/client-server/#!/Server32administration/getWellknown + /// public static string Discovery = "/.well-known/matrix/client"; + + /// + /// Gets the versions of the specifications supported by the server. + /// + /// + /// https://matrix.org/docs/api/client-server/#!/Server32administration/getVersions + /// public static string Versions = "/_matrix/client/versions"; + + /// + /// Gets information about the server's supported feature set and other relevant capabilities. + /// + /// + /// https://matrix.org/docs/api/client-server/#!/Capabilities/getCapabilities + /// public static string Capabilities = "/_matrix/client/r0/capabilities"; + + /// + /// Gets information about a particular user. + /// + /// + /// https://matrix.org/docs/api/client-server/#!/Server32administration/getWhoIs + /// public static string Whois = "/_matrix/client/r0/admin/whois/{userid}"; + + /// + /// Performs a full text search across different categories. Posts with a search object. + /// + /// + /// https://matrix.org/docs/api/client-server/#!/Search/search + /// public static string Search = "/_matrix/client/r0/search"; + + /// + /// Performs a user search. Posts with a user search object. + /// + /// + /// https://matrix.org/docs/api/client-server/#!/User32data/searchUserDirectory + /// public static string UserSearch = "/_matrix/client/r0/user_directory/search"; + + /// + /// Lists the public rooms on the server. Posts with a filter object. + /// + /// + /// https://matrix.org/docs/api/client-server/#!/Room32discovery/getPublicRooms + /// public static string RoomList = "/_matrix/client/r0/publicrooms"; - public static string ManageRooms = "/_matrix/client/r0/directory/list/appservice/{networkid}/{roomid}"; - public static string UpgradeRoom = "/_matrix/client/r0/rooms/{roomid}/upgrade"; - public static string Openid = "/_matrix/client/r0/user/{userid}/openid/request_token"; - public static string Voip = "/_matrix/client/r0/voip/turnserver"; - } - public class Key - { - public static string Latest = "/_matrix/client/r0/keys/changes"; - public static string Claim = "/_matrix/client/r0/keys/claim"; - public static string Download = "/_matrix/client/r0/keys/query"; - public static string Upload = "/_matrix/client/r0/keys/upload"; - } + /// + /// Updates the visibility of a given room in the directory. Puts a visibility object. + /// + /// + /// https://matrix.org/docs/api/client-server/#!/Application32service32room32directory32management/updateAppserviceRoomDirectoryVsibility + /// + public static string ManageRooms = "/_matrix/client/r0/directory/list/appservice/{networkid}/{roomid}"; - public class Device - { - public static string Manage = "/_matrix/client/r0/devices"; - public static string MultiDelete = "/_matrix/client/r0/delete_devices"; - public static string Send = "/_matrix/client/r0/sendtodevice/{eventtype}/{txnid}"; - } + /// + /// Upgrades the given room to a particular room version. Posts with a room upgrade object. + /// + /// + /// https://matrix.org/docs/api/client-server/#!/Room32upgrades/upgradeRoom + /// + public static string UpgradeRoom = "/_matrix/client/r0/rooms/{roomid}/upgrade"; - public class Media - { - public static string Config = "/_matrix/media/r0/config"; - public static string Save = "/_matrix/media/r0/download/{servername}/{mediaid}"; - public static string Saveas = "/_matrix/media/r0/download/{servername}/{mediaid}/{filename}"; - public static string Preview = "/_matrix/media/r0/preview_url"; - public static string Thumb = "/_matrix/media/r0/thumbnail/{servername}/{mediaid}"; - public static string Upload = "/_matrix/media/r0/upload"; - } + /// + /// Gets an OpenID token object for use with another service. Posts with a userId parameter. + /// + /// + /// https://matrix.org/docs/api/client-server/#!/OpenID/requestOpenIdToken + /// + public static string Openid = "/_matrix/client/r0/user/{userid}/openid/request_token"; - public class Notifier - { - public static string Notifiers = "/_matrix/client/r0/notifications"; - public static string Pushers = "/_matrix/client/r0/pushers"; - public static string Set = "/_matrix/client/r0/pushers/set"; - public static string Rules = "/_matrix/client/r0/pushrules/"; + /// + /// Gets client credentials for use when initiating a call. + /// + /// + /// https://matrix.org/docs/api/client-server/#!/VOIP/getTurnServer + /// + public static string Voip = "/_matrix/client/r0/voip/turnserver"; } - public class NotifierRule - { - public static string Manage = "/_matrix/client/r0/pushrules/{scope}/{kind}/{ruleid}"; - public static string Actions = "/_matrix/client/r0/pushrules/{scope}/{kind}/{ruleid}/actions"; - public static string Toggle = "/_matrix/client/r0/pushrules/{scope}/{kind}/{ruleid}/enabled"; - } + public static class Key + { + /// + /// Gets a list of users who have updated their device identity keys. + /// + /// + /// https://matrix.org/docs/api/client-server/#!/End45to45end32encryption/getKeysChanges + /// + public static string Latest = "/_matrix/client/r0/keys/changes"; - public static class User - { - public static string Thirdparty = "/_matrix/client/r0/account/3pid"; - public static string Deactivate = "/_matrix/client/r0/account/deactivate"; - public static string Password = "/_matrix/client/r0/account/password"; - public static string Register = "/_matrix/client/r0/register"; - public static string WhoAmI = "/_matrix/client/r0/account/whoami"; - public static string Profile = "/_matrix/client/r0/profile/{userid}"; - public static string Avatar = "/_matrix/client/r0/profile/{userid}/avtatar_url"; - public static string DisplayName = "/_matrix/client/r0/profile/{userid}/displayname"; - public static string ExtraInfo = "/_matrix/client/r0/user/{userid}/account_data/{type}"; - public static string EventFilter = "/_matrix/client/r0/user/{userid}/filter"; - public static string Rooms = "/_matrix/client/r0/joined_rooms"; - public static string Login = "/_matrix/client/r0/login"; - public static string Logout = "/_matrix/client/r0/logout"; - public static string Status = "/_matrix/client/r0/presence/{userid}/status"; - } + /// + /// Claims one-time keys for use in pre-key messages. Posts with a key object. + /// + /// + /// https://matrix.org/docs/api/client-server/#!/End45to45end32encryption/claimKeys + /// + public static string Claim = "/_matrix/client/r0/keys/claim"; - public class UserRoom - { - public static string Extrainfo = "/_matrix/client/r0/user/{userid}/rooms/{roomid}/account_data/{type}"; - public static string Tags = "/_matrix/client/r0/user/{userid}/rooms/{roomid}/tags"; - } + /// + /// Returns the current devices and identity keys for the given users. Posts with a key object. + /// + /// + /// https://matrix.org/docs/api/client-server/#!/End45to45end32encryption/queryKeys + /// + public static string Download = "/_matrix/client/r0/keys/query"; - public class Room - { - public static string Create = "/_matrix/client/r0/createroom"; - public static string Aliases = "/_matrix/client/unstable/org.matrix.msc2432/rooms/{roomid}/aliases"; - public static string Active = "/_matrix/client/r0/rooms/{roomid}/joined_members"; - public static string Members = "/_matrix/client/r0/rooms/{roomid}/members"; - public static string Messages = "/_matrix/client/r0/rooms/{roomid}/messages"; - public static string Join = "/_matrix/client/r0/join/{roomidoralias}"; - public static string Ban = "/_matrix/client/r0/rooms/{roomid}/ban"; - public static string Forget = "/_matrix/client/r0/rooms/{roomid}/forget"; - public static string Invite = "/_matrix/client/r0/rooms/{roomid}/invite"; - public static string IdJoin = "/_matrix/client/r0/rooms/{roomid}/join"; - public static string Kick = "/_matrix/client/r0/rooms/{roomid}/kick"; - public static string Leave = "/_matrix/client/r0/rooms/{roomid}/leave"; - public static string Unban = "/_matrix/client/r0/rooms/{roomid}/unban"; - public static string Marker = "/_matrix/client/r0/rooms/{roomid}/read_markers"; - } + /// + /// Publishes end-to-end encryption keys for the device. Posts with a key object. + /// + /// + /// https://matrix.org/docs/api/client-server/#!/End45to45end32encryption/uploadKeys + /// + public static string Upload = "/_matrix/client/r0/keys/upload"; + } - public class RoomEvent - { - public static string Context = "/_matrix/client/r0/rooms/{roomid}/context/{eventid}"; - public static string Event = "/_matrix/client/r0/rooms/{roomid}/event/{eventid}"; - public static string Receipt = "/_matrix/client/r0/rooms/{roomid}/receipt/{receipttype}/{eventid}"; - public static string Redact = "/_matrix/client/r0/rooms/{roomid}/redact/{eventid}/{txnid}"; - public static string Send = "/_matrix/client/r0/rooms/{roomid}/send/{eventtype}/{txnid}"; - public static string Typing = "/_matrix/client/r0/rooms/{roomid}/typing/{userid}"; - public static string Flag = "/_matrix/client/r0/rooms/{roomid}/report/{eventid}"; - } + public static class Device + { + /// + /// Private string for device URL + /// + private static string _deviceIdUrl = "/_matrix/client/r0/devices/{deviceId}"; - public class RoomState - { - public static string List = "/_matrix/client/r0/rooms/{roomid}/state"; - public static string State = "/_matrix/client/r0/rooms/{roomid}/state/{eventtype}/{statekey}"; - } - } + /// + /// Gets information about all devices for the current user. + /// + /// + /// https://matrix.org/docs/api/client-server/#!/Device32management/getDevices + /// + public static string Manage = "/_matrix/client/r0/devices"; + + /// + /// Deletes the given device, and invalidates any access token associated with it. Deletes with interactive auth object and deviceId parameter. + /// + /// + /// https://matrix.org/docs/api/client-server/#!/Device32management/deleteDevice + /// + public static string Delete = _deviceIdUrl; + + /// + /// Gets information on a single device, by device id. Gets with deviceId parameter. + /// + /// + /// https://matrix.org/docs/api/client-server/#!/Device32management/getDevice + /// + public static string Info = _deviceIdUrl; + + /// + /// Updates the metadata (display name) on the given device. Puts a device metadata object with deviceId parameter. + /// + /// + /// https://matrix.org/docs/api/client-server/#!/Device32management/updateDevice + /// + public static string Update = _deviceIdUrl; + + /// + /// Deletes the given devices, and invalidates any access token associated with them. Posts with an interactive auth + device list object. + /// + /// + /// https://matrix.org/docs/api/client-server/#!/Device32management/deleteDevices + /// + public static string MultiDelete = "/_matrix/client/r0/delete_devices"; + + /// + /// Sends send-to-device events to the given devices. Puts a device message object with eventType and txnId parameters. + /// + /// + /// https://matrix.org/docs/api/client-server/#!/Send45to45Device32messaging/sendToDevice + /// + public static string Send = "/_matrix/client/r0/sendtodevice/{eventtype}/{txnid}"; + } + + public static class Media + { + + /// + /// + /// + /// + /// + /// + public static string Config = "/_matrix/media/r0/config"; + + /// + /// + /// + /// + /// + /// + public static string Save = "/_matrix/media/r0/download/{servername}/{mediaid}"; + + /// + /// + /// + /// + /// + /// + public static string Saveas = "/_matrix/media/r0/download/{servername}/{mediaid}/{filename}"; + + /// + /// + /// + /// + /// + /// + public static string Preview = "/_matrix/media/r0/preview_url"; + + /// + /// + /// + /// + /// + /// + public static string Thumb = "/_matrix/media/r0/thumbnail/{servername}/{mediaid}"; + + /// + /// + /// + /// + /// + /// + public static string Upload = "/_matrix/media/r0/upload"; + } + + public static class Notifier + { + + /// + /// + /// + /// + /// + /// + public static string Notifiers = "/_matrix/client/r0/notifications"; + + /// + /// + /// + /// + /// + /// + public static string Pushers = "/_matrix/client/r0/pushers"; + + /// + /// + /// + /// + /// + /// + public static string Set = "/_matrix/client/r0/pushers/set"; + + /// + /// + /// + /// + /// + /// + public static string Rules = "/_matrix/client/r0/pushrules/"; + } + + public static class NotifierRule + { + + /// + /// + /// + /// + /// + /// + public static string Manage = "/_matrix/client/r0/pushrules/{scope}/{kind}/{ruleid}"; + + /// + /// + /// + /// + /// + /// + public static string Actions = "/_matrix/client/r0/pushrules/{scope}/{kind}/{ruleid}/actions"; + + /// + /// + /// + /// + /// + /// + public static string Toggle = "/_matrix/client/r0/pushrules/{scope}/{kind}/{ruleid}/enabled"; + } + + public static class User + { + + /// + /// + /// + /// + /// + /// + public static string Thirdparty = "/_matrix/client/r0/account/3pid"; + + /// + /// + /// + /// + /// + /// + public static string Deactivate = "/_matrix/client/r0/account/deactivate"; + + /// + /// + /// + /// + /// + /// + public static string Password = "/_matrix/client/r0/account/password"; + + /// + /// + /// + /// + /// + /// + public static string Register = "/_matrix/client/r0/register"; + + /// + /// + /// + /// + /// + /// + public static string WhoAmI = "/_matrix/client/r0/account/whoami"; + + /// + /// + /// + /// + /// + /// + public static string Profile = "/_matrix/client/r0/profile/{userid}"; + + /// + /// + /// + /// + /// + /// + public static string Avatar = "/_matrix/client/r0/profile/{userid}/avtatar_url"; + + /// + /// + /// + /// + /// + /// + public static string DisplayName = "/_matrix/client/r0/profile/{userid}/displayname"; + + /// + /// + /// + /// + /// + /// + public static string ExtraInfo = "/_matrix/client/r0/user/{userid}/account_data/{type}"; + + /// + /// + /// + /// + /// + /// + public static string EventFilter = "/_matrix/client/r0/user/{userid}/filter"; + + /// + /// Gets a list of the user's current rooms. + /// + /// + /// https://matrix.org/docs/api/client-server/#!/Room32membership/getJoinedRooms + /// + public static string Rooms = "/_matrix/client/r0/joined_rooms"; + + /// + /// + /// + /// + /// + /// + public static string Login = "/_matrix/client/r0/login"; + + /// + /// + /// + /// + /// + /// + public static string Logout = "/_matrix/client/r0/logout"; + + /// + /// + /// + /// + /// + /// + public static string Status = "/_matrix/client/r0/presence/{userid}/status"; + } + + public static class UserRoom + { + + /// + /// + /// + /// + /// + /// + public static string Extrainfo = "/_matrix/client/r0/user/{userid}/rooms/{roomid}/account_data/{type}"; + + /// + /// + /// + /// + /// + /// + public static string Tags = "/_matrix/client/r0/user/{userid}/rooms/{roomid}/tags"; + } + + public static class Room + { + + /// + /// + /// + /// + /// + /// + public static string Create = "/_matrix/client/r0/createroom"; + + /// + /// + /// + /// + /// + /// + public static string Aliases = "/_matrix/client/unstable/org.matrix.msc2432/rooms/{roomid}/aliases"; + + /// + /// + /// + /// + /// + /// + public static string Active = "/_matrix/client/r0/rooms/{roomid}/joined_members"; + + /// + /// + /// + /// + /// + /// + public static string Members = "/_matrix/client/r0/rooms/{roomid}/members"; + + /// + /// + /// + /// + /// + /// + public static string Messages = "/_matrix/client/r0/rooms/{roomid}/messages"; + + /// + /// + /// + /// + /// + /// + public static string Join = "/_matrix/client/r0/join/{roomidoralias}"; + + /// + /// + /// + /// + /// + /// + public static string Ban = "/_matrix/client/r0/rooms/{roomid}/ban"; + + /// + /// + /// + /// + /// + /// + public static string Forget = "/_matrix/client/r0/rooms/{roomid}/forget"; + + /// + /// + /// + /// + /// + /// + public static string Invite = "/_matrix/client/r0/rooms/{roomid}/invite"; + + /// + /// + /// + /// + /// + /// + public static string IdJoin = "/_matrix/client/r0/rooms/{roomid}/join"; + + /// + /// + /// + /// + /// + /// + public static string Kick = "/_matrix/client/r0/rooms/{roomid}/kick"; + + /// + /// + /// + /// + /// + /// + public static string Leave = "/_matrix/client/r0/rooms/{roomid}/leave"; + + /// + /// + /// + /// + /// + /// + public static string Unban = "/_matrix/client/r0/rooms/{roomid}/unban"; + + /// + /// + /// + /// + /// + /// + public static string Marker = "/_matrix/client/r0/rooms/{roomid}/read_markers"; + } + + public static class RoomEvent + { + + /// + /// + /// + /// + /// + /// + public static string Context = "/_matrix/client/r0/rooms/{roomid}/context/{eventid}"; + + /// + /// + /// + /// + /// + /// + public static string Event = "/_matrix/client/r0/rooms/{roomid}/event/{eventid}"; + + /// + /// + /// + /// + /// + /// + public static string Receipt = "/_matrix/client/r0/rooms/{roomid}/receipt/{receipttype}/{eventid}"; + + /// + /// + /// + /// + /// + /// + public static string Redact = "/_matrix/client/r0/rooms/{roomid}/redact/{eventid}/{txnid}"; + + /// + /// + /// + /// + /// + /// + public static string Send = "/_matrix/client/r0/rooms/{roomid}/send/{eventtype}/{txnid}"; + + /// + /// + /// + /// + /// + /// + public static string Typing = "/_matrix/client/r0/rooms/{roomid}/typing/{userid}"; + + /// + /// + /// + /// + /// + /// + public static string Flag = "/_matrix/client/r0/rooms/{roomid}/report/{eventid}"; + } + + public static class RoomState + { + + /// + /// + /// + /// + /// + /// + public static string List = "/_matrix/client/r0/rooms/{roomid}/state"; + + /// + /// + /// + /// + /// + /// + public static string State = "/_matrix/client/r0/rooms/{roomid}/state/{eventtype}/{statekey}"; + } + } public class MatrixApiEntities { diff --git a/MatrixDotNetLib/MatrixDotNetLib/MatrixSessionManager.cs b/MatrixDotNetLib/MatrixDotNetLib/MatrixSessionManager.cs index 5bafd44..fd5aca6 100644 --- a/MatrixDotNetLib/MatrixDotNetLib/MatrixSessionManager.cs +++ b/MatrixDotNetLib/MatrixDotNetLib/MatrixSessionManager.cs @@ -1,6 +1,7 @@ using Newtonsoft.Json; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Net; using System.Net.Http; using System.Net.Http.Headers; @@ -13,10 +14,21 @@ namespace MatrixDotNetLib { public class MatrixSessionManager { + /// + /// Gets or sets the authentication token. + /// public string Token { get; set; } + /// + /// Gets or sets the Matrix homeserver FQDN. + /// public string Server { get; set; } + /// + /// Gets or sets the login session as a MatrixLoginResponse object. + /// + public MatrixLoginResponse LoginSession { get; set; } + /// /// Logs the user in with specified server and credentials. /// @@ -27,6 +39,9 @@ namespace MatrixDotNetLib /// public MatrixLoginResponse Login(string server, string user, string pass, string device = "") { + // set the object's server + this.Server = server; + MatrixLoginIdentifier userId = new MatrixLoginIdentifier() { IdType = "m.id.user", @@ -45,51 +60,34 @@ namespace MatrixDotNetLib theRequest.DeviceId = device; } - // create a web request - string loginUrl = "https://" + server + MatrixApis.User.Login; - // serialize object into JSON string requestJson = JsonConvert.SerializeObject(theRequest); - string responseJson = ApiResult(MatrixApis.User.Login, httpAction.GET, requestJson); + string responseJson = ApiResult(MatrixApis.User.Login, httpAction.POST, requestJson); MatrixLoginResponse response = JsonConvert.DeserializeObject(responseJson); - return response; - //// simplest implementation - //// might not work for UWP - //// sauce: https://stackoverflow.com/questions/5527316/how-to-set-the-content-of-an-httpwebrequest-in-c - //HttpContent requestContent = new StringContent(requestJson, Encoding.UTF8, "application/json"); + // we're logged in, so set the object's token + this.Token = response.Token; - //HttpClient client = new HttpClient(); - //client.BaseAddress = new Uri(loginUrl); - //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + // also set the object's session data + this.LoginSession = response; - //HttpResponseMessage responseMessage = client.PostAsync(loginUrl, requestContent).Result; - //string responseString = responseMessage.Content.ReadAsStringAsync().Result; - - //if(responseString.Contains("errcode")) - //{ - // // deserialize into error object - // MatrixError error = JsonConvert.DeserializeObject(responseString); + return response; + } - // // convert error object to a string - // string errMsg = error.ErrorCode + ": " + error.ErrorMessage; + /// + /// Gets the logged-in user's currently-joined rooms. + /// + /// + public MatrixUserRooms GetRooms() + { - // // throw exception (can be caught and handled gracefully) - // throw new Exception(errMsg); - //} - //else - //{ - // MatrixLoginResponse response = JsonConvert.DeserializeObject(responseString); + string responseJson = ApiResult(MatrixApis.User.Rooms, httpAction.GET); - // return response; - //} - } + MatrixUserRooms theRooms = JsonConvert.DeserializeObject(responseJson); - public MatrixUserRooms GetRooms(string token) - { - + return theRooms; } public enum httpAction @@ -109,10 +107,17 @@ namespace MatrixDotNetLib // replace all entities in param string url = "https://" + this.Server + api; - + HttpClient client = new HttpClient(); client.BaseAddress = new Uri(url); + // if an access token exists, create a bearer authorization header + if (this.Token != null) + { + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", this.Token); + + } + // init empty string string responseString = ""; @@ -143,6 +148,7 @@ namespace MatrixDotNetLib // get value of property through GetType().GetProperty().GetValue() // sauce: https://www.techrepublic.com/article/applied-reflection-dynamically-accessing-properties-of-a-class-at-runtime/ string newVal = entities.GetType().GetProperty(prop).GetValue(entities).ToString(); + // return URL with entities replaced and URL-encoded url = WebUtility.UrlEncode(url.Replace(oldVal, newVal)); } @@ -171,8 +177,6 @@ namespace MatrixDotNetLib responseMessage = client.PutAsync(url, requestContent).Result; break; } - - responseString = responseMessage.Content.ReadAsStringAsync().Result; } else { @@ -188,6 +192,8 @@ namespace MatrixDotNetLib } } + responseString = responseMessage.Content.ReadAsStringAsync().Result; + if (responseString.Contains("errcode")) { // deserialize into error object @@ -199,10 +205,48 @@ namespace MatrixDotNetLib // throw exception (can be caught and handled gracefully) throw new Exception(errMsg); } + else if (responseMessage.StatusCode != HttpStatusCode.OK) + { + // web server responded with an HTTP status code other than 200 OK + string errMsg = "Server returned " + (int)responseMessage.StatusCode + ": " + responseMessage.StatusCode.ToString(); + throw new Exception(errMsg); + } else { return responseString; } } } -} \ No newline at end of file +} + + + +//// simplest implementation +//// might not work for UWP +//// sauce: https://stackoverflow.com/questions/5527316/how-to-set-the-content-of-an-httpwebrequest-in-c +//HttpContent requestContent = new StringContent(requestJson, Encoding.UTF8, "application/json"); + +//HttpClient client = new HttpClient(); +//client.BaseAddress = new Uri(loginUrl); +//client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + +//HttpResponseMessage responseMessage = client.PostAsync(loginUrl, requestContent).Result; +//string responseString = responseMessage.Content.ReadAsStringAsync().Result; + +//if(responseString.Contains("errcode")) +//{ +// // deserialize into error object +// MatrixError error = JsonConvert.DeserializeObject(responseString); + +// // convert error object to a string +// string errMsg = error.ErrorCode + ": " + error.ErrorMessage; + +// // throw exception (can be caught and handled gracefully) +// throw new Exception(errMsg); +//} +//else +//{ +// MatrixLoginResponse response = JsonConvert.DeserializeObject(responseString); + +// return response; +//} \ No newline at end of file diff --git a/MatrixDotNetLib/MatrixDotNetLib/bin/Debug/netcoreapp3.1/MatrixDotNetLib.dll b/MatrixDotNetLib/MatrixDotNetLib/bin/Debug/netcoreapp3.1/MatrixDotNetLib.dll index b5d30dd..61bb19a 100644 Binary files a/MatrixDotNetLib/MatrixDotNetLib/bin/Debug/netcoreapp3.1/MatrixDotNetLib.dll and b/MatrixDotNetLib/MatrixDotNetLib/bin/Debug/netcoreapp3.1/MatrixDotNetLib.dll differ diff --git a/MatrixDotNetLib/MatrixDotNetLib/bin/Debug/netcoreapp3.1/MatrixDotNetLib.pdb b/MatrixDotNetLib/MatrixDotNetLib/bin/Debug/netcoreapp3.1/MatrixDotNetLib.pdb index 105b92b..77e9efc 100644 Binary files a/MatrixDotNetLib/MatrixDotNetLib/bin/Debug/netcoreapp3.1/MatrixDotNetLib.pdb and b/MatrixDotNetLib/MatrixDotNetLib/bin/Debug/netcoreapp3.1/MatrixDotNetLib.pdb differ diff --git a/MatrixDotNetLib/MatrixDotNetLib/obj/Debug/netcoreapp3.1/MatrixDotNetLib.csproj.CoreCompileInputs.cache b/MatrixDotNetLib/MatrixDotNetLib/obj/Debug/netcoreapp3.1/MatrixDotNetLib.csproj.CoreCompileInputs.cache index 143c214..17cb158 100644 --- a/MatrixDotNetLib/MatrixDotNetLib/obj/Debug/netcoreapp3.1/MatrixDotNetLib.csproj.CoreCompileInputs.cache +++ b/MatrixDotNetLib/MatrixDotNetLib/obj/Debug/netcoreapp3.1/MatrixDotNetLib.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -f16bca70409c8e2ae88277b1bedf2f151bb03767 +533436dc4ebb032189320843d6e1e024aef4bca8 diff --git a/MatrixDotNetLib/MatrixDotNetLib/obj/Debug/netcoreapp3.1/MatrixDotNetLib.csprojAssemblyReference.cache b/MatrixDotNetLib/MatrixDotNetLib/obj/Debug/netcoreapp3.1/MatrixDotNetLib.csprojAssemblyReference.cache index b872225..c7e9bdd 100644 Binary files a/MatrixDotNetLib/MatrixDotNetLib/obj/Debug/netcoreapp3.1/MatrixDotNetLib.csprojAssemblyReference.cache and b/MatrixDotNetLib/MatrixDotNetLib/obj/Debug/netcoreapp3.1/MatrixDotNetLib.csprojAssemblyReference.cache differ diff --git a/MatrixDotNetLib/MatrixDotNetLib/obj/Debug/netcoreapp3.1/MatrixDotNetLib.dll b/MatrixDotNetLib/MatrixDotNetLib/obj/Debug/netcoreapp3.1/MatrixDotNetLib.dll index b5d30dd..61bb19a 100644 Binary files a/MatrixDotNetLib/MatrixDotNetLib/obj/Debug/netcoreapp3.1/MatrixDotNetLib.dll and b/MatrixDotNetLib/MatrixDotNetLib/obj/Debug/netcoreapp3.1/MatrixDotNetLib.dll differ diff --git a/MatrixDotNetLib/MatrixDotNetLib/obj/Debug/netcoreapp3.1/MatrixDotNetLib.pdb b/MatrixDotNetLib/MatrixDotNetLib/obj/Debug/netcoreapp3.1/MatrixDotNetLib.pdb index 105b92b..77e9efc 100644 Binary files a/MatrixDotNetLib/MatrixDotNetLib/obj/Debug/netcoreapp3.1/MatrixDotNetLib.pdb and b/MatrixDotNetLib/MatrixDotNetLib/obj/Debug/netcoreapp3.1/MatrixDotNetLib.pdb differ