episode26

command module
v0.0.0-...-5d8b580 Latest Latest
Warning

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

Go to latest
Published: May 13, 2021 License: Apache-2.0 Imports: 1 Imported by: 0

README

Consuming a REST API in Go

Go in 5 Minutes, episode 26.

In this screencast, we're going to build a command line client to consume the awesome Dark Sky API.

We'll be using cobra to build our command line client, and I did a previous episode on that package. If you haven't seen episode 18, you might want to go review that before you look at this one.

Instead of using an already-built Dark Sky API client (there are a few for Go), we're going to build our own client according to the API documentation to show some tips and tricks for building clients for any REST API.

In this screencast, we'll use the awesome gorequest package to help us build a DarkSky client from scratch.

Outline

  1. Quick primer on Cobra
  2. Quick primer on gorequest
  3. Let's check out the code!

How to Run This Code

You'll need Go version 1.11 or above to run this code. If you have an appropriate version, simply run go build -o darksky . to build.

Before you run the binary, you'll need an environment variable called DARKSKY_API_KEY set to your DarkSky API key (if you don't have one, get it from your account, or create an account if you haven't already).

Then, call the binary like so, ensuring that DARKSKY_API_KEY is set in your environment:

$ ./darksky temp --lat 45.512230 --long -122.658722

The lat and long flags are set to the latitude and longitude (respectively) of the location for which to get the temperature.

The latitude and longitude in the above example are set to Portland, OR, USA. If you'd like to try another location, you can use https://www.latlong.net/

Show Notes

Documentation

Overview

Copyright © 2019 NAME HERE <EMAIL ADDRESS>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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