# Webservice integration

**URL:** <https://forums.plex.tv/t/webservice-integration/31295>\
**Category:** Dev/API Corner\
**Tags:** other-dev\
**Created:** [March 23, 2013, 8:10pm UTC](https://forums.plex.tv/t/webservice-integration/31295 "2013-03-23T20:10:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![robmathieson](https://avatars.discourse-cdn.com/v4/letter/r/cdc98d/32.png) [@robmathieson](https://forums.plex.tv/u/robmathieson)\
**Post date:** [March 23, 2013, 8:10pm UTC](https://forums.plex.tv/t/webservice-integration/31295/1 "2013-03-23T20:10:18Z")

</div>

Would be fantastic if Plex could expose a series of web services, which would allow data to be retrieved and actions to be called. At the moment I am a .NET/C# developer so am unable to quickly program in Python, but would love to integrate with the solution.

---

<div class="post-metadata">

**Author:** ![tester22](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@tester22](https://forums.plex.tv/u/tester22)\
**Post date:** [March 28, 2013, 7:27am UTC](https://forums.plex.tv/t/webservice-integration/31295/2 "2013-03-28T07:27:46Z")

</div>

> Would be fantastic if Plex could expose a series of web services, which would allow data to be retrieved and actions to be called. At the moment I am a .NET/C# developer so am unable to quickly program in Python, but would love to integrate with the solution.

  
Check out [http://127.0.0.1:32400/](http://127.0.0.1:32400/) and search the forums for docs.

---

<div class="post-metadata">

**Author:** ![Deathspike](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/deathspike/32/233756_2.png) [@Deathspike](https://forums.plex.tv/u/Deathspike)\
**Post date:** [April 6, 2013, 4:15pm UTC](https://forums.plex.tv/t/webservice-integration/31295/3 "2013-04-06T16:15:00Z")

</div>

Communication is simple with XML over HTTP or JSON over HTTP. The .NET framework has excellent facilities for XML out of the box, so I wrote a ‘Plex’ class that serves as a dynamic wrapper around the data. Here’s an usage example:

```
using (var wc = new WebClient()) {
wc.Encoding = Encoding.UTF8;
foreach (dynamic section in new Plex(wc.DownloadString(“[http://127.0.0.1:32400/library/sections](http://127.0.0.1:32400/library/sections)”))) {
Console.WriteLine(section.Title);
}
}
```
And the class that serves as dynamic wrapper:
```
using System;
using System.Collections;
using System.Dynamic;
using System.Linq;
using System.Xml.Linq;

namespace PlexLibraryToHtml {  
///

#  
/// Represents dynamic XML for Plex.  
/// 
public sealed class Plex : DynamicObject, IEnumerable {
///#  
/// Contains the attribute.  
/// 
private readonly XAttribute _Attribute;

```
	/// <summary>
	/// Contains the element.
	/// </summary>
	private readonly XElement _Element;

	#region Constructor
	/// <summary>
	/// Initialize a new instance of the Plex class.
	/// </summary>
	/// <param name="Document">The document.</param>
	public Plex(string Document) {
		// Set the element.
		_Element = XElement.Parse(Document);
	}

	/// <summary>
	/// Initialize a new instance of the Plex class.
	/// </summary>
	/// <param name="Attribute">The attribute.</param>
	public Plex(XAttribute Attribute) {
		// Set the attribute.
		_Attribute = Attribute;
	}

	/// <summary>
	/// Initialize a new instance of the Plex class.
	/// </summary>
	/// <param name="Element">The element.</param>
	public Plex(XElement Element) {
		// Set the element.
		_Element = Element;
	}
	#endregion

	#region DynamicObject
	/// <summary>
	/// Provides implementation for type conversion operations.
	/// </summary>
	/// <param name="Binder">Provides information about the conversion operation.</param>
	/// <param name="Result">The result of the type conversion operation.</param>
	public override bool TryConvert(ConvertBinder Binder, out object Result) {
		// Attempt the following code.
		try {
			// Change the type of the value to the binder type.
			Result = Convert.ChangeType(ToString(), Binder.ReturnType);
			// Return true.
			return true;
		} catch {
			// Initialize the default.
			Result = Activator.CreateInstance(Binder.ReturnType);
			// Return true.
			return true;
		}
	}

	/// <summary>
	/// Provides the implementation for operations that get member values.
	/// </summary>
	/// <param name="Binder">Provides information about the object that called the dynamic operation.</param>
	/// <param name="Result">The result of the get operation.</param>
	public override bool TryGetMember(GetMemberBinder Binder, out object Result) {
		// Check if the element is invalid.
		if (_Element == null) {
			// Set the result.
			Result = null;
			// Return true.
			return true;
		} else {
			// Retrieve the attribute.
			var Attribute = _Element.Attributes().FirstOrDefault(x => string.Equals(x.Name.LocalName, Binder.Name, StringComparison.InvariantCultureIgnoreCase));
			// Check if the attribute is invalid.
			if (Attribute == null) {
				// Set the result.
				Result = null;
				// Return true.
				return true;
			} else {
				// Set the result.
				Result = new Plex(Attribute);
				// Return true.
				return true;
			}
		}
	}
	#endregion

	#region Object
	/// <summary>
	/// Returns a string that represents the current object.
	/// </summary>
	public override string ToString() {
		// Return the attribute or element value.
		return _Attribute != null ? _Attribute.Value : _Element.Value;
	}
	#endregion

	#region IEnumerable
	/// <summary>
	/// Returns an enumerator that iterates through the collection.
	/// </summary>
	public IEnumerator GetEnumerator() {
		// Return the enumerator.
		return _Element.Elements().Select(x => new Plex(x)).GetEnumerator();
	}
	#endregion
}

```

}
```

---

<div class="post-metadata">

**Author:** ![robmathieson](https://avatars.discourse-cdn.com/v4/letter/r/cdc98d/32.png) [@robmathieson](https://forums.plex.tv/u/robmathieson)\
**Post date:** [April 6, 2013, 7:16pm UTC](https://forums.plex.tv/t/webservice-integration/31295/4 "2013-04-06T19:16:16Z")

</div>

Fantastic, thanks guys!!!!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/plex/original/3X/2/a/2acb9765406f63293d357b4ec509ec39aa28f2ad.png) [@system](https://forums.plex.tv/u/system)\
**Post date:** [December 20, 2019, 11:05pm UTC](https://forums.plex.tv/t/webservice-integration/31295/5 "2019-12-20T23:05:35Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
