App Engine Python SDK version 1.8.8
[gae.git] / python / php / sdk / google / appengine / runtime / RemoteApiProxy.php
blob6859e8840c064e2a2ad5da601eebaf5a528b4cd3
1 <?php
2 /**
3 * Copyright 2007 Google Inc.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 namespace google\appengine\runtime;
19 require_once 'google/appengine/runtime/ApiProxyBase.php';
20 require_once 'google/appengine/ext/remote_api/remote_api_pb.php';
22 require_once 'google/appengine/runtime/CallNotFoundError.php';
23 require_once 'google/appengine/runtime/ArgumentError.php';
24 require_once 'google/appengine/runtime/DeadlineExceededError.php';
25 require_once 'google/appengine/runtime/CancelledError.php';
26 require_once 'google/appengine/runtime/ApplicationError.php';
27 require_once 'google/appengine/runtime/OverQuotaError.php';
28 require_once 'google/appengine/runtime/RequestTooLargeError.php';
29 require_once 'google/appengine/runtime/ResponseTooLargeError.php';
30 require_once 'google/appengine/runtime/CapabilityDisabledError.php';
31 require_once 'google/appengine/runtime/FeatureNotEnabledError.php';
32 require_once 'google/appengine/runtime/RPCFailedError.php';
34 use google\appengine\ext\remote_api\Request;
35 use google\appengine\ext\remote_api\Response;
36 use google\appengine\runtime\RPCFailedError;
38 class RemoteApiProxy extends ApiProxyBase{
40 private $apiHost = null;
41 private $apiPort = null;
42 private $requestId = null;
44 /**
45 * Constructs an instance of RemoteApiProxy.
46 * @param string $apiHost Host to use
47 * @param int $apiPort Port to use
48 * @param string $requestId ID of the request
50 public function __construct($apiHost, $apiPort, $requestId) {
51 $this->apiHost = $apiHost;
52 $this->apiPort = $apiPort;
53 $this->requestId = $requestId;
56 /**
57 * Makes a synchronous RPC call.
58 * @param string $package Package to call
59 * @param string $call_name Specific RPC call to make
60 * @param string $request Request proto, serialised to string
61 * @param string $response Response proto string to populate
62 * @param double $deadline Optional deadline for the RPC call
64 public function makeSyncCall(
65 $package,
66 $call_name,
67 $request,
68 $response,
69 $deadline = null) {
70 if ($deadline === null) {
71 $deadline = 5;
74 $remote_request = new Request();
75 $remote_request->setServiceName($package);
76 $remote_request->setMethod($call_name);
77 $remote_request->setRequest($request->serializeToString());
78 $remote_request->setRequestId($this->requestId);
80 $serialized_remote_request = $remote_request->serializeToString();
82 $opts = array(
83 'http' => array(
84 'method' => 'POST',
85 'header' =>
86 "Content-type: application/octet-stream\r\n" .
87 'Content-Length: ' . strlen($serialized_remote_request) . "\r\n",
88 'content' => $serialized_remote_request
92 $context = stream_context_create($opts);
93 $serialized_remote_respone = file_get_contents(
94 'http://' . $this->apiHost . ':' . $this->apiPort, false, $context);
95 $remote_response = new Response();
96 $remote_response->parseFromString($serialized_remote_respone);
98 if ($remote_response->hasApplicationError()) {
99 throw new ApplicationError(
100 $remote_response->getApplicationError()->getCode(),
101 $remote_response->getApplicationError()->getDetail());
104 if ($remote_response->hasException() ||
105 $remote_response->hasJavaException()) {
106 // This indicates a bug in the remote implementation.
107 throw new RPCFailedError(sprintf('Remote implementation for %s.%s failed',
108 $package,
109 $call_name));
111 $response->parseFromString($remote_response->getResponse());