Skip to content

Commit

Permalink
fix: Bugfix of session and statement requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
locona committed Feb 7, 2020
1 parent 1525349 commit b36e4ac
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 39 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.envrc
.vscode
livy
3 changes: 0 additions & 3 deletions .vscode/settings.json

This file was deleted.

9 changes: 7 additions & 2 deletions gensupport/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ package gensupport
import (
"bytes"
"encoding/json"
"fmt"
"io"
"strings"
)

func JSONReader(v interface{}) (io.Reader, error) {
buf := new(bytes.Buffer)
err := json.NewEncoder(buf).Encode(v)
encoder := json.NewEncoder(buf)
encoder.SetEscapeHTML(false)
err := encoder.Encode(v)
if err != nil {
return nil, err
}
return buf, nil

return bytes.NewBufferString(strings.TrimRight(buf.String(), "\n")), nil
}
3 changes: 3 additions & 0 deletions gensupport/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
)

func SendRequest(client *http.Client, req *http.Request) (*http.Response, error) {
reqHeaders := make(http.Header)
reqHeaders.Set("Content-Type", "application/json")
req.Header = reqHeaders
return client.Do(req)

// TODO: Send request.
Expand Down
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
module github.com/locona/livy

go 1.12

require (
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/k0kubun/pp v3.0.1+incompatible
github.com/lithammer/dedent v1.1.0
github.com/mattn/go-colorable v0.1.4 // indirect
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 h1:uC1QfSlInpQF+M0ao65imhwqKnz3Q2z/d8PWZRMQvDM=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k=
github.com/k0kubun/pp v3.0.1+incompatible h1:3tqvf7QgUnZ5tXO6pNAZlrvHgl6DvifjDrd9g2S9Z40=
github.com/k0kubun/pp v3.0.1+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg=
github.com/lithammer/dedent v1.1.0 h1:VNzHMVCBNG1j0fh3OrsFRkVUwStdDArbgBWoPAffktY=
github.com/lithammer/dedent v1.1.0/go.mod h1:jrXYCQtgg0nJiN+StA2KgR7w6CiQNv9Fd/Z9BP0jIOc=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
18 changes: 5 additions & 13 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package livy

import (
"context"
"errors"
"net/http"
)

Expand All @@ -20,23 +19,16 @@ type Service struct {
Statements *StatementsService
}

func NewService(ctx context.Context) (*Service, error) {
s, err := New(http.DefaultClient)
if err != nil {
return nil, err
}

return s, nil
func NewService(ctx context.Context) *Service {
return New(http.DefaultClient)

}

func New(client *http.Client) (*Service, error) {
if client == nil {
return nil, errors.New("client is nil")
}
func New(client *http.Client) *Service {
s := &Service{client: client, BasePath: basePath}
s.Batches = NewBatchesService(s)
s.Sessions = NewSessionsService(s)
s.Statements = NewStatementsService(s)
return s, nil

return s
}
35 changes: 17 additions & 18 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type SessionsListCall struct {

type Session struct {
ID int `json:"id"`
AppID string `json:"appid"`
AppID string `json:"appId"`
Owner string `json:"owner"`
ProxyUser string `json:"proxyUser"`
Kind SessionKind `json:"kind"`
Expand All @@ -73,10 +73,9 @@ func NewSessionsService(s *Service) *SessionsService {
return rs
}

func (r *SessionsService) List(from int, size int) *SessionsListCall {
func (r *SessionsService) List() *SessionsListCall {
c := &SessionsListCall{s: r.s}
c.from = from
c.size = size

return c
}

Expand Down Expand Up @@ -149,35 +148,35 @@ func (c *SessionsGetCall) doRequest() (*http.Response, error) {

type InsertSessionRequest struct {
// The name of this session
Name string `json:"name"`
Name string `json:"name,omitempty"`
// The session kind
Kind SessionKind `json:"kind"`
// User to impersonate when starting the session
ProxyUser string `json:"proxyUser"`
ProxyUser string `json:"proxyUser,omitempty"`
// jars to be used in this session
Jars []string `json:"jars"`
Jars []string `json:"jars,omitempty"`
// Python files to be used in this session
PyFiles []string `json:"pyFiles"`
PyFiles []string `json:"pyFiles,omitempty"`
// files to be used in this session
Files []string `json:"files"`
Files []string `json:"files,omitempty"`
// Amount of memory to use for the driver process
DriverMemory string `json:"driverMemory"`
DriverMemory string `json:"driverMemory,omitempty"`
// Number of cores to use for the driver process
DriverCores int `json:"driverCores"`
DriverCores int `json:"driverCores,omitempty"`
// Amount of memory to use per executor process
ExecutorMemory string `json:"executorMemory"`
ExecutorMemory string `json:"executorMemory,omitempty"`
// Number of cores to use for each executor
ExecutorCores int `json:"executorCores"`
ExecutorCores int `json:"executorCores,omitempty"`
// Number of executors to launch for this session
NumExecutors int `json:"num_executors"`
NumExecutors int `json:"num_executors,omitempty"`
// Archives to be used in this session
Archives []string `json:"archives"`
Archives []string `json:"archives,omitempty"`
// The name of the YARN queue to which submitted
Queue string `json:"queue"`
Queue string `json:"queue,omitempty"`
// Spark configuration properties
Conf map[string]string `json:"conf"`
Conf map[string]string `json:"conf,omitempty"`
// Timeout in second to which session be orphaned
HeartbeatTimeoutInSecond int `json:"heartbeatTimeoutInSecond"`
HeartbeatTimeoutInSecond int `json:"heartbeatTimeoutInSecond,omitempty"`
}

type SessionsInsertCall struct {
Expand Down
11 changes: 8 additions & 3 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"

"github.com/locona/livy/gensupport"
Expand Down Expand Up @@ -122,7 +123,7 @@ func (c *StatementsGetCall) doRequest() (*http.Response, error) {

type InsertStatementRequest struct {
// The code to execute
Code string
Code string `json:"code"`
}

type StatementsInsertCall struct {
Expand All @@ -145,9 +146,9 @@ func (c *StatementsInsertCall) Do() (*Statement, error) {
return nil, err
}

s, _ := ioutil.ReadAll(res.Body)
statement := &Statement{}
err = gensupport.DecodeResponse(statement, res)

if err != nil {
return nil, err
}
Expand All @@ -156,7 +157,11 @@ func (c *StatementsInsertCall) Do() (*Statement, error) {

func (c *StatementsInsertCall) doRequest() (*http.Response, error) {
url := c.s.BasePath + fmt.Sprintf("/sessions/%v/statements", c.sessionID)
var body io.Reader = nil
body, err := gensupport.JSONReader(c.insertStatementRequest)
if err != nil {
return nil, err
}

req, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, err
Expand Down

0 comments on commit b36e4ac

Please sign in to comment.