모바일 Api 호출

Oracle Mobile Hub 에서 백엔드는 앱에서 사용할 수 있는 사용자정의 api, 저장 영역 모음 및 기타 리소스의 논리적 그룹화입니다. 또한 백엔드는 이러한 리소스에 액세스하기 위한 보안 컨텍스트를 제공합니다.

Android 앱에서 백엔드를 사용하기 위한 일반 단계는 다음과 같습니다.

  1. 앱에 클라이언트 SDK를 추가합니다.

  2. 백엔드에 대한 환경 및 인증 세부정보로 oracle_mobile_cloud_config.xml 를 입력합니다.

  3. 구성 정보를 로드할 수 있도록 앱에 SDK 호출을 추가합니다.

  4. 인증을 처리하기 위해 앱에 SDK 호출을 추가합니다.

  5. 사용할 다른 SDK 호출을 추가합니다.

백엔드 구성 로드

Android 클라이언트 SDK를 사용하여 Oracle Mobile Hub API를 성공적으로 완료하려면 앱의 oracle_mobile_cloud_config.xml 파일에서 백엔드의 구성이 로드되어 있어야 합니다. MobileManager 클래스를 사용하여 다음 작업을 수행합니다.

MobileManager.getManager().getMobileBackend(this)

플랫폼 Api 호출

모바일 백엔드의 구성 정보가 앱에 로드되면 클라이언트 SDK 클래스를 호출할 수 있습니다.

Android SDK의 루트 객체는 MobileManager 입니다. MobileManager 객체는 MobileBackend 객체를 관리합니다.

MobileBackend 객체는 플랫폼 api 호출 및 정의한 사용자 정의 api를 비롯하여 애플리케이션과 연관된 백엔드 간의 접속, 인증 및 기타 트랜잭션을 관리합니다. ServiceProxy 인스턴스 (예: StorageLocation) 를 통해 플랫폼 api에 대한 호출을 관리합니다.

다음은 이러한 클래스를 사용하여 스토리지 API를 사용하여 이미지를 업로드하는 방법의 예입니다.

try {
    Storage storage = MobileManager.getManager().getMobileBackend(this).getServiceProxy(Storage.class);
    StorageCollection imagesCollection = storage.getStorageCollection("FIF_Images");
    StorageObject imageToUpload = new StorageObject(null, imageBytes, "image/jpeg");
    StorageObject uploadedImage = imagesCollection.post(imageToUpload);
} catch(ServiceProxyException e) {int errorCode = e.getErrorCode();
            ...
}

생성된 ServiceProxy 인스턴스는 API에 액세스하는 데 필요한 모바일 백엔드 인증서로 HTTP 헤더를 생성하는 것을 포함하여 스토리지 플랫폼 API에 대한 호출을 관리합니다.

저장 영역 API를 사용하여 이미지를 검색하는 방법은 다음과 같습니다.

try {
    Storage storage = MobileManager.getManager().getMobileBackend(this).getServiceProxy(Storage.class);
    StorageCollection imagesCollection = storage.getStorageCollection("FIF_Images");
    StorageObject image = imagesCollection.get("3x4mp1e-st0r4g3-0bj3ct-k3y");byte[] imageBytes = image.getPayloadBytes();
} catch(ServiceProxyException e) {int errorCode = e.getErrorCode();
 ...
}

사용자정의 Api 호출

클라이언트 SDK는 Oracle Mobile Hub 에서 사용자정의 api 호출을 간소화하기 위해 권한 부여 클래스의 CustomHttpResponse 클래스, GenericCustomCodeClientCallBack 인터페이스 및 invokeCustomCodeJSONRequest 메소드를 제공합니다. 요청 페이로드가 JSON 또는 비어 있고 응답 페이로드가 JSON 또는 비어 있는 끝점에서 REST 메소드(GET, PUT, POST 또는 DELETE) 를 호출할 수 있습니다.

GenericCustomCodeClientCallBack 를 사용하여 응답에 대한 처리기를 생성합니다 (CustomHttpResponse 객체 형식으로 반환됨).

사용자정의 API를 호출하려면 Authorization 객체에서 invokeCustomCodeJSONRequest(GenericCustomCodeClientCallBack restClientCallback, JSONObject data, String functionName, RestClient.HttpMethod httpMethod) 를 호출합니다.

사용자 정의 API 끝점을 호출하려면 다음과 같이 사용할 수 있습니다.

import org.json.JSONObject;
import oracle.cloud.mobile.customcode.CustomHttpResponse;
import oracle.cloud.mobile.customcode.GenericCustomCodeClientCallBack;
import oracle.cloud.mobile.mobilebackend.MobileManager;
.......
 
final GenericCustomCodeClientCallBack genericCustomCodeClientCallBack = new GenericCustomCodeClientCallBack() {
    @Override
    public void requestCompleted(CustomHttpResponse response, JSONObject data, Exception e) {
        boolean getResponse = (response.getHttpStatus() >=200 && response.getHttpStatus() <300);
                   
        // write any logic based on above response
    }
};
AuthorizationAgent authorization = MobileManager.getManager().getMobileBackend(this).getAuthorization();
            
authorization.authenticate(mActivity, "user1", "pass1", successCallback);

........
// after the user successfully authenticates, make a call to the custom API endpoint
authorization.invokeCustomCodeJSONRequest(genericCustomCodeClientCallBack, null, "TaskApi/tasks", RestClient.HttpMethod.GET);