api

package
v0.0.0-...-cee764d Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FetchApiData

func FetchApiData(baseAPI string) []byte

FetchApiData requests, gets response with custom headers. and then returns the parsed response.Body bytes into string.

Requirements : $ curl -H "Accept: application/json" https://icanhazdadjoke.com/

func GetJokeParseData

func GetJokeParseData() string

GetJokeParseData()

func GetOkejoke

func GetOkejoke()

Function getOkejoke makes an API call to fetch joke.

# It uses the `http` package. API Calls.

func GetRandomJokeWithTerm

func GetRandomJokeWithTerm(jokeTerm string)

GetRandomJokeWithTerm searches through jokes with a search term.

Usage: $ okejoke random --term=hipster

Example: $ curl -H "Accept: application/json" "https://icanhazdadjoke.com/search?term=hipster" Main concern in the json response are the following terms: "search_term":"hipster","status":200,"total_jokes":3,"total_pages":1

fmt.Printf("Search term: %v", jokeTerm) // for testing function.

Types

type DataJSON

type DataJSON struct {
	ID     string `json:"id"`
	Body   string `json:"joke"`
	Status int    `json:"status"`
}

data := DataJSON{} Store data in a variable.

func FetchUserTermFlagData

func FetchUserTermFlagData(jokeTerm string) (totalJokes int, jokeList []DataJSON)

FetchUserTermFlagData passes search term Cobra cmd random.go init() --flags.

Wildcards Description ---- https://www.shell-tips.com/bash/wildcards-globbing/#gsc.tab=0 ? A question-mark is a pattern that matches any single character. * An asterisk is a pattern that matches any number of any characters, including the null string/none. [...] The square brackets matches any one of the enclosed characters.

17:34  ➜  go run main.go random --term=joke

# Find search terms with wildcard-globbing curl -H "Accept: application/json" "https://icanhazdadjoke.com/search?term=hipster"

func UnmarshalBytes

func UnmarshalBytes(bRes []byte, d DataJSON) DataJSON

UnmarshalBytes parses the JSON\-encoded data and stores the result.

in the value pointed to by v\. If v is nil or not a pointer, Unmarshal returns an InvalidUnmarshalError.

type Joke

type Joke struct {
	ID     string `json:"id"`
	Joke   string `json:"joke"`
	Status int    `json:"status"`
}

Joke struct

# Example $ curl -H "Accept: application/json" https://icanhazdadjoke.com/

{
	 "id":      "aFtzPRSnbxc",
	 "joke":    "Two fish are in a tank, one turns to the other and says,
	            \"how do  you drive this thing?\"",
	 "status":  200
}

func GetJokeDataWithTerm

func GetJokeDataWithTerm(jokeTerm string) (totalJokes int, jokeList []Joke)

GetJokeDataWithTerm

API Call.

type SearcResJSON

type SearcResJSON struct {
	Results    json.RawMessage `json:"results"`
	SearchTerm string          `json:"search_term"`
	Status     int             `json:"status"`
	TotalJokes int             `json:"total_jokes"`
}

Construct SearchResJSON with curl results. $ curl -H "Accept: application/json" https://icanhazdadjoke.com/search { "current_page":1,"limit":20,"next_page":2,"previous_page":1, "results":[ {"id":"0189hNRf2g","joke":"I'm tired of following my dreams. I'm just going to ask them where they are going and meet up with them later."},{"id":"08EQZ8EQukb","joke":"Did you hear about the guy whose whole left side was cut off? He's all right now."},{"id":"08xHQCdx5Ed","joke":"Why didn\u2019t the skeleton cross the road? Because he had no guts."},{"id":"0DQKB51oGlb"," joke":"What did one nut say as he chased another nut? I'm a cashew!"},{"id":"0DtrrOZDlyd","joke":"Chances are if you' ve seen one shopping center, you've seen a mall."},{"id":"0LuXvkq4Muc","joke":"I knew I shouldn't steal a mixer from work, but it was a whisk I was willing to take."},{"id":"0ga 2EdN7prc","joke":"How come the stadium got hot after the game? Because all of the fans left."},{"id":"0oO71TSv4Ed","joke":"Why was it called the dar k ages? Because of all the knights. "},{"id":"0oz51ozk3ob","joke":"A steak pun is a rare medium well done."},{"id":"0ozAXv4Mmjb","joke":"Why did the tomato blush? Because it saw the salad dressing."},{"id":"0wcFBQfiGBd","joke":"Did you hear the joke about the wandering nun? She was a roman catho lic."},{"id":"189xHQ7pOuc","joke":"What creature is smarter than a talking parrot? A spelling bee."},{"id":"18Elj3EIYvc","joke":"I'll tell you what often gets over looked... garden fences."},{"id":"18h3wcU8xAd","joke":"Why did the kid cross the playground? To get to the other slide."},{"id":"1DI RSfx51Dd","joke":"Why do birds fly south for the winter? Because it's too far to walk."},{"id":"1DQZDY0gVnb","joke":"What is a centipedes's favorite Beatle song? I want to hold your hand, hand, hand, hand..."},{"id":"1DQZvXvX8Ed","joke":"My first time using an elevator was an uplifting experien ce. The second time let me down."},{"id":"1DQZvcFBdib","joke":"To be Frank, I'd have to change my name."},{"id":"1Dt4M7Ufaxc","joke":"Slept like a l og last night \u2026 woke up in the fireplace."},{"id":"1T01LBXLuzd","joke":"Why does a Moon-rock taste better than an Earth-rock? Because it's a li ttle meteor."} ], "search_term":"", "status":200, "total_jokes":649, "total_pages":33 }

type SearchResult

type SearchResult struct {
	Results    json.RawMessage `json:"results"`
	SearchTerm string          `json:"search_term"`
	Status     int             `json:"status"`
	TotalJokes int             `json:"total_jokes"`
}

SearchResult struct

Shape of the data to work with API response.

Results: RawMessage is a raw encoded JSON value. It implements Marshaler and Unmarshaler and can be used to delay JSON decoding or precompute a JSON encoding.

Jump to

Keyboard shortcuts

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