Skip to content

Instantly share code, notes, and snippets.

@jeriley jeriley/V1Api.cls
Last active Aug 29, 2015

Embed
What would you like to do?
Example of Apex code for sending a Story to VersionOne
public class V1Api {
private V1BasicConnector;
public V1Api(){
V1BasicConnector = new V1BasicConnector('https://www99.host.com/myInstance', 'admin', 'admin');
}
public void CreateStory(V1Story story)
{
Dom.Document doc = new Dom.Document();
dom.XmlNode root = doc.createRootElement('Asset', null, null);
for (V1ApiXmlSetterNode node: story.XmlNodes())
{
node.AddSetterAttributeNode(root);
}
String body = doc.toXmlString();
system.debug(body);
V1BasicConnector.HttpPost('Story', body);
}
}
public interface V1ApiXmlSetterNode {
void AddSetterAttributeNode(Dom.XmlNode assetRoot);
}
public class V1AttributeSetMultiRelationNode implements V1ApiXmlSetterNode {
public V1AttributeSetMultiRelationNode(String relationName, List<String> values)
{
this.RelationName = relationName;
this.Values = values;
}
public String RelationName {get; private set;}
public List<String> Values {get; private set;}
public void AddSetterAttributeNode(Dom.XmlNode assetRoot)
{
if (this.Values.size() == 0)
return;
Dom.Xmlnode node = assetRoot.addChildElement('Relation', null, null);
node.setAttribute('name', RelationName);
for (String oid: Values)
{
Dom.Xmlnode assetRef = node.addChildElement('Asset', null, null);
assetRef.setAttribute('idref', oid);
assetRef.setAttribute('act', 'add');
}
}
}
public class V1AttributeSetNode implements V1ApiXmlSetterNode {
public V1AttributeSetNode(String attributeName, String value)
{
this.AttributeName = attributeName;
this.Value = value;
}
public String AttributeName {get; private set;}
public String Value {get; private set;}
public void AddSetterAttributeNode(Dom.XmlNode assetRoot)
{
if (this.Value == null)
return;
Dom.Xmlnode node = assetRoot.addChildElement('Attribute', null, null);
node.setAttribute('act', 'set');
node.setAttribute('name', AttributeName);
node.addTextNode(Value);
}
}
public class V1AttributeSetRelationLookupNode implements V1ApiXmlSetterNode {
public V1AttributeSetRelationLookupNode(String relationToType, String value, Map<String, String> lookup)
{
this.RelationToType = relationToType;
this.Value = value;
this.Lookup = lookup;
}
public String RelationToType {get; private set;}
public String Value {get; private set;}
public Map<String, String> Lookup {get; private set;}
public void AddSetterAttributeNode(Dom.XmlNode assetRoot)
{
if (this.Value == null || lookup.size() == 0)
return;
if (!Lookup.containsKey(Value)) // possible error condition?
return;
Dom.Xmlnode node = assetRoot.addChildElement('Relation', null, null);
node.setAttribute('act', 'set');
node.setAttribute('name', RelationToType);
Dom.Xmlnode assetRef = node.addChildElement('Asset', null, null);
assetRef.setAttribute('idref', Lookup.get(Value));
}
}
public class V1BasicConnector {
private string auth64;
private string instance;
public static final String DATA_URL = '/rest-1.v1/Data/';
public V1BasicConnector(string instanceRoot, string user, string pass){
instance = instanceRoot;
auth64 = EncodingUtil.base64Encode(Blob.valueof(user + ':' + pass));
}
public HttpResponse HttpGet(string query)
{
string apiQuery = instance + DATA_URL + query;
HttpRequest request = new HttpRequest();
request.setMethod('GET');
request.setEndpoint(apiQuery);
request.setHeader('Authorization', 'Basic ' + auth64);
system.debug(request.getEndpoint());
Http http = new Http();
HttpResponse response = http.send(request);
return response;
}
public HttpResponse HttpPost(string assetSlashId, string xmlPayload)
{
string apiQuery = instance + DATA_URL + assetSlashId;
HttpRequest request = new HttpRequest();
request.setMethod('POST');
request.setEndpoint(apiQuery);
request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
request.setHeader('Authorization', 'Basic ' + auth64);
request.setBody(xmlPayload);
request.setHeader('Content-Length', String.valueOf(xmlPayload.length()));
system.debug(request.getEndpoint());
Http http = new Http();
HttpResponse response = http.send(request);
return response;
}
}
public class V1Story {
public V1Story()
{
FillMappings();
}
private Map<String, String> PriorityLookup;
private void FillMappings()
{
PriorityLookup = new Map<String, String>(); //should this be file-driven
PriorityLookup.put('High', 'StoryPriority:200');
PriorityLookup.put('Medium', 'StoryPriority:201');
PriorityLookup.put('Low', 'StoryPriority:202');
}
public String Title {get; set;}
public String Description {get; set;}
public String Scope {get; set;}
public String Category {get; set;}
public String Priority {get;set;}
public List<V1ApiXmlSetterNode> XmlNodes()
{
List<V1ApiXmlSetterNode> nodes = new List<V1ApiXmlSetterNode>();
nodes.add(new V1AttributeSetNode('Name', Title));
nodes.add(new V1AttributeSetNode('Description', Description));
nodes.add(new V1AttributeSetRelationNode('Scope', 'Scope:1003'));
nodes.add(new V1AttributeSetRelationNode('Category', StoryTypeCategoryID));
nodes.add(new V1AttributeSetRelationLookupNode('Priority', Priority, PriorityLookup));
nodes.add(new V1AttributeSetNode('Custom_MyCustomField', customField));
return nodes;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.