Coder Social home page Coder Social logo

go-rest's Introduction

Usage

Usage examples for SendGrid REST library

Initialization

package main

import (
	"encoding/json"
	"fmt"
	"os"

	"github.com/sendgrid/rest"
)

// Build the URL
const host = "https://api.sendgrid.com"
endpoint := "/v3/api_keys"
baseURL := host + endpoint

// Build the request headers
key := os.Getenv("SENDGRID_API_KEY")
Headers := make(map[string]string)
Headers["Authorization"] = "Bearer " + key

Table of Contents

GET

GET Single

method = rest.Get

// Make the API call
request = rest.Request{
    Method:  method,
    BaseURL: baseURL + "/" + apiKey,
    Headers: Headers,
}
response, err = rest.API(request)
if err != nil {
    fmt.Println(err)
} else {
    fmt.Println(response.StatusCode)
    fmt.Println(response.Body)
    fmt.Println(response.Headers)
}

GET Collection

method := rest.Get

// Build the query parameters
queryParams := make(map[string]string)
queryParams["limit"] = "100"
queryParams["offset"] = "0"

// Make the API call
request := rest.Request{
    Method:      method,
    BaseURL:     baseURL,
    Headers:     Headers,
    QueryParams: queryParams,
}
response, err := rest.API(request)
if err != nil {
    fmt.Println(err)
} else {
    fmt.Println(response.StatusCode)
    fmt.Println(response.Body)
    fmt.Println(response.Headers)
}

DELETE

method = rest.Delete

// Make the API call
request = rest.Request{
	Method:      method,
	BaseURL:     baseURL + "/" + apiKey,
	Headers:     Headers,
	QueryParams: queryParams,
}
response, err = rest.API(request)
if err != nil {
	fmt.Println(err)
} else {
	fmt.Println(response.StatusCode)
	fmt.Println(response.Headers)
}

POST

method = rest.Post

// Build the request body
var Body = []byte(`{
    "name": "My API Key",
    "scopes": [
        "mail.send",
        "alerts.create",
        "alerts.read"
    ]
}`)

// Make the API call
request = rest.Request{
	Method:      method,
	BaseURL:     baseURL,
	Headers:     Headers,
	QueryParams: queryParams,
	Body:        Body,
}
response, err = rest.API(request)
if err != nil {
	fmt.Println(err)
} else {
	fmt.Println(response.StatusCode)
	fmt.Println(response.Body)
	fmt.Println(response.Headers)
}

// Get a particular return value.
// Note that you can unmarshall into a struct if
// you know the JSON structure in advance.
b := []byte(response.Body)
var f interface{}
err = json.Unmarshal(b, &f)
if err != nil {
	fmt.Println(err)
}
m := f.(map[string]interface{})
apiKey := m["api_key_id"].(string)

PUT

method = rest.Put

// Build the request body
Body = []byte(`{
    "name": "A New Hope",
    "scopes": [
        "user.profile.read",
        "user.profile.update"
    ]
}`)

// Make the API call
request = rest.Request{
	Method:  method,
	BaseURL: baseURL + "/" + apiKey,
	Headers: Headers,
	Body:    Body,
}
response, err = rest.API(request)
if err != nil {
	fmt.Println(err)
} else {
	fmt.Println(response.StatusCode)
	fmt.Println(response.Body)
	fmt.Println(response.Headers)
}

PATCH

method = rest.Patch

// Build the request body
Body = []byte(`{
    "name": "A New Hope"
}`)

// Make the API call
request = rest.Request{
	Method:  method,
	BaseURL: baseURL + "/" + apiKey,
	Headers: Headers,
	Body:    Body,
}
response, err = rest.API(request)
if err != nil {
	fmt.Println(err)
} else {
	fmt.Println(response.StatusCode)
	fmt.Println(response.Body)
	fmt.Println(response.Headers)
}

go-rest's People

Watchers

Trương Hải Nam avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.