チュートリアル:初めての EMS リソースを実装する
EMS リソースの概要 への移動
EMS パッケージを作成すると、EMS サーバー上に新しいリソースを作成することができます。
Delphi パッケージを新規作成するには:
- [ファイル|新規作成|その他...|新規作成]を開きます。
- [Delphi プロジェクト|EMS|EMS パッケージ]を選択します。
- [リソースを含むパッケージを作成する]を選択し、[次へ >>]をクリックします。
- [リソース名]に「Test」と入力します。
- このサンプルでは、[データ モジュール]と[ユニット]のどちらを選択してもかまいません。
- [次へ >>]をクリックします。
- [Get]と[GetItem]を選択し、[完了]をクリックします。
プロジェクトに追加されたユニットに、以下のコードを追加します。
新しい関数を作成します:
- Delphi の場合:
type
[ResourceName('Test')]
TTestResource1 = class(TDataModule)
private
// Declare the function
function MakeJSON(I: Integer): TJSONObject; //It takes an integer as a parameter and returns a JSON Object.
published
procedure Get(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);
[ResourceSuffix('{item}')]
procedure GetItem(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);
end;
- C++ の場合(Unit1.h ファイル内):
class TTestResource1 : public TDataModule
{
__published:
private:
TJSONObject* MakeJSON(int I);
public:
__fastcall TTestResource1(TComponent* Owner);
void Get(TEndpointContext* AContext, TEndpointRequest* ARequest, TEndpointResponse* AResponse);
void GetItem(TEndpointContext* AContext, TEndpointRequest* ARequest, TEndpointResponse* AResponse);
};
Ctrl+Shift+C を押してクラス補完を使用し、この関数のスタブを implementation セクションに作成します。
追加したこの関数のコードを次のように記述します:
- Delphi の場合:
function TTestResource1.MakeJSON(I: Integer): TJSONObject;
begin
Result := TJSONObject.Create;
Result.AddPair('index', TJSONNumber.Create(I)); //Adds to the JSON object a pair {"index": I}, the index number.
Result.AddPair('value', TJSONString.Create(TestValues[I])); //Adds to the the JSON object a pair {"value":String}, the string corresponding to the index number.
end;
- C++ の場合(Unit1.cpp ファイル内):
TJSONObject* TTestResource1::MakeJSON(int I) {
TJSONObject *result = new TJSONObject();
result->AddPair(new TJSONPair("index", new TJSONNumber(I)));
result->AddPair(new TJSONPair("value", new TJSONString(TestValues[I])));
return result;
}
implementation セクションの uses 文の下に定数を追加します:
- Delphi の場合:
const
TestValues: array [0 .. 2] of string = ('a', 'b', 'c'); // It creates an array of string values.
- C++ の場合(Unit1.cpp ファイル内):
const char* TestValues[] = { "a", "b", "c" };
Get エンドポイントのコードを記述します:
- Delphi の場合:
procedure TTestResource1.Get(const AContext: TEndpointContext;
const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);
var
LJSON: TJSONArray;
I: Integer;
begin
LJSON := TJSONArray.Create;
for I := Low(TestValues) to High(TestValues) do
LJSON.Add(MakeJSON(I)); //[{"index":0,"value":"a"},{"index":1,"value":"b"},{"index":2,"value":"c"}]
AResponse.Body.SetValue(LJSON, True) // True causes AResponse to free JSON
end;
- C++ の場合(Unit1.cpp ファイル内):
void TTestResource1::Get(TEndpointContext* AContext, TEndpointRequest* ARequest, TEndpointResponse* AResponse) {
int I;
TJSONArray *LJSON = new TJSONArray();
int TestValuesSize = sizeof(TestValues) / sizeof(TestValues[0]);
for (I = 0; I < TestValuesSize; I++) {
LJSON->Add(MakeJSON(I));
}
AResponse->Body->SetValue(LJSON, true);
}
GetItem エンドポイントのコードを記述します:
- Delphi の場合:
procedure TTestResource1.GetItem(const AContext: TEndpointContext;
const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);
var
I: Integer;
begin
if not TryStrToInt(ARequest.Params.Values['item'], I) then //{"index":I,"value":value}
AResponse.RaiseBadRequest('Index expected');
if (I < 0) or (I >= Length(TestValues)) then
AResponse.RaiseBadRequest('Index out of range');
AResponse.Body.SetValue(MakeJSON(I), True);
// True causes AResponse to free JSON
end;
- C++ の場合(Unit1.cpp ファイル内):
void TTestResource1::GetItem(TEndpointContext* AContext, TEndpointRequest* ARequest, TEndpointResponse* AResponse) {
String item;
int I = 0;
int TestValuesSize = sizeof(TestValues) / sizeof(TestValues[0]);
item = ARequest->Params->Values["item"];
if (!(TryStrToInt(item, I))) {
AResponse->RaiseBadRequest("Index expected");
}
else {
if ((I < 0) || (I >= TestValuesSize))
AResponse->RaiseBadRequest("Index out of range");
else
AResponse->Body->SetValue(MakeJSON(I), true);
}
}
新規リソースのテスト
開発環境でプロジェクトをテストするには:
- [実行|実行時引数...]を選択します。
- [ホスト アプリケーション]フィールドの EMS サーバー実行可能ファイルのパスが正しいことを確認します。パスは C:\Program Files (x86)\Embarcadero\Studio\17.0\bin\EMSDevServer.exe です。
- プロジェクトを実行するとサーバー ウィンドウが開きます。
ブラウザに以下の URL を入力します。
http://localhost:8080/test/ //It uses the Get() endpoint to get all the JSON array http://localhost:8080/test/1//It uses the GetItem() endpoint to get the first item of the array. http://localhost:8080/test/2 http://localhost:8080/test/3
<localhost>:<port_number>/resource/parameter <ip_address>:<port_number>/resource/parameter