Giter Site home page Giter Site logo

nationbuilderapi's Introduction

NationBuilderAPI

A .NET binding to NationBuilder's Web API.

Sample Usage

using NationBuilderAPI.V1;

NationBuilderSession nbSession = new NationBuilderSession("your-nation-slug", "your-access-token");

// Iterate over all people in our nation:
foreach (AbbreviatedPerson p in nbSession.GetPeopleResults())
{
  // Use the person data.  E.g., display the name and e-mail:
  outpTextBox.AppendText("E-mail: " + p.email + ", Name: " + p.first_name + " " + p.last_name + "\n");
}
  • Install the NationBuilderAPI NuGet package in your project. (If, for some reason, you can't install the NuGet package, compile this project, and reference the <NationBuilderAPI.dll> in your project.)

  • You need to obtain an access token for your NationBuilder app in order to be able to call NationBuilder's API methods (endpoints). You can obtain one by completing an OAuth login and token exchange. Once you have the access token you can use it as shown above. (You can also create a 'test token' through Nation Builder's web API.)

Receive "Person created" Webhooks in a WCF Service:

// Expose this method in your WCF service, and use its URL to receive "Person created"
// webhook requests:
[WebInvoke(Method = "POST",
    BodyStyle = WebMessageBodyStyle.Bare,
    ResponseFormat = WebMessageFormat.Json,
    UriTemplate = "WebhookReception/NationBuilder/PersonCreated")]
public void NationBuilder_PersonCreated(
    NationBuilderAPI.V1.Webhooks.V4.WebhookContent<
      NationBuilderAPI.V1.Webhooks.V4.PersonWebhookPayload>
        webhookContent)
{
    NationBuilder_WebhookRequest_CheckAccess(webhookContent);

    // !!!: Process your webhookContent here.
    // Use webhookContent.payload.person.ToPerson() to get the base
    // NationBuilderAPI.V1.Person.  If you simply cast
    // webhookContent.payload.person, you'll have to resolve types when
    // serializing.
}

/// <summary>
/// Validate a webhook's security token.
/// </summary>
/// <param name="webhookContent">The webhook input payload to validate.</param>
/// <returns><c>true</c>, meaning that access was granted, or throws a
///   <see cref="System.ServiceModel.Security.SecurityAccessDeniedException"/>
///   if access was denied.</returns>
private bool NationBuilder_WebhookRequest_CheckAccess<PayloadT>(
    NationBuilderAPI.V1.Webhooks.V4.WebhookContent<PayloadT>
      webhookContent)
{
    if (webhookContent.token != "your-webhook-authentication-token")
    {
        throw new System.ServiceModel.Security.SecurityAccessDeniedException(
            "Invalid Nation Builder webhook token!");
    }

    return true;
}

Use Custom Fields

[DataContract]
class CustomPerson : Person
{
    [DataMember]
    double height;
	
	// . . .
}

[DataContract]
class CustomDonation : Donation
{
    [DataMember]
    string in_memory_of;
	
	// . . .
}


public void UseCustomFields()
{
    using (var session = new NationBuilderSession<CustomPerson, CustomDonation>(nationSlug, nationAccessToken))
    {
        var shownPersonResponse = session.ShowPerson("123");
        CustomPerson shownPerson = shownPersonResponse.person;
        
		// . . .
    }
}

Migration from v. 1.2 and Earlier

  • Since the addition of custom fields, methods returning PersonResponse now take a type parameter for the custom Person type:
// Convert:
NationBuilderAPI.V1.PersonResponse res;

// into:
NationBuilderAPI.V1.PersonResponse<NationBuilderAPI.V1.Person> res;

License

This project is distributed under GPL v.2. If you need a different license for a commercial project, send me a note.

-- Pav

nationbuilderapi's People

Contributors

pavpen avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

nationbuilderapi's Issues

Library status?

I'm considering using the NationBuilder API, and would love to do it from C#. The library was last updated 2 years ago... I'm guessing the underlying API has changed a fair amount since then - any plans for continuing this, or is it effectively on-hold? (I completely understand if so... open source commitments can play havoc with a home life.)

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.