Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonvfx committed May 26, 2015
0 parents commit c124b69
Show file tree
Hide file tree
Showing 17 changed files with 1,583 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof

*.out
.env

gin-bin
go-restful

Godeps/_workspace
33 changes: 33 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Godeps/Readme

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 Brandon Ashworth

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

35 changes: 35 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# SG Restful - WIP

## NOT PRODUCTION READY!

SG Restful is a restful interface for the [Shotgun](http://shotgunsoftware.com) Api.


## What works currently

### Entities

- Read
- Get by id
- Get all
- Returning fields
- Pagination
- Create
- Update
- Delete


## Auth

SG Restful using basic auth for getting script and user credentials. This may change in the future.

Script `Authorization` header:
```
Basic <base64 scipt_name:sript_key>
```

User `Authorization` header:
( I don't suggest using this unless you have an internal Shotgun sever. )
```
Basic-user <base64 user_name:user_password>
```
101 changes: 101 additions & 0 deletions entity_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package main

import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"strings"

log "github.com/Sirupsen/logrus"
"github.com/gorilla/context"
"github.com/gorilla/mux"
)

type CreateResponse struct {
Results map[string]interface{} `json:"results"`
Exception bool `json:"exception",omitempty`
Message string `json:"message",omitempty`
ErrorCode int `json:"error_code",omitempty`
}

func entityCreateHandler(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
entity_type := vars["entity_type"]
log.Debug("Entity Type:", entity_type)

var postData map[string]interface{}
postBody, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Error(err)
return
}

err = json.Unmarshal(postBody, &postData)
if err != nil {
log.Error(err)
return
}
log.Debug("Post Data:", postData)

fields := make([]map[string]interface{}, len(postData))
i := 0
for key, value := range postData {
field := make(map[string]interface{})
field["field_name"] = key
field["value"] = value
fields[i] = field
i++
}

query := map[string]interface{}{
"return_fields": []string{"id"},
"type": entity_type,
"fields": fields,
}

sg_conn, ok := context.GetOk(req, "sg_conn")
if !ok {
rw.WriteHeader(http.StatusInternalServerError)
return
}
sg := sg_conn.(Shotgun)
sgReq, err := sg.Request("create", query)
if err != nil {
log.Error("Request Error: ", err)
return
}

var createResp CreateResponse
respBody, err := ioutil.ReadAll(sgReq.Body)
if err != nil {
log.Error(err)
return
}
err = json.Unmarshal(respBody, &createResp)
if err != nil {
log.Error(err)
return
}
log.Debug("Response: ", createResp)

if createResp.Exception {
if strings.Contains(createResp.Message, "unique") {
rw.WriteHeader(http.StatusConflict)
} else {
rw.WriteHeader(http.StatusBadRequest)
}
rw.Write(bytes.NewBufferString(createResp.Message).Bytes())
return
}

jsonResp, err := json.Marshal(createResp.Results)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}

rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(http.StatusCreated)
rw.Write(jsonResp)
}
79 changes: 79 additions & 0 deletions entity_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
"encoding/json"
"io/ioutil"
"net/http"
"strconv"

log "github.com/Sirupsen/logrus"
"github.com/gorilla/context"
"github.com/gorilla/mux"
)

type DeleteResponse struct {
Results bool `json:"results"`
// Exception bool `json:"exception",omitempty`
// Message string `json:"message",omitempty`
// ErrorCode int `json:"error_code",omitempty`
}

func entityDeleteHandler(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
entity_type := vars["entity_type"]
log.Debug("Entity Type:", entity_type)

var entityId int
var err error
entityIdStr, hasId := vars["id"]
if hasId {
entityId, err = strconv.Atoi(entityIdStr)
log.Debug(entityId)
if err != nil {
rw.WriteHeader(http.StatusBadRequest)
return
}
} else {
rw.WriteHeader(http.StatusBadRequest)
return
}

query := map[string]interface{}{
"type": entity_type,
"id": entityId,
}

sg_conn, ok := context.GetOk(req, "sg_conn")
if !ok {
rw.WriteHeader(http.StatusInternalServerError)
return
}
sg := sg_conn.(Shotgun)
sgReq, err := sg.Request("delete", query)
if err != nil {
log.Error("Request Error: ", err)
return
}

var deleteResp DeleteResponse
respBody, err := ioutil.ReadAll(sgReq.Body)
if err != nil {
log.Error(err)
return
}

err = json.Unmarshal(respBody, &deleteResp)
if err != nil {
log.Error(err)
return
}

log.Debug("Response: ", deleteResp)

if !deleteResp.Results {
rw.WriteHeader(http.StatusNotFound)
return
}

rw.WriteHeader(http.StatusOK)
}
Loading

0 comments on commit c124b69

Please sign in to comment.