GithubHelp home page GithubHelp logo

zeet-dev / cloudflare-graphql-go Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 224 KB

Go client library SDK for interacting with Cloudflare's GraphQL API

Home Page: https://github.com/zeet-dev/cloudflare-graphql-go

License: MIT License

Makefile 4.03% Go 95.97%
cloudflare cloudflare-analytics cloudflare-api go golang graphql sdk

cloudflare-graphql-go's Introduction

Cloudflare GraphQL Go

License

Cloudflare GraphQL Go is a Go client library SDK for interacting with Cloudflare's GraphQL API. This library provides a convenient and idiomatic way to query Cloudflare's analytics data using GraphQL.

Table of Contents

Features

  • Easy-to-use Go client for Cloudflare's GraphQL API
  • Supports querying analytics data for workers and zones
  • Automatically generated Go types from GraphQL schema
  • Includes examples for quick start

Installation

To install the Cloudflare GraphQL Go library, use go get:

go get github.com/zeet-dev/cloudflare-graphql-go

Usage

First, import the library in your Go code:

import (
    "github.com/zeet-dev/cloudflare-graphql-go/pkg/cloudflaregraphql"
    "github.com/cloudflare/cloudflare-go"
)

Initialize the client:

package main

import (
    "context"
    "log"
    "os"
    "time"

    "github.com/cloudflare/cloudflare-go"
    "github.com/zeet-dev/cloudflare-graphql-go/pkg/cloudflaregraphql"
)

func main() {
    cfApiToken := os.Getenv("CF_API_TOKEN")

    debug := os.Getenv("DEBUG") == "true"

    api, err := cloudflare.NewWithAPIToken(cfApiToken)
    if err != nil {
        log.Fatal(err)
    }

    client, err := cloudflaregraphql.New(
        func(o *cloudflaregraphql.ClientOption) {
            o.CloudflareAPIToken = cfApiToken
            o.Debug = debug
        })
    if err != nil {
        log.Fatal(err)
    }

    ctx := context.Background()

    zones, err := api.ListZonesContext(ctx)
    if err != nil {
        log.Fatal(err)
    }

    for _, zone := range zones.Result {
        zoneTag := zone.ID

        currentDate := time.Now().UTC().Format("2006-01-02")
        sinceDate := time.Now().UTC().AddDate(0, 0, -3).Format("2006-01-02")
        result, err := client.GetZoneAnalyticsByDayQuery(ctx, &zoneTag, sinceDate, currentDate)
        if err != nil {
            log.Fatal(err)
        }

        log.Println("Zone Analytics Results:", zone.Name)
        for _, zoneResult := range result.Viewer.Zones[0].Zones {
            date := zoneResult.Dimensions.Timeslot
            value := zoneResult.Sum.Requests
            log.Printf("Date: %s, Requests: %d\n", date, value)
        }
    }
}

See cmd/cfgo/cfgo.go for more complete examples

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository.
  2. Create a new branch for your feature or bugfix.
  3. Write your code and tests.
  4. Ensure all tests pass.
  5. Submit a pull request.

Please make sure to update tests as appropriate.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Special thanks to the contributors and the open-source community for their continuous support.
  • Cloudflare for providing the GraphQL API.

cloudflare-graphql-go's People

Contributors

zzh8829 avatar

Stargazers

 avatar

Watchers

 avatar  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.