Changeset b858e6f for src/webservice


Ignore:
Timestamp:
Aug 28, 2015, 3:59:07 PM (11 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
master
Children:
552da81
Parents:
1e37648
Message:

Started implementing station register function

Location:
src/webservice
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/webservice/htdocs/index.php

    r1e37648 rb858e6f  
    4242}
    4343
     44/**
     45  * @brief Registers a new weather station
     46  */
     47function stationsRegister(){
     48        try{
     49                $app = \Slim\Slim::getInstance("siweather");
     50                $body = $app->request->getBody();
     51                $json = json_decode($body);
     52
     53                $location = new IpGeoLocator($app->request->getIp());
     54                if($location->city() != $json["city"]){
     55                        return json_encode(["result" => "unprovenlocation", "message" => "You send "]);
     56                }
     57               
     58                $app->response()->header("Content-Type", "application/json");
     59                echo json_encode($json);
     60        }
     61        catch(Exception $exception){
     62                error_log("Caught exception handling request stationsRegister: ". $exception->getMessage());
     63                $app->response()->status(400);
     64                $app->response()->header("X-Status-Reason", $exception->getMessage());
     65        }
     66}
     67
    4468$app = new \Slim\Slim
    4569(
    … …  
    4973        )
    5074);
    51 $app->setName("SiWeather Webservice");
     75$app->setName("siweather");
    5276
    5377// Only invoked if mode is "production"
    … …  
    84108
    85109$app->get("/stations", "stations");
    86 $app->get("/stations/:uuid", function ($uuid){
    87         echo "stations 123";
    88 });
    89 
    90 
     110$app->post("/stations/register", "stationsRegister");
    91111
    92112$app->run();
  • src/webservice/tests/StationsTest.php

    r1e37648 rb858e6f  
    5252        }
    5353       
     54        public function testRegisterStation()
     55        {
     56                $baseUrl = $this->_webserviceConfig->webserviceBaseUrl();
     57                $this->assertInternalType('string', $baseUrl, "baseUrl is not of type string: ". $baseUrl);
     58                $this->assertTrue(!empty($baseUrl), "baseUrl is empty: ". $baseUrl);
     59                $client = new GuzzleHttp\Client
     60                (
     61                        [
     62                                "base_uri" => $baseUrl,
     63                                "timeout" => 2.0,
     64                                "http_errors" => false,
     65                        ]
     66                );
     67                $response = $client->post("index.php/stations/register",
     68                        [ "json" => ["name" => "Cool Station", "longitude" => 23.2, "latitude" => 90, "city" => "Krefeld"]]
     69                );
     70                echo $response->getStatusCode();
     71                echo $response->getBody();
     72        }
     73       
    5474        protected function query($url){
    5575                $baseUrl = $this->_webserviceConfig->webserviceBaseUrl();
Note: See TracChangeset for help on using the changeset viewer.