Salesforce Call Extend PHP Custom API (MyIot)
-
添加远程服务授权端点到Remote Site 中:
Salesforce ->Setup->Administer->Security Site Settings->Remote Site Settings
2. 匿名测试API
Salesforce -> Developer Console -> Debut -> Open Execute Anonymous Window
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(‘http://www.####.com/myiot/dht11_api.php’);
request.setMethod(‘GET’);
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
// Deserialize the JSON string into collections of primitive data types.
Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
// Cast the values in the ‘result’ key as a list
List<Object> dht11_data = (List<Object>) results.get(‘result’);
System.debug(‘Received the following dht11_data:’);
for (Object dht11: dht11_data) {
System.debug(dht11);
}
}
选择 Open Log , 然后点击 Execute, 测试log 中可能看到已取到数据( 勾选 Debug Only)
Developer Console -> File -> New -> Apex Class
创建apex class 读取远程 API 的数据
public class DHT11Callouts {
public static HttpResponse makeGetCallout() {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(‘http://www.####.com/myiot/dht11_api.php’);
request.setMethod(‘GET’);
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
// Deserialize the JSON string into collections of primitive data types.
Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
// Cast the values in the ‘result’ key as a list
List<Object> dht11_data = (List<Object>) results.get(‘result’);
System.debug(‘Received the following dht11_data:’);
for (Object dht11: dht11_data) {
System.debug(dht11);
}
}
return response;
}
}
Developer Console -> File -> New -> Static Resource
For the name, enter GetDHT11Resource.
For the MIME type, select text/plain, even though we are using JSON.
Click Submit.
设置mock 数据,将因定比较数据存入statusResource 中
{“result”:[{“id”:”43942″,”hostname”:”pisvr01″,”location”:”DG1″,”room_temp_c”:”22″,”room_temp_f”:”71.6″,”room_humidity”:”95%”,”cpu_temp”:”45.084″,”gpu_temp”:”45.1″,”cpu_use”:”8.0%”,”ram_total”:”948″,”ram_used”:”255″,”ram_free”:”120″,”disk_total”:”15G”,”disk_used”:”4.4G”,”disk_used_perc”:”32%”,”creation_year”:”2020″,”creation_datetime”:”2020-04-03 14:50:06″},{“id”:”43941″,”hostname”:”pisvr01″,”location”:”DG1″,”room_temp_c”:”22″,”room_temp_f”:”71.6″,”room_humidity”:”95%”,”cpu_temp”:”45.084″,”gpu_temp”:”45.6″,”cpu_use”:”0.0%”,”ram_total”:”948″,”ram_used”:”254″,”ram_free”:”121″,”disk_total”:”15G”,”disk_used”:”4.4G”,”disk_used_perc”:”32%”,”creation_year”:”2020″,”creation_datetime”:”2020-04-03 14:45:03″},{“id”:”43940″,”hostname”:”pisvr01″,”location”:”DG1″,”room_temp_c”:”22″,”room_temp_f”:”71.6″,”room_humidity”:”95%”,”cpu_temp”:”45.622″,”gpu_temp”:”46.2″,”cpu_use”:”8.7%”,”ram_total”:”948″,”ram_used”:”256″,”ram_free”:”119″,”disk_total”:”15G”,”disk_used”:”4.4G”,”disk_used_perc”:”32%”,”creation_year”:”2020″,”creation_datetime”:”2020-04-03 14:40:53″},{“id”:”43939″,”hostname”:”pisvr01″,”location”:”DG1″,”room_temp_c”:”22″,”room_temp_f”:”71.6″,”room_humidity”:”95%”,”cpu_temp”:”44.546″,”gpu_temp”:”45.1″,”cpu_use”:”0.0%”,”ram_total”:”948″,”ram_used”:”254″,”ram_free”:”121″,”disk_total”:”15G”,”disk_used”:”4.4G”,”disk_used_perc”:”32%”,”creation_year”:”2020″,”creation_datetime”:”2020-04-03 14:35:07″},{“id”:”43938″,”hostname”:”pisvr01″,”location”:”DG1″,”room_temp_c”:”22″,”room_temp_f”:”71.6″,”room_humidity”:”95%”,”cpu_temp”:”45.084″,”gpu_temp”:”45.1″,”cpu_use”:”8.0%”,”ram_total”:”948″,”ram_used”:”254″,”ram_free”:”121″,”disk_total”:”15G”,”disk_used”:”4.4G”,”disk_used_perc”:”32%”,”creation_year”:”2020″,”creation_datetime”:”2020-04-03 14:30:08″},{“id”:”43937″,”hostname”:”pisvr01″,”location”:”DG1″,”room_temp_c”:”22″,”room_temp_f”:”71.6″,”room_humidity”:”95%”,”cpu_temp”:”45.084″,”gpu_temp”:”45.1″,”cpu_use”:”5.3%”,”ram_total”:”948″,”ram_used”:”254″,”ram_free”:”121″,”disk_total”:”15G”,”disk_used”:”4.4G”,”disk_used_perc”:”32%”,”creation_year”:”2020″,”creation_datetime”:”2020-04-03 14:25:05″},{“id”:”43936″,”hostname”:”pisvr01″,”location”:”DG1″,”room_temp_c”:”22″,”room_temp_f”:”71.6″,”room_humidity”:”95%”,”cpu_temp”:”45.084″,”gpu_temp”:”45.1″,”cpu_use”:”8.8%”,”ram_total”:”948″,”ram_used”:”254″,”ram_free”:”121″,”disk_total”:”15G”,”disk_used”:”4.4G”,”disk_used_perc”:”32%”,”creation_year”:”2020″,”creation_datetime”:”2020-04-03 14:20:08″},{“id”:”43935″,”hostname”:”pisvr01″,”location”:”DG1″,”room_temp_c”:”22″,”room_temp_f”:”71.6″,”room_humidity”:”95%”,”cpu_temp”:”45.084″,”gpu_temp”:”45.1″,”cpu_use”:”0.5%”,”ram_total”:”948″,”ram_used”:”253″,”ram_free”:”122″,”disk_total”:”15G”,”disk_used”:”4.4G”,”disk_used_perc”:”32%”,”creation_year”:”2020″,”creation_datetime”:”2020-04-03 14:15:03″},{“id”:”43934″,”hostname”:”pisvr01″,”location”:”DG1″,”room_temp_c”:”22″,”room_temp_f”:”71.6″,”room_humidity”:”95%”,”cpu_temp”:”45.084″,”gpu_temp”:”45.1″,”cpu_use”:”7.3%”,”ram_total”:”948″,”ram_used”:”255″,”ram_free”:”120″,”disk_total”:”15G”,”disk_used”:”4.4G”,”disk_used_perc”:”32%”,”creation_year”:”2020″,”creation_datetime”:”2020-04-03 14:10:10″},{“id”:”43933″,”hostname”:”pisvr01″,”location”:”DG1″,”room_temp_c”:”22″,”room_temp_f”:”71.6″,”room_humidity”:”95%”,”cpu_temp”:”45.084″,”gpu_temp”:”44.5″,”cpu_use”:”0.3%”,”ram_total”:”948″,”ram_used”:”255″,”ram_free”:”121″,”disk_total”:”15G”,”disk_used”:”4.4G”,”disk_used_perc”:”32%”,”creation_year”:”2020″,”creation_datetime”:”2020-04-03 14:05:03″},{“id”:”43932″,”hostname”:”pisvr01″,”location”:”DG1″,”room_temp_c”:”22″,”room_temp_f”:”71.6″,”room_humidity”:”95%”,”cpu_temp”:”45.622″,”gpu_temp”:”45.1″,”cpu_use”:”5.5%”,”ram_total”:”948″,”ram_used”:”254″,”ram_free”:”121″,”disk_total”:”15G”,”disk_used”:”4.4G”,”disk_used_perc”:”32%”,”creation_year”:”2020″,”creation_datetime”:”2020-04-03 14:00:09″},{“id”:”43931″,”hostname”:”pisvr01″,”location”:”DG1″,”room_temp_c”:”22″,”room_temp_f”:”71.6″,”room_humidity”:”95%”,”cpu_temp”:”45.622″,”gpu_temp”:”45.1″,”cpu_use”:”0.3%”,”ram_total”:”948″,”ram_used”:”254″,”ram_free”:”121″,”disk_total”:”15G”,”disk_used”:”4.4G”,”disk_used_perc”:”32%”,”creation_year”:”2020″,”creation_datetime”:”2020-04-03 13:55:14″},{“id”:”43930″,”hostname”:”pisvr01″,”location”:”DG1″,”room_temp_c”:”22″,”room_temp_f”:”71.6″,”room_humidity”:”95%”,”cpu_temp”:”45.622″,”gpu_temp”:”45.1″,”cpu_use”:”6.8%”,”ram_total”:”948″,”ram_used”:”254″,”ram_free”:”122″,”disk_total”:”15G”,”disk_used”:”4.4G”,”disk_used_perc”:”32%”,”creation_year”:”2020″,”creation_datetime”:”2020-04-03 13:50:03″},{“id”:”43929″,”hostname”:”pisvr01″,”location”:”DG1″,”room_temp_c”:”22″,”room_temp_f”:”71.6″,”room_humidity”:”95%”,”cpu_temp”:”45.084″,”gpu_temp”:”45.1″,”cpu_use”:”0.0%”,”ram_total”:”948″,”ram_used”:”254″,”ram_free”:”121″,”disk_total”:”15G”,”disk_used”:”4.4G”,”disk_used_perc”:”32%”,”creation_year”:”2020″,”creation_datetime”:”2020-04-03 13:45:06″},{“id”:”43928″,”hostname”:”pisvr01″,”location”:”DG1″,”room_temp_c”:”22″,”room_temp_f”:”71.6″,”room_humidity”:”95%”,”cpu_temp”:”45.084″,”gpu_temp”:”45.1″,”cpu_use”:”7.0%”,”ram_total”:”948″,”ram_used”:”255″,”ram_free”:”120″,”disk_total”:”15G”,”disk_used”:”4.4G”,”disk_used_perc”:”32%”,”creation_year”:”2020″,”creation_datetime”:”2020-04-03 13:40:03″},{“id”:”43927″,”hostname”:”pisvr01″,”location”:”DG1″,”room_temp_c”:”22″,”room_temp_f”:”71.6″,”room_humidity”:”95%”,”cpu_temp”:”45.084″,”gpu_temp”:”45.1″,”cpu_use”:”0.0%”,”ram_total”:”948″,”ram_used”:”253″,”ram_free”:”122″,”disk_total”:”15G”,”disk_used”:”4.4G”,”disk_used_perc”:”32%”,”creation_year”:”2020″,”creation_datetime”:”2020-04-03 13:35:04″},{“id”:”43926″,”hostname”:”pisvr01″,”location”:”DG1″,”room_temp_c”:”22″,”room_temp_f”:”71.6″,”room_humidity”:”95%”,”cpu_temp”:”45.084″,”gpu_temp”:”45.1″,”cpu_use”:”9.2%”,”ram_total”:”948″,”ram_used”:”255″,”ram_free”:”121″,”disk_total”:”15G”,”disk_used”:”4.4G”,”disk_used_perc”:”32%”,”creation_year”:”2020″,”creation_datetime”:”2020-04-03 13:30:03″},{“id”:”43925″,”hostname”:”pisvr01″,”location”:”DG1″,”room_temp_c”:”22″,”room_temp_f”:”71.6″,”room_humidity”:”95%”,”cpu_temp”:”45.084″,”gpu_temp”:”45.1″,”cpu_use”:”0.2%”,”ram_total”:”948″,”ram_used”:”253″,”ram_free”:”122″,”disk_total”:”15G”,”disk_used”:”4.4G”,”disk_used_perc”:”32%”,”creation_year”:”2020″,”creation_datetime”:”2020-04-03 13:25:08″},{“id”:”43924″,”hostname”:”pisvr01″,”location”:”DG1″,”room_temp_c”:”22″,”room_temp_f”:”71.6″,”room_humidity”:”95%”,”cpu_temp”:”45.622″,”gpu_temp”:”45.6″,”cpu_use”:”7.3%”,”ram_total”:”948″,”ram_used”:”254″,”ram_free”:”121″,”disk_total”:”15G”,”disk_used”:”4.4G”,”disk_used_perc”:”32%”,”creation_year”:”2020″,”creation_datetime”:”2020-04-03 13:20:05″},{“id”:”43923″,”hostname”:”pisvr01″,”location”:”DG1″,”room_temp_c”:”22″,”room_temp_f”:”71.6″,”room_humidity”:”95%”,”cpu_temp”:”45.084″,”gpu_temp”:”45.1″,”cpu_use”:”0.3%”,”ram_total”:”948″,”ram_used”:”254″,”ram_free”:”121″,”disk_total”:”15G”,”disk_used”:”4.4G”,”disk_used_perc”:”32%”,”creation_year”:”2020″,”creation_datetime”:”2020-04-03 13:15:06″}]}
Developer Console -> File -> New -> Apex Class.
For the class name, enter DHT11CalloutsTest and then click OK.
Class: DHT11CalloutsTest
@isTest
private class DHT11CalloutsTest {
@isTest static void testGetCallout() {
// Create the mock response based on a static resource
StaticResourceCalloutMock mock = new StaticResourceCalloutMock();
mock.setStaticResource(‘GetDHT11Resource’);
mock.setStatusCode(200);
mock.setHeader(‘Content-Type’, ‘application/json;charset=UTF-8’);
// Associate the callout with a mock response
Test.setMock(HttpCalloutMock.class, mock);
// Call method to test
HttpResponse result = DHT11Callouts.makeGetCallout();
// Verify mock response is not null
System.assertNotEquals(null,result,
‘The callout returned a null response.’);
// Verify status code
System.assertEquals(200,result.getStatusCode(),
‘The status code is not 200.’);
// Verify content type
System.assertEquals(‘application/json;charset=UTF-8’,
result.getHeader(‘Content-Type’),
‘The content type value is not expected.’);
// Verify the array contains 3 items
Map<String, Object> results = (Map<String, Object>)
JSON.deserializeUntyped(result.getBody());
List<Object> dht11_data = (List<Object>) results.get(‘result’);
System.assertEquals(20, dht11_data.size(),
‘The array should only contain 20 items.’);
}
}
定义API 类
数据结构 用来接受处理 API 的数据
Class name: API_DATA.apxc
注意,这里的元素对象都要定义成 public <type> xxxx {get; set;} 的开式, 如果不带 {get; set;} 后面在apex 中调用会出问题
public class API_DATA {
public class dht11 {
public String id;
public String hostname { get; set; }
public String location { get; set; }
public String room_temp_c;
public String room_temp_f;
}
public class dht11s {
public String id { get; set; }
public String hostname { get; set; }
public String Location { get; set; }
public String room_temp_c { get; set; }
public String room_temp_f { get; set; }
public String room_humidity { get; set; }
public String cpu_temp { get; set; }
public String gpu_temp { get; set; }
public String cpu_use { get; set; }
public String ram_total { get; set; }
public String ram_used { get; set; }
public String ram_free { get; set; }
public String disk_total { get; set; }
public String disk_used { get; set; }
public String disk_used_perc { get; set; }
public String creation_datetime { get; set; }
}
}
定义Wrapper 类, 用于page 对应的control 中的API数据装载
Class: dht11Wrappers.apxc
public class dht11Wrappers {
public API_DATA.dht11s api_dht11s { get; set; }
public dht11Wrappers(){
api_dht11s = new API_DATA.dht11s();
}
public dht11Wrappers(API_DATA.dht11s dht11s){
api_dht11s = dht11s;
}
}
定义Apex page 和 Control class
Controller class: dht11Controller.apxc
public class dht11Controller {
public String title {get; set;}
public List<dht11Wrapper> wrap {get; set;}
public List<API_DATA.dht11> dht_data {get; set;}
public List<dht11Wrappers> wraps {get; set;}
public List<API_DATA.dht11s> dht_datas {get; set;}
public dht11Controller(){}
public String getTest1(){
Datetime nowDatetime = Datetime.now();
this.title = nowDatetime.format(‘yyyy-MM-dd HH:mm:ss’);
return this.title;
}
public List<dht11Wrapper> getWrap1(){
wrap = new List<dht11Wrapper>();
API_DHT11 api = new API_DHT11();
dht_data = api.getDHT11();
for (API_DATA.dht11 a : dht_data){
wrap.add(new dht11Wrapper(a));
}
return wrap;
}
public List<dht11Wrappers> getWrap1s(){
wraps = new List<dht11Wrappers>();
API_DHT11 api = new API_DHT11();
dht_datas = api.getDHT11s();
for (API_DATA.dht11s a : dht_datas){
wraps.add(new dht11Wrappers(a));
}
return wraps;
}
}
创建 vfp page 并绑定controller
Page name: dht11.vfp
<apex:page controller=”dht11Controller”>
<apex:form >
<apex:pageBlock title=”dht11″>
MyIot Data [ {!Test1} ]
<apex:pageBlockTable value=”{!Wrap1s}” var=”ct”>
<apex:column >
<apex:facet name=”header”>Hostname</apex:facet>
<apex:outputText value=”{!ct.api_dht11s.hostname}”/>
</apex:column>
<apex:column >
<apex:facet name=”header”>Location</apex:facet>
<apex:outputText value=”{!ct.api_dht11s.location}”/>
</apex:column>
<apex:column >
<apex:facet name=”header”>room_temp_c</apex:facet>
<apex:outputText value=”{!ct.api_dht11s.room_temp_c}”/>
</apex:column>
<apex:column >
<apex:facet name=”header”>room_temp_f</apex:facet>
<apex:outputText value=”{!ct.api_dht11s.room_temp_f}”/>
</apex:column>
<apex:column >
<apex:facet name=”header”>room_humidity</apex:facet>
<apex:outputText value=”{!ct.api_dht11s.room_humidity}”/>
</apex:column>
<apex:column >
<apex:facet name=”header”>cpu_temp</apex:facet>
<apex:outputText value=”{!ct.api_dht11s.cpu_temp}”/>
</apex:column>
<apex:column >
<apex:facet name=”header”>gpu_temp</apex:facet>
<apex:outputText value=”{!ct.api_dht11s.gpu_temp}”/>
</apex:column>
<apex:column >
<apex:facet name=”header”>cpu_use</apex:facet>
<apex:outputText value=”{!ct.api_dht11s.cpu_use}”/>
</apex:column>
<apex:column >
<apex:facet name=”header”>ram_total</apex:facet>
<apex:outputText value=”{!ct.api_dht11s.ram_total}”/>
</apex:column>
<apex:column >
<apex:facet name=”header”>ram_used</apex:facet>
<apex:outputText value=”{!ct.api_dht11s.ram_used}”/>
</apex:column>
<apex:column >
<apex:facet name=”header”>ram_free</apex:facet>
<apex:outputText value=”{!ct.api_dht11s.ram_free}”/>
</apex:column>
<apex:column >
<apex:facet name=”header”>disk_total</apex:facet>
<apex:outputText value=”{!ct.api_dht11s.disk_total}”/>
</apex:column>
<apex:column >
<apex:facet name=”header”>disk_used</apex:facet>
<apex:outputText value=”{!ct.api_dht11s.disk_used}”/>
</apex:column>
<apex:column >
<apex:facet name=”header”>disk_used_perc</apex:facet>
<apex:outputText value=”{!ct.api_dht11s.disk_used_perc}”/>
</apex:column>
<apex:column >
<apex:facet name=”header”>creation_datetime</apex:facet>
<apex:outputText value=”{!ct.api_dht11s.creation_datetime}”/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
— 点击 vpf page 上的 Preview 测试 —
— 测试结果 —