apiutils

package module
v0.0.0-...-ebaed49 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 6, 2015 License: BSD-2-Clause Imports: 5 Imported by: 0

README

apiutils

Helpful API handling utilities.

Getting Started

go get github.com/jixwanwang/apiutils

Example Usage:

package main

import (
        "log"
        "net/http"

        "github.com/jixwanwang/apiutils"
)

func main() {
        http.HandleFunc("/dowork", workwork)

        if err := http.ListenAndServe(":8080", http.DefaultServeMux); err != nil {
                log.Fatalf("Could not start server: %s", err.Error())
        }
}

func workwork(w http.ResponseWriter, r *http.Request) {
		query := r.URL.Query()
		err := apiutils.RequireParams(query, []string{"foo", "bar"})
		if err != nil {
			apiutils.ServeError(w, apiutils.NewErrorResponse(http.StatusBadRequest, err.Error()))
			return
		}
        apiutils.ServeJSON(w, []string{"Hello!", "World"})
}

Documentation

Index

Constants

View Source
const (
	StatusUnprocessableEntity = 422
)

Variables

This section is empty.

Functions

func ExtentionStatusText

func ExtentionStatusText(code int) string

func ReadParams

func ReadParams(r *http.Request) (map[string]interface{}, error)

ReadParams reads in parameters from the request, using the content type.

func RequireFormParams

func RequireFormParams(r *http.Request, params []string) error

func RequireParams

func RequireParams(form url.Values, params []string) error

func ServeError

func ServeError(w http.ResponseWriter, errRes ErrorResponse)

func ServeJSON

func ServeJSON(w http.ResponseWriter, v interface{})

Types

type ErrorResponse

type ErrorResponse struct {
	Status     int    `json:"status"`
	Message    string `json:"message"`
	StatusText string `json:"error"`
}

func NewErrorResponse

func NewErrorResponse(status int, message string) ErrorResponse

func (ErrorResponse) Error

func (T ErrorResponse) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL