HTTP API Client: Add example to documentation 22/182922/6 httpapi-client
authorMaciej Wereski <m.wereski@partner.samsung.com>
Thu, 28 Jun 2018 14:19:02 +0000 (16:19 +0200)
committerMaciej Wereski <m.wereski@partner.samsung.com>
Tue, 10 Jul 2018 08:17:41 +0000 (10:17 +0200)
Change-Id: I73d3b6e53bbae98130af1f0861752f84444e9265
Signed-off-by: Maciej Wereski <m.wereski@partner.samsung.com>
http/client/example_test.go [new file with mode: 0644]

diff --git a/http/client/example_test.go b/http/client/example_test.go
new file mode 100644 (file)
index 0000000..47580bf
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ *  Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+package client_test
+
+import (
+       "log"
+       "os"
+       "time"
+
+       "git.tizen.org/tools/boruta"
+       "git.tizen.org/tools/boruta/http/client"
+)
+
+func Example() {
+       cl := client.NewBorutaClient("http://localhost:1234")
+
+       caps := make(boruta.Capabilities)
+       caps["arch"] = "armv7l"
+       validAfter := time.Now()
+       deadline := validAfter.Add(time.Hour)
+
+       // Create new Boruta request.
+       id, err := cl.NewRequest(caps, boruta.Priority(4), boruta.UserInfo{},
+               validAfter, deadline)
+       if err != nil {
+               log.Fatalln("unable to create new request:", err)
+       }
+
+       // Check state of created request.
+       state, err := cl.GetRequestState(id)
+       if err != nil {
+               log.Fatalln("unable to check state of request:", err)
+       }
+       if state == boruta.INPROGRESS {
+               // Acquire worker if the request is in "IN PROGRESS" state.
+               access, err := cl.AcquireWorker(id)
+               if err != nil {
+                       log.Fatalln("unable to acquire worker:", err)
+               }
+               log.Println("dryad address:", access.Addr)
+               log.Println("dryad username:", access.Username)
+               // Connect to dryad using access variable (boruta.AccessInfo type).
+               // ...
+               timeout, err := cl.GetJobTimeout(id)
+               if err != nil {
+                       log.Fatalln("unable to check timeout of a job:", err)
+               }
+               log.Println("job will timeout on", timeout)
+
+               select {
+               case <-time.After(timeout.Sub(time.Now())):
+                       log.Fatalln("job timeout passed, prolong access next time")
+               default:
+                       // Do stuff.
+                       // ...
+
+                       // Close request after stuff was done.
+                       if err = cl.CloseRequest(id); err != nil {
+                               log.Fatalln("unable to close request", err)
+                       }
+                       log.Println("closed request", id)
+                       os.Exit(0)
+               }
+       }
+       log.Printf("request %d in state %s\n", id, state)
+}