diff --git a/docs/usage/queues.rst b/docs/usage/queues.rst new file mode 100644 index 0000000..e0f7d15 --- /dev/null +++ b/docs/usage/queues.rst @@ -0,0 +1,115 @@ +.. module:: twilio.rest.resources + +============================== +Queues and Members +============================== + +For more information, see the +`Queue REST Resource `_ +and `Member REST Resource `_ +documentation. + + +Listing Queues +----------------------- + +.. code-block:: javascript + + String ACCOUNT_SID = 'XXXXXXXXXXXXXXX'; + String AUTH_TOKEN = 'XXXXXXXXXXXXXXX'; + TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN); + + List params = new List(); + params.add(new TwilioNameValuePair('FriendlyName','TestQueue')); + + TwilioQueue queue = client.getAccount().getQueues().create(params); + + +List All Available Queues +---------------------- + + +.. code-block:: javascript + + String ACCOUNT_SID = 'XXXXXXXXXXXXXXX'; + String AUTH_TOKEN = 'XXXXXXXXXXXXXXX'; + TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN); + + TwilioQueueList queues = client.getAccount().getQueues(); + + for(TwilioQueue queue :queues.getPageData()) + { + system.debug(queue); + } + + +Retrieving Queue information using QueueId +---------------------- + + +.. code-block:: javascript + + String ACCOUNT_SID = 'XXXXXXXXXXXXXXX'; + String AUTH_TOKEN = 'XXXXXXXXXXXXXXX'; + TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN); + TwilioQueue queue = client.getAccount().getQueue('YYYYYYYYYYYYYYYYY'); + + system.debug(queue); + +Updating Queue Properties +---------------------- + + +.. code-block:: javascript + + String ACCOUNT_SID = 'XXXXXXXXXXXXXXX'; + String AUTH_TOKEN = 'XXXXXXXXXXXXXXX'; + TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN); + TwilioQueue queue = client.getAccount().getQueue('YYYYYYYYYYYYYYYYY'); + + List params = new List(); + params.add(new TwilioNameValuePair('MaxSize','120')); + + queue.updateResource(params); + + + +List of Members in a queue +---------------------- + + +.. code-block:: javascript + + String ACCOUNT_SID = 'XXXXXXXXXXXXXXX'; + String AUTH_TOKEN = 'XXXXXXXXXXXXXXX'; + TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN); + TwilioMemberList members = client.getAccount().getQueue('QUcdbadab1b3de41f39f2257395e9b80a9').getMembers(); + + system.debug('Members :'+members); + +Getting a specific Queue Member +---------------------- + + +.. code-block:: javascript + + String ACCOUNT_SID = 'XXXXXXXXXXXXXXX'; + String AUTH_TOKEN = 'XXXXXXXXXXXXXXX'; + TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN); + TwilioMember member = client.getAccount().getQueue('QUcdbadab1b3de41f39f2257395e9b80a9').getMember('ZZZZZZZZZZZZZZZZZ'); + +Updating Member information +---------------------- + + +.. code-block:: javascript + + String ACCOUNT_SID = 'XXXXXXXXXXXXXXX'; + String AUTH_TOKEN = 'XXXXXXXXXXXXXXX'; + TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN); + //need to have callSid + TwilioMember member = client.getAccount().getQueue('QUcdbadab1b3de41f39f2257395e9b80a9').getMember('Front'); + List params = new List(); + params.add(new TwilioNameValuePair('URL','http://demo.twilio.com/docs/voice.xml')); + params.add(new TwilioNameValuePair('Method','POST')); + member.updateResource(params); diff --git a/install/build.properties b/install/build.properties index 9aac4e4..e39f4ee 100644 --- a/install/build.properties +++ b/install/build.properties @@ -18,4 +18,3 @@ sf.serverurl = https://login.salesforce.com # If your network requires an HTTP proxy, you'll configuration instructions # at http://ant.apache.org/manual/proxy.html - diff --git a/src/classes/TwilioAccount.cls b/src/classes/TwilioAccount.cls index aa7576c..49bb7c9 100644 --- a/src/classes/TwilioAccount.cls +++ b/src/classes/TwilioAccount.cls @@ -210,6 +210,26 @@ global class TwilioAccount extends TwilioResource.InstanceResource { return message; } + public TwilioQueueList getQueues() + { + TwilioQueueList Queuelist = new TwilioQueueList(this.getClient(), filters); + Queuelist.setRequestAccountSid(this.getRequestAccountSid()); + return Queuelist; + } + + /** + * Get a given queue by sid + * + * @param sid The Sid starting with QU + * @return the TwilioQueue object + */ + public TwilioQueue getQueue(String sid) + { + TwilioQueue queue = new TwilioQueue(this.getClient(), sid); + queue.setRequestAccountSid(this.getRequestAccountSid()); + return queue; + } + /** * Suspend a subaccount. * diff --git a/src/classes/TwilioAccountList.cls b/src/classes/TwilioAccountList.cls index 87c66bc..d9358e8 100644 --- a/src/classes/TwilioAccountList.cls +++ b/src/classes/TwilioAccountList.cls @@ -23,59 +23,62 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ global class TwilioAccountList extends TwilioResource.ListResource - implements Iterable { - - /** - * Instantiates a new account list. - * - * @param client the client - */ - public TwilioAccountList(TwilioRestClient client) { - super(client); - } - - /** - * Instantiates a new call list. - * - * @param client the client - * @param filters the filters - */ - public TwilioAccountList(TwilioRestClient client, Map filters) { - super(client, filters); - } + implements Iterable { + + /** + * Instantiates a new account list. + * + * @param client the client + */ + public TwilioAccountList(TwilioRestClient client) { + super(client); + } + + /** + * Instantiates a new call list. + * + * @param client the client + * @param filters the filters + */ + public TwilioAccountList(TwilioRestClient client, Map filters) { + super(client, filters); + } - /* implements method from TwilioResource */ - public override String getResourceLocation() { - return '/' + TwilioRestClient.DEFAULT_VERSION + '/Accounts.json'; - } + /* implements method from TwilioResource */ + public override String getResourceLocation() { + return '/' + TwilioRestClient.DEFAULT_VERSION + '/Accounts.json'; + } - /* implements method from TwilioResource.ListResource */ - public override Object makeNew(TwilioRestClient client, - Map params) { - return new TwilioAccount(client, params); - } + /* implements method from TwilioResource.ListResource */ + public override Object makeNew(TwilioRestClient client, + Map params) { + return new TwilioAccount(client, params); + } - /* implements method from TwilioResource.ListResource */ - public override String getListKey() { - return 'accounts'; - } - - public TwilioAccount create(Map params) { - return (TwilioAccount)this.createInstance(params); - } - - public List getPageData() { - List returnList = new List(); - - for (Object o : this.getObjectPageData()) { - if (o instanceof TwilioAccount) { - returnList.add((TwilioAccount) o); - } - } - return returnList; - } - - global Iterator iterator() { - return new TwilioIterator.AccountIterator(this); - } + /* implements method from TwilioResource.ListResource */ + public override String getListKey() { + return 'accounts'; + } + + public TwilioAccount create(Map params) { + return (TwilioAccount)this.createInstance(params); + } + public TwilioAccount create(List params) { + return (TwilioAccount)this.createInstance(params); + } + + public List getPageData() { + List returnList = new List(); + + for (Object o : this.getObjectPageData()) { + if (o instanceof TwilioAccount) { + returnList.add((TwilioAccount) o); + } + } + return returnList; + } + + global Iterator iterator() { + return new TwilioIterator.AccountIterator(this); + } } \ No newline at end of file diff --git a/src/classes/TwilioCapability.cls b/src/classes/TwilioCapability.cls index 1dc0281..2010da3 100644 --- a/src/classes/TwilioCapability.cls +++ b/src/classes/TwilioCapability.cls @@ -142,6 +142,7 @@ global class TwilioCapability { return jwtEncode(payload, this.authToken); } + /* PRIVATE METHODS */ /** @@ -325,4 +326,4 @@ global class TwilioCapability { } private class TestOnlyException extends Exception {} -} +} \ No newline at end of file diff --git a/src/classes/TwilioIterator.cls b/src/classes/TwilioIterator.cls index b343b20..f62068a 100644 --- a/src/classes/TwilioIterator.cls +++ b/src/classes/TwilioIterator.cls @@ -218,6 +218,26 @@ global class TwilioIterator { } } + global class MemberIterator extends ObjectIteratorSupport implements Iterator { + global MemberIterator(TwilioMemberList twlist) { + super(twlist); + } + + global TwilioMember next() { + return (TwilioMember)getNext(); + } + } + + global class QueueIterator extends ObjectIteratorSupport implements Iterator { + global QueueIterator(TwilioQueueList twlist) { + super(twlist); + } + + global TwilioQueue next() { + return (TwilioQueue)getNext(); + } + } + global class TranscriptionIterator extends ObjectIteratorSupport implements Iterator { global TranscriptionIterator(TwilioTranscriptionList twlist) { super(twlist); diff --git a/src/classes/TwilioMedia.cls b/src/classes/TwilioMedia.cls index 7dc6d71..f612dd7 100644 --- a/src/classes/TwilioMedia.cls +++ b/src/classes/TwilioMedia.cls @@ -129,17 +129,6 @@ global class TwilioMedia extends TwilioResource.InstanceResource return this.getProperty('content_type'); } - /** - * Gets the duration - * - * @return the duration - */ - public Integer getDuration() - { - Integer duration = (Integer) this.getObject('duration'); - return duration; - } - /** * Gets the api version. * diff --git a/src/classes/TwilioMediaList.cls b/src/classes/TwilioMediaList.cls index 3356f43..ba0081f 100644 --- a/src/classes/TwilioMediaList.cls +++ b/src/classes/TwilioMediaList.cls @@ -65,8 +65,19 @@ global class TwilioMediaList extends TwilioResource.ListResource implements Iter return this.requestMessageSid; } + public List getPageData() { + List returnList = new List(); + + for (Object o : this.getObjectPageData()) { + if (o instanceof TwilioMedia) { + returnList.add((TwilioMedia) o); + } + } + return returnList; + } global Iterator iterator() { return new TwilioIterator.MediaIterator(this); } + } \ No newline at end of file diff --git a/src/classes/TwilioMember.cls b/src/classes/TwilioMember.cls index 5115734..511cdc9 100644 --- a/src/classes/TwilioMember.cls +++ b/src/classes/TwilioMember.cls @@ -1,3 +1,75 @@ -global class TwilioMember { +global class TwilioMember extends TwilioResource.InstanceResource +{ + // Constants + private final static String URL = 'Url'; + private final static String METHOD = 'Method'; + private final static String QUEUE_SID = 'queue_sid'; + private final static String CALL_SID = 'call_sid'; + private final static String DATE_ENQUEUED = 'date_enqueued'; + private final static String WAIT_TIME = 'wait_time'; + private final static String POSITION = 'position'; + + public final static String QUEUE_FRONT = 'Front'; + + //constructor + public TwilioMember(TwilioRestClient client, String queueSid, String callSid) + { + super(client); + this.setProperty(QUEUE_SID, queueSid); + this.setProperty(CALL_SID,callSid); + } + + public TwilioMember(TwilioRestClient client,String queueSid) + { + this(client,queueSid,QUEUE_FRONT); + } + public TwilioMember(TwilioRestClient client,Map properties,String queueSid) { + super(client, properties); + this.setProperty(QUEUE_SID, queueSid); + } + + public String getQueueSid() { + return this.getProperty(QUEUE_SID); + } + + public String getCallSid() { + return this.getProperty(CALL_SID); + } + + public Datetime getDateEnqueued() + { + return this.getPropertyDateTime(DATE_ENQUEUED); + } + + public String getWaitTime() { + return this.getProperty(WAIT_TIME); + } + + public String getPosition() { + return this.getProperty(POSITION); + } + + public override String getResourceLocation() { + return '/' + TwilioRestClient.DEFAULT_VERSION + '/Accounts/' + + this.getRequestAccountSid() + '/Queues/' + this.getQueueSid() + + '/Members/' + this.getCallSid() + '.json'; + } + + public Datetime getDateCreated() + { + return this.getPropertyDatetime('date_created'); + } + + public TwilioMember dequeue(String urlInfo, String methodInfo) + { + Map vars = new Map(); + vars.put(URL, urlInfo); + vars.put(METHOD, methodInfo); + TwilioRestResponse response = this.getClient().safeRequest(this.getResourceLocation(), 'POST', vars); + + TwilioMember member = new TwilioMember(this.getClient(), response.toMap(), getQueueSid()); + member.setRequestAccountSid(this.getRequestAccountSid()); + return member; + } } \ No newline at end of file diff --git a/src/classes/TwilioMemberList.cls b/src/classes/TwilioMemberList.cls index 22f36f3..7f522cf 100644 --- a/src/classes/TwilioMemberList.cls +++ b/src/classes/TwilioMemberList.cls @@ -1,3 +1,53 @@ -global class TwilioMemberList { +global class TwilioMemberList extends TwilioResource.ListResource implements Iterable +{ + private String queueSid; + + public TwilioMemberList(TwilioRestClient client) + { + super(client); + } + + public TwilioMemberList(TwilioRestClient client,Map filters) { + super(client, filters); + } + + public TwilioMemberList(TwilioRestClient client,String queueSid) { + super(client); + this.queueSid = queueSid; + } + + /* implements method from TwilioResource.ListResource */ + public override Object makeNew(TwilioRestClient client, Map params) { + return new TwilioMember(client, params, queueSid); + } + + /* implements method from TwilioResource.ListResource */ + public override String getListKey() { + return 'queue_members'; + } + + /* implements method from TwilioResource */ + public override String getResourceLocation() { + return '/' + TwilioRestClient.DEFAULT_VERSION + '/Accounts/' + + this.getRequestAccountSid() + '/Queues/' + + this.queueSid + '/Members.json'; + } + + public List getPageData() { + List returnList = new List(); + + for (Object o : this.getObjectPageData()) { + if (o instanceof TwilioMember) { + returnList.add((TwilioMember) o); + } + } + return returnList; + } + + /* implement method from Iterable interface */ + global Iterator iterator() + { + return new TwilioIterator.MemberIterator(this); + } } \ No newline at end of file diff --git a/src/classes/TwilioQueue.cls b/src/classes/TwilioQueue.cls index d38eab2..99791be 100644 --- a/src/classes/TwilioQueue.cls +++ b/src/classes/TwilioQueue.cls @@ -1,3 +1,103 @@ -global class TwilioQueue { +global class TwilioQueue extends TwilioResource.InstanceResource +{ + // Constants + private static final String SID_PROPERTY = 'sid'; + private static final String FRIENDLY_NAME = 'friendly_name'; + private static final String CURRENT_SIZE = 'current_size'; + private static final String MAX_SIZE = 'max_size'; + private static final String AVERAGE_WAIT_TIME = 'average_wait_time'; + + public TwilioQueue(TwilioRestClient client, String sid) + { + super(client); + this.setProperty(SID_PROPERTY, sid); + } + + public TwilioQueue(TwilioRestClient client, Map params) { + super(client, params); + } + + /* implements method from TwilioResource */ + public override String getResourceLocation() + { + return '/' + TwilioRestClient.DEFAULT_VERSION + '/Accounts/' + + this.getRequestAccountSid() + '/Queues/' + this.getSid(); + return null; + } + + public String getSid() { + return this.getProperty(SID_PROPERTY); + } + + public String getFriendlyName() { + return this.getProperty(FRIENDLY_NAME); + } + + public Integer getCurrentSize() { + Integer prop = this.getPropertyInteger(CURRENT_SIZE); + if (prop != null) { + return prop; + } else { + throw new TwilioRestException('The Queue instance doesn\'t have the current size property set'); + } + } + + public Integer getMaxSize() { + Integer prop = this.getPropertyInteger(MAX_SIZE); + if (prop != null) { + return prop; + } else { + throw new TwilioRestException('The Queue instance doesn\'t have the max size property set'); + } + } + + public Integer getAverageWaitTime() { + Integer prop = (Integer) this.getPropertyInteger(AVERAGE_WAIT_TIME); + if (prop != null) { + return prop; + } else { + throw new TwilioRestException('The Queue instance doesn\'t have the average wait time property set'); + } + } + + public TwilioMemberList getMembers() + { + TwilioMemberList memberlist = new TwilioMemberList(this.getClient(), this.getSid()); + memberlist.setRequestAccountSid(this.getRequestAccountSid()); + return memberlist; + } + + public TwilioMember getMember(String callSid) { + TwilioMember member = new TwilioMember(this.getClient(), this.getSid(), callSid); + member.setRequestAccountSid(this.getRequestAccountSid()); + return member; + } + + public TwilioMember dequeueHeadOfQueue(String url, String method){ + TwilioMember m = new TwilioMember(this.getClient(), this.getSid()); + m.setRequestAccountSid(this.getRequestAccountSid()); + return m.dequeue(url, method); + } + + public void setFriendlyName(String friendlyName){ + Map vars = new Map(); + vars.put(FRIENDLY_NAME, friendlyName); + TwilioRestResponse response = this.getClient().safeRequest(this.getResourceLocation(), 'POST', vars); + if (response.isError()) { + throw new TwilioRestException('Response indicated error:' + response.getResponseText()); + } + this.setProperty(FRIENDLY_NAME, friendlyName); + } + + public void setMaxSize(Integer maxSize){ + Map vars = new Map(); + String maxSizeString = String.valueof(maxSize); + vars.put(MAX_SIZE, maxSizeString); + TwilioRestResponse response = this.getClient().safeRequest(this.getResourceLocation(), 'POST', vars); + if (response.isError()) { + throw new TwilioRestException('Response indicated error:' + response.getResponseText()); + } + this.setProperty(MAX_SIZE, maxSizeString); + } } \ No newline at end of file diff --git a/src/classes/TwilioQueueList.cls b/src/classes/TwilioQueueList.cls index c08008e..f20e5c1 100644 --- a/src/classes/TwilioQueueList.cls +++ b/src/classes/TwilioQueueList.cls @@ -1,3 +1,49 @@ -global class TwilioQueueList { +global class TwilioQueueList extends TwilioResource.ListResource implements Iterable +{ + public TwilioQueueList(final TwilioRestClient client) { + super(client); + } + + public TwilioQueueList(TwilioRestClient client,Map filters) { + super(client, filters); + } + + public TwilioQueue create(Map params) { + return (TwilioQueue)this.createInstance(params); + } + + public TwilioQueue create(List params) { + return (TwilioQueue) this.createInstance(params); + } + + public override Object makeNew(TwilioRestClient client, Map params) { + return new TwilioQueue(client, params); + } + + /* implements method from TwilioResource.ListResource */ + public override String getListKey() { + return 'queues'; + } + + /* implements method from TwilioResource */ + public override String getResourceLocation() { + return '/' + TwilioRestClient.DEFAULT_VERSION + '/Accounts/' + + this.getRequestAccountSid() + '/Queues.json'; + } + + public List getPageData() { + List returnList = new List(); + + for (Object o : this.getObjectPageData()) { + if (o instanceof TwilioQueue) { + returnList.add((TwilioQueue) o); + } + } + return returnList; + } + + global Iterator iterator() { + return new TwilioIterator.QueueIterator(this); + } } \ No newline at end of file diff --git a/src/classes/TwilioResource.cls b/src/classes/TwilioResource.cls index 052bfd7..75af00f 100644 --- a/src/classes/TwilioResource.cls +++ b/src/classes/TwilioResource.cls @@ -247,6 +247,10 @@ public abstract class TwilioResource this.getClient().safeRequest(this.getResourceLocation(), 'POST', params); } + public void updateResource(List params) + { + this.getClient().safeRequestPair(this.getResourceLocation(), 'POST',params); + } protected override void parseResponse(TwilioRestResponse response) { Map properties = response.toMap(); this.properties = new Map(properties); diff --git a/src/classes/TwilioRestClient.cls b/src/classes/TwilioRestClient.cls index 6ddf2a0..754b9d1 100644 --- a/src/classes/TwilioRestClient.cls +++ b/src/classes/TwilioRestClient.cls @@ -28,7 +28,7 @@ OTHER DEALINGS IN THE SOFTWARE. public class TwilioRestClient { /** The Constant VERSION. */ - private static final String VERSION = '3.1.0'; + private static final String VERSION = '3.2.0'; /** The endpoint. */ private String endpoint = 'https://api.twilio.com'; @@ -471,10 +471,12 @@ public class TwilioRestClient { String contentType; if (Test.isRunningTest()) { // can't execute HTTP requests during test execution + system.debug('Request :'+request); Twilio_TestHTTPMock.Response responseMock = Twilio_TestHTTPMock.getInstance().send(request); responseBody = (responseMock.getBody()==null)?'':responseMock.getBody(); statusCode = responseMock.getStatusCode(); contentType = responseMock.getHeader('Content-Type'); + system.debug('Response Mock :'+responseMock); } else { // execute HTTP request HttpResponse response = (new Http()).send(request); @@ -527,7 +529,6 @@ public class TwilioRestClient { request.setHeader('User-Agent', 'twilio-salesforce/' + VERSION); request.setHeader('Accept', 'application/json'); request.setHeader('Accept-Charset', 'utf-8'); - request.setHeader('Authorization', 'Basic '+EncodingUtil.base64Encode(Blob.valueOf(this.accountSid + ':' + this.authToken))); diff --git a/src/classes/Twilio_TestHTTPMock.cls b/src/classes/Twilio_TestHTTPMock.cls index 37d441f..0a6a18f 100644 --- a/src/classes/Twilio_TestHTTPMock.cls +++ b/src/classes/Twilio_TestHTTPMock.cls @@ -69,6 +69,7 @@ public class Twilio_TestHTTPMock { Map resourceMap = this.methodMap.get(method.toUpperCase()); if (resourceMap!=null) { res = resourceMap.get(uri.toUpperCase()); + system.debug('uri:'+uri.toUpperCase()); system.debug('Resource Map:'+resourceMap+' , Keys :'+resourceMap.keySet()); if (res!=null) { System.debug('Twilio_TestHTTPMock::getResponse() Found Resource for '+method+' '+uri+' = '+res); diff --git a/src/classes/Twilio_TestMedia.cls b/src/classes/Twilio_TestMedia.cls new file mode 100644 index 0000000..a168ab9 --- /dev/null +++ b/src/classes/Twilio_TestMedia.cls @@ -0,0 +1,73 @@ +@isTest +private class Twilio_TestMedia { + + final static String authToken = '12345678901234567890123456789012'; + + static testMethod void testTwilioMedia() { + + string accountJsonResponseBody='{' + +'"first_page_uri":"/2010-04-01/Accounts/AC03c2fcd60e144e7cbeee413fcbf812a3/Messages/MM800f449d0399ed014aae2bcc0cc2f2ec/Media.json?PageSize=50&Page=0",' + +'"last_page_uri":"/2010-04-01/Accounts/AC03c2fcd60e144e7cbeee413fcbf812a3/Messages/MM800f449d0399ed014aae2bcc0cc2f2ec/Media.json?PageSize=50&Page=0",' + +'"media_list":[' + +'{' + +'"sid":"ME85ebf7e12cb821f84b319340424dcb02",' + +'"account_sid":"AC03c2fcd60e144e7cbeee413fcbf812a3",' + +'"parent_sid":"MM800f449d0399ed014aae2bcc0cc2f2ec",' + +'"content_type":"image/png",' + +'"date_created":"Wed, 25 Sep 2013 22:47:18 +0000",' + +'"date_updated":"Wed, 25 Sep 2013 22:47:19 +0000",' + +'"uri":"/2010-04-01/Accounts/AC03c2fcd60e144e7cbeee413fcbf812a3/Messages/MM800f449d0399ed014aae2bcc0cc2f2ec/Media/ME85ebf7e12cb821f84b319340424dcb02.json"' + +'},' + +'{' + +'"sid":"ME8d8f717e2d6e5383055b3cd150ac5f54",' + +'"account_sid":"AC03c2fcd60e144e7cbeee413fcbf812a3",' + +'"parent_sid":"MM800f449d0399ed014aae2bcc0cc2f2ec",' + +'"content_type":"image/png",' + +'"date_created":"Wed, 25 Sep 2013 22:47:18 +0000",' + +'"date_updated":"Wed, 25 Sep 2013 22:47:19 +0000",' + +'"uri":"/2010-04-01/Accounts/AC03c2fcd60e144e7cbeee413fcbf812a3/Messages/MM800f449d0399ed014aae2bcc0cc2f2ec/Media/ME8d8f717e2d6e5383055b3cd150ac5f54.json"' + +'}' + +'],' + +'"previous_page_uri":null,' + +'"end":1,' + +'"uri":"/2010-04-01/Accounts/AC03c2fcd60e144e7cbeee413fcbf812a3/Messages/MM800f449d0399ed014aae2bcc0cc2f2ec/Media.json?PageSize=50&Page=0",' + +'"page_size":50,' + +'"start":0,' + +'"next_page_uri":null,' + +'"num_pages":1,' + +'"total":2,' + +'"page":0' + +'}'; + + Twilio_TestHTTPMock.getInstance().putResponse( + 'GET', + 'https://api.twilio.com/2010-04-01/Accounts/AC03c2fcd60e144e7cbeee413fcbf812a3/Messages/MM800f449d0399ed014aae2bcc0cc2f2ec/Media.json', + new Twilio_TestHTTPMock.Response(accountJsonResponseBody,200) + ); + + + // Get an API client and request the Twilio Account + TwilioRestClient client = new TwilioRestClient('AC03c2fcd60e144e7cbeee413fcbf812a3', authToken); + //TwilioMessage mms= client.getAccount().getMessage('SM800f449d0399ed014aae2bcc0cc2f2ec'); + TwilioMediaList medias = client.getAccount().getMessage('MM800f449d0399ed014aae2bcc0cc2f2ec').getMedia(); + + medias.getResourceLocation(); + medias.getListkey(); + medias.getRequestMessageSid(); + + + for (TwilioMedia media : medias.getPageData()) { + media.getResourceLocation(); + media.getSid(); + media.getRequestMessageSid(); + media.getDateCreated(); + media.getDateUpdated(); + media.getAccountSid(); + media.getParentSid(); + media.getContentType(); + media.getAPIVersion(); + media.getUri(); + + } + } +} \ No newline at end of file diff --git a/src/classes/Twilio_TestMedia.cls-meta.xml b/src/classes/Twilio_TestMedia.cls-meta.xml new file mode 100644 index 0000000..f165265 --- /dev/null +++ b/src/classes/Twilio_TestMedia.cls-meta.xml @@ -0,0 +1,5 @@ + + + 29.0 + Active + diff --git a/src/classes/Twilio_TestMember.cls b/src/classes/Twilio_TestMember.cls new file mode 100644 index 0000000..63c64c4 --- /dev/null +++ b/src/classes/Twilio_TestMember.cls @@ -0,0 +1,78 @@ +@isTest +private class Twilio_TestMember { + + final static String authToken = '12345678901234567890123456789012'; + + static testMethod void testTwilioMember_memberList() + { + string accountJsonResponseBody='{' + +'"num_pages": 1,' + +'"page": 0,' + +'"page_size": 50,' + +'"start": 0,' + +'"total": 2,' + +'"end": 1,' + +'"first_page_uri": "/2010-04-01/Accounts/ACadfb5891c0e4f8c6c34be620a8ec1ef3/Queues/QUd6ef9309ce0d4780b243057e37dda857/Members.json?Page=0&PageSize=50",' + +'"last_page_uri": "/2010-04-01/Accounts/ACadfb5891c0e4f8c6c34be620a8ec1ef3/Queues/QUd6ef9309ce0d4780b243057e37dda857/Members.json?Page=0&PageSize=50",' + +'"next_page_uri": null,' + +'"previous_page_uri": null,' + +'"uri": "/2010-04-01/Accounts/ACadfb5891c0e4f8c6c34be620a8ec1ef3/Queues/QUd6ef9309ce0d4780b243057e37dda857/Members.json",' + +'"queue_members": [' + +'{' + +'"call_sid": "CA5ef8732a3c49700934481addd5ce1659",' + +'"date_enqueued": "Mon, 4 Feb 2012 15:44:15 +0000",' + +'"wait_time": 30,' + +'"position": 1,' + +'"uri": "/2010-04-01/Accounts/ACadfb5891c0e4f8c6c34be620a8ec1ef3/Queues/QUd6ef9309ce0d4780b243057e37dda857/Members/CA5ef8732a3c49700934481addd5ce1659.json"' + +' }' + +']' + +'}'; + + Twilio_TestHTTPMock.getInstance().putResponse('GET', + 'https://api.twilio.com/2010-04-01/Accounts/ACadfb5891c0e4f8c6c34be620a8ec1ef3/Queues/QUd6ef9309ce0d4780b243057e37dda857/Members.json', + new Twilio_TestHTTPMock.Response(accountJsonResponseBody,200)); + + // Get an API client and request the Twilio Account + TwilioRestClient client = new TwilioRestClient('ACadfb5891c0e4f8c6c34be620a8ec1ef3', authToken); + + TwilioMemberList members = client.getAccount().getQueue('QUd6ef9309ce0d4780b243057e37dda857').getMembers(); + + // Loop over members and print out a property for each one. + for (TwilioMember member : members.getPageData()) { + member.getQueueSid(); + member.getCallSid(); + member.getDateEnqueued(); + member.getWaitTime(); + member.getPosition(); + member.getResourceLocation(); + member.getDateCreated(); + } + } + + static testMethod void testTwilioMember_dequeue() + { + + string accountJsonResponseBody='{' + +'"call_sid": "CA386025c9bf5d6052a1d1ea42b4d16662",' + +'"date_enqueued": "Mon, 4 Feb 2012 15:44:15 +0000",' + +'"wait_time": 30,' + +'"position": 0,' + +'"uri": "/2010-04-01/Accounts/ACadfb5891c0e4f8c6c34be620a8ec1ef3/Queues/QUd6ef9309ce0d4780b243057e37dda857/Members/CA386025c9bf5d6052a1d1ea42b4d16662.json"' + +'}'; + + Twilio_TestHTTPMock.getInstance().putResponse('POST', + 'https://api.twilio.com/2010-04-01/Accounts/ACadfb5891c0e4f8c6c34be620a8ec1ef3/Queues/QUd6ef9309ce0d4780b243057e37dda857/Members/CA386025c9bf5d6052a1d1ea42b4d16662.json', + new Twilio_TestHTTPMock.Response(accountJsonResponseBody,200)); + + // Get an API client and request the Twilio Account + TwilioRestClient client = new TwilioRestClient('ACadfb5891c0e4f8c6c34be620a8ec1ef3', authToken); + + TwilioMember member = client.getAccount().getQueue('QUd6ef9309ce0d4780b243057e37dda857').getMember('CA386025c9bf5d6052a1d1ea42b4d16662'); + // Build a filter for the MemberList + List params = new List(); + params.add(new TwilioNameValuePair('Url', 'http://demo.twilio.com/docs/voice.xml')); + params.add(new TwilioNameValuePair('Method','POST')); + member.updateResource(params); + + } +} \ No newline at end of file diff --git a/src/classes/Twilio_TestMember.cls-meta.xml b/src/classes/Twilio_TestMember.cls-meta.xml new file mode 100644 index 0000000..f165265 --- /dev/null +++ b/src/classes/Twilio_TestMember.cls-meta.xml @@ -0,0 +1,5 @@ + + + 29.0 + Active + diff --git a/src/classes/Twilio_TestQueue.cls b/src/classes/Twilio_TestQueue.cls new file mode 100644 index 0000000..6030caa --- /dev/null +++ b/src/classes/Twilio_TestQueue.cls @@ -0,0 +1,128 @@ +@isTest +private class Twilio_TestQueue +{ + final static String authToken = '12345678901234567890123456789012'; + + static testMethod void testTwilioQueue_create() + { + string accountJsonResponseBody='{' + +'"sid": "QUd6ef9309ce0d4780b243057e37dda857",' + +'"friendly_name": "TestQueue",' + +'"current_size": 0,' + +'"average_wait_time": 0,' + +'"max_size": 100,' + +'"date_created": "Fri, 01 Nov 2013 06:06:49 +0000",' + +'"date_updated": "Fri, 01 Nov 2013 06:06:49 +0000",' + +'"uri": "/2010-04-01/Accounts/ACadfb5891c0e4f8c6c34be620a8ec1ef3/Queues/QUd6ef9309ce0d4780b243057e37dda857.json"' + +'}'; + + Twilio_TestHTTPMock.getInstance().putResponse('POST', + 'https://api.twilio.com/2010-04-01/Accounts/ACadfb5891c0e4f8c6c34be620a8ec1ef3/Queues.json', + new Twilio_TestHTTPMock.Response(accountJsonResponseBody,200)); + + // Get an API client and request the Twilio Account + TwilioRestClient client = new TwilioRestClient('ACadfb5891c0e4f8c6c34be620a8ec1ef3', authToken); + + + List params = new List(); + params.add(new TwilioNameValuePair('FriendlyName','TestQueue')); + + TwilioQueue queue = client.getAccount().getQueues().create(params); + + System.assertEquals('QUd6ef9309ce0d4780b243057e37dda857', queue.getsid()); + System.assertEquals('TestQueue', queue.getFriendlyName()); + System.assertEquals('0',String.valueof(queue.getCurrentSize())); + System.assertEquals('100',String.valueof(queue.getMaxSize())); + System.assertEquals('0',String.valueof(queue.getAverageWaitTime())); + + } + + static testMethod void testTwilioQueue_availableQueues() + { + string accountJsonResponseBody='{' + +'"start": 0,' + +'"total": 2,' + +'"uri": "/2010-04-01/Accounts/ACadfb5891c0e4f8c6c34be620a8ec1ef3/Queues.json",' + +'"end": 49,' + +'"first_page_uri": "/2010-04-01/Accounts/ACadfb5891c0e4f8c6c34be620a8ec1ef3/Queues.json?Page=0&PageSize=50",' + +'"last_page_uri": "/2010-04-01/Accounts/ACadfb5891c0e4f8c6c34be620a8ec1ef3/Queues.json?Page=0&PageSize=50",' + +'"next_page_uri": "",' + +'"num_pages": 0,' + +'"page": 0,' + +'"page_size": 50,' + +'"previous_page_uri": "",' + +'"queues": [' + +'{' + +'"average_wait_time": 0,' + +'"current_size": 0,' + +'"date_created": "Thu, 26 Apr 2012 20:12:45 +0000",' + +'"date_updated": "Thu, 26 Apr 2012 20:12:45 +0000",' + +'"friendly_name": "TestQueue",' + +'"max_size": 100,' + +'"sid": "QUd6ef9309ce0d4780b243057e37dda857",' + +'"uri": "/2010-04-01/Accounts/ACadfb5891c0e4f8c6c34be620a8ec1ef3/Queues/QUd6ef9309ce0d4780b243057e37dda857.json"' + +'}' + +']' + +'}'; + + Twilio_TestHTTPMock.getInstance().putResponse('GET', + 'https://api.twilio.com/2010-04-01/Accounts/ACadfb5891c0e4f8c6c34be620a8ec1ef3/Queues.json', + new Twilio_TestHTTPMock.Response(accountJsonResponseBody,200)); + + // Get an API client and request the Twilio Account + TwilioRestClient client = new TwilioRestClient('ACadfb5891c0e4f8c6c34be620a8ec1ef3', authToken); + + + TwilioQueueList queues = client.getAccount().getQueues(); + + for(TwilioQueue queue :queues.getPageData()) + { + System.assertEquals('QUd6ef9309ce0d4780b243057e37dda857', queue.getsid()); + System.assertEquals('TestQueue', queue.getFriendlyName()); + System.assertEquals('0',String.valueof(queue.getCurrentSize())); + System.assertEquals('100',String.valueof(queue.getMaxSize())); + System.assertEquals('0',String.valueof(queue.getAverageWaitTime())); + + } + + + } + + static testMethod void testTwilioQueue_update() + { + string accountJsonResponseBody='{' + +'"average_wait_time": 0,' + +'"friendly_name": "TestQueue",' + +'"current_size": 0,' + +'"max_size": 123,' + +'"average_wait_time": 0,' + +'"date_updated": "Fri, 22 Jun 2012 00:02:15 +0000",' + +'"date_created": "Thu, 21 Jun 2012 23:55:36 +0000",' + +'"sid": "QUd6ef9309ce0d4780b243057e37dda857",' + +'"uri": "/2010-04-01/Accounts/ACadfb5891c0e4f8c6c34be620a8ec1ef3/Queues/QUd6ef9309ce0d4780b243057e37dda857.json"' + +'}'; + + Twilio_TestHTTPMock.getInstance().putResponse('POST', + 'https://api.twilio.com/2010-04-01/Accounts/ACadfb5891c0e4f8c6c34be620a8ec1ef3/Queues/QUd6ef9309ce0d4780b243057e37dda857', + new Twilio_TestHTTPMock.Response(accountJsonResponseBody,200)); + + // Get an API client and request the Twilio Account + TwilioRestClient client = new TwilioRestClient('ACadfb5891c0e4f8c6c34be620a8ec1ef3', authToken); + + TwilioQueue queue = client.getAccount().getQueue('QUd6ef9309ce0d4780b243057e37dda857'); + List params = new List(); + params.add(new TwilioNameValuePair('MaxSize','123')); + + queue.updateResource(params); + + /*TwilioQueue queue = client.getAccount().getQueues().create(params); + + System.assertEquals('QUd6ef9309ce0d4780b243057e37dda857', queue.getsid()); + System.assertEquals('TestQueue', queue.getFriendlyName()); + System.assertEquals('0',String.valueof(queue.getCurrentSize())); + System.assertEquals('100',String.valueof(queue.getMaxSize())); + System.assertEquals('0',String.valueof(queue.getAverageWaitTime()));*/ + + } + +} \ No newline at end of file diff --git a/src/classes/Twilio_TestQueue.cls-meta.xml b/src/classes/Twilio_TestQueue.cls-meta.xml new file mode 100644 index 0000000..f165265 --- /dev/null +++ b/src/classes/Twilio_TestQueue.cls-meta.xml @@ -0,0 +1,5 @@ + + + 29.0 + Active + diff --git a/src/classes/Twilio_TestRest.cls b/src/classes/Twilio_TestRest.cls index fb36e51..11db654 100644 --- a/src/classes/Twilio_TestRest.cls +++ b/src/classes/Twilio_TestRest.cls @@ -25,9 +25,9 @@ OTHER DEALINGS IN THE SOFTWARE. @isTest private class Twilio_TestRest { - final static String accountSid = 'AC03c2fcd60e144e7cbeed179fcbf812a3'; - final static String authToken = '12345678901234567890123456789012'; - + final static String accountSid = 'AC03c2fcd60e144e7cbeed179fcbf812a3'; + final static String authToken = '12345678901234567890123456789012'; + static testMethod void testTwilioRestResponse() { String url = 'https://api.twilio.com'; String queryString = 'a=1&b=2'; @@ -77,12 +77,12 @@ private class Twilio_TestRest { Exception restEx = null; try { - Map xmlMap = response.toMap(); + Map xmlMap = response.toMap(); } catch (Exception e) { - restEx = e; - } - System.assert(restEx instanceof TwilioRestException); - + restEx = e; + } + System.assert(restEx instanceof TwilioRestException); + response.setUrl('test'); System.assertEquals('test', response.getUrl()); @@ -92,77 +92,77 @@ private class Twilio_TestRest { } static testmethod void testTwilioRestClient() { - TwilioRestClient client = new TwilioRestClient(accountSid, authToken); - - client.setNumRetries(3); - System.assertEquals(3, client.getNumRetries()); - - Exception e = null; - try { - client.safeRequest('https://api.twilio.com', 'PUT', new Map{'key'=>'val'}); - } catch (TwilioRestException tre) { - e = tre; - } - System.assert(e instanceof TwilioRestException); - - e = null; - try { - client.safeRequest('https://api.twilio.com', 'DELETE', null); - } catch (TwilioRestException tre) { - e = tre; - } - System.assert(e instanceof TwilioRestException); - - client = new TwilioRestClient(accountSid,authToken,'https://anotherendpoint.twilio.com'); - System.assertEquals('https://anotherendpoint.twilio.com', client.getEndpoint()); - - e = null; - try { - client = new TwilioRestClient(null,authToken); - } catch (TwilioRestException tre) { - e = tre; - } - System.assert(e instanceof TwilioRestException); - - e = null; - try { - client = new TwilioRestClient('badSid',authToken); - } catch (TwilioRestException tre) { - e = tre; - } - System.assert(e instanceof TwilioRestException); - - e = null; - try { - client = new TwilioRestClient('ACbadSid',authToken); - } catch (TwilioRestException tre) { - e = tre; - } - System.assert(e instanceof TwilioRestException); - - e = null; - try { - client = new TwilioRestClient(accountSid,null); - } catch (TwilioRestException tre) { - e = tre; - } - System.assert(e instanceof TwilioRestException); - - e = null; - try { - client = new TwilioRestClient(accountSid,'badToken'); - } catch (TwilioRestException tre) { - e = tre; - } - System.assert(e instanceof TwilioRestException); + TwilioRestClient client = new TwilioRestClient(accountSid, authToken); + + client.setNumRetries(3); + System.assertEquals(3, client.getNumRetries()); + + Exception e = null; + try { + client.safeRequest('https://api.twilio.com', 'PUT', new Map{'key'=>'val'}); + } catch (TwilioRestException tre) { + e = tre; + } + System.assert(e instanceof TwilioRestException); + + e = null; + try { + client.safeRequest('https://api.twilio.com', 'DELETE', null); + } catch (TwilioRestException tre) { + e = tre; + } + System.assert(e instanceof TwilioRestException); + + client = new TwilioRestClient(accountSid,authToken,'https://anotherendpoint.twilio.com'); + System.assertEquals('https://anotherendpoint.twilio.com', client.getEndpoint()); + + e = null; + try { + client = new TwilioRestClient(null,authToken); + } catch (TwilioRestException tre) { + e = tre; + } + System.assert(e instanceof TwilioRestException); + + e = null; + try { + client = new TwilioRestClient('badSid',authToken); + } catch (TwilioRestException tre) { + e = tre; + } + System.assert(e instanceof TwilioRestException); + + e = null; + try { + client = new TwilioRestClient('ACbadSid',authToken); + } catch (TwilioRestException tre) { + e = tre; + } + System.assert(e instanceof TwilioRestException); + + e = null; + try { + client = new TwilioRestClient(accountSid,null); + } catch (TwilioRestException tre) { + e = tre; + } + System.assert(e instanceof TwilioRestException); + + e = null; + try { + client = new TwilioRestClient(accountSid,'badToken'); + } catch (TwilioRestException tre) { + e = tre; + } + System.assert(e instanceof TwilioRestException); } static testmethod void testValidateRequest() { - //The Twilio authToken is the encryption key + //The Twilio authToken is the encryption key String methodAuthToken = '1c892n40nd03kdnc0112slzkl3091j20'; TwilioRestClient client = new TwilioRestClient(accountSid,methodAuthToken); - // This is the signature we expect + // This is the signature we expect String expected_sig = 'fF+xx6dTinOaCdZ0aIeNkHr/ZAA='; //This is the url that twilio requested diff --git a/src/package.xml b/src/package.xml index b6c91c4..add649f 100755 --- a/src/package.xml +++ b/src/package.xml @@ -66,6 +66,9 @@ Twilio_TestSandbox Twilio_TestSms Twilio_TestTwiML + Twilio_TestQueue + Twilio_TestMedia + Twilio_TestMember ApexClass