Skip to content

Commit

Permalink
#2 Add option to register schema from url
Browse files Browse the repository at this point in the history
  • Loading branch information
fhussonnois committed Apr 24, 2017
1 parent 16bb1bc commit be0546e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 10 deletions.
80 changes: 71 additions & 9 deletions cmd/schemaregistrycli/registrycli.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
package main

import (
"bytes"
"errors"
"flag"
"fmt"
"github.com/fhussonnois/kafkacli/registry"
"github.com/fhussonnois/kafkacli/utils"
"io/ioutil"
"net/http"
"os"
"sort"
"strconv"
Expand Down Expand Up @@ -70,6 +73,7 @@ type CommandArgs struct {
isSchema *bool
schemaString *string
schemaJson *string
schemaUrl *string
compatibility *string
force *bool
}
Expand Down Expand Up @@ -162,6 +166,7 @@ func (p *ArgParser) withSubjectArg() *ArgParser {
func (p *ArgParser) withSchemaArg() *ArgParser {
p.Args.schemaString = p.Flag.String("schema", "", "The Avro schema json string (Required).")
p.Args.schemaJson = p.Flag.String("schema.json", "", "<file> The Avro schema json file (Required).")
p.Args.schemaUrl = p.Flag.String("schema.url", "", "<url> The Avro schema json url (Required).")
return p
}

Expand Down Expand Up @@ -388,17 +393,74 @@ func main() {
os.Exit(0)
}

func evaluateSchemaArg(args CommandArgs) (schema registry.Schema) {
// Default interface to read a JSON Schema
type SchemaReader interface {
Read(source string) (*registry.Schema, error)
}

// JSONSchemaReader implementation to read Schema from json string.
type JSONSchemaReader struct {
}

func (reader JSONSchemaReader) Read(source string) (*registry.Schema, error) {
return &registry.Schema{Value: source}, nil
}

// FileSchemaReader implementation to read Schema from file string.
type FileSchemaReader struct {
JsonReader JSONSchemaReader
}

func (reader FileSchemaReader) Read(source string) (*registry.Schema, error) {
file, err := ioutil.ReadFile(source)
if err != nil {
return nil, errors.New(string("Error while reading config file " + source + " error: " + err.Error()))
}
return reader.JsonReader.Read(string(file))
}

// HTTPSchemaReader implementation to read Schema from file string.
type HTTPSchemaReader struct {
JsonReader JSONSchemaReader
}

func (reader HTTPSchemaReader) Read(source string) (*registry.Schema, error) {
req, err := http.NewRequest("GET", source, bytes.NewBufferString(""))
req.Header.Add("Accept", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)

if resp.StatusCode >= 400 && resp.StatusCode <= 500 {
return nil, errors.New(string(body))
}
return reader.JsonReader.Read(string(body))
}

func evaluateSchemaArg(args CommandArgs) registry.Schema {

var source string
var reader SchemaReader
if *args.schemaString != "" {
schema = registry.Schema{Value: *args.schemaString}
reader = JSONSchemaReader{}
source = *args.schemaString
}
if *args.schemaJson != "" {
file, err := ioutil.ReadFile(*args.schemaJson)
if err != nil {
fmt.Fprint(os.Stderr, "Error while reading config file %s error: %v\n", *args.schemaJson, err)
os.Exit(1)
}
schema = registry.Schema{Value: string(file)}
reader = FileSchemaReader{}
source = *args.schemaJson
}
return
if *args.schemaUrl != "" {
reader = HTTPSchemaReader{}
source = *args.schemaUrl
}

schema, err := reader.Read(source)
if err != nil {
panic(err)
}
return *schema
}
1 change: 0 additions & 1 deletion registry/schemaregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,5 +232,4 @@ func sendGetResponse(method string, url string, content string) ([]byte, error)
return nil, errors.New(string(body))
}
return body, nil

}

0 comments on commit be0546e

Please sign in to comment.